Found 10784 Articles for Python

How to create a sample dataset using Python Scikit-learn?

Gaurav Leekha
Updated on 04-Oct-2022 07:59:33

662 Views

In this tutorial, we will learn how to create a sample dataset using Python Scikit-learn. There are various built-in scikit-learn datasets which we can use easily for our ML model but sometimes we need some toy dataset. For this purpose, scikit-learn python library provides us a great sample dataset generator. Creating Sample Blob Dataset using Scikit-Learn For creating sample blob dataset, we need to import sklearn.datsets.make_blobs which is very fast and easy to use. Example In the below given example, let’s see how we can use this library to create sample blob dataset. # Importing libraries from sklearn.datasets import make_blobs ... Read More

How to Install Python Scikit-learn on Different Operating Systems?

Gaurav Leekha
Updated on 04-Oct-2022 07:47:09

12K+ Views

Scikit-learn, also known as Sklearn, is the most useful and robust open-source Python library that implements machine learning and statistical modeling algorithms including classification, regression, clustering, and dimensionality reduction using a unified interface. Scikit-learn library is written in Python and is built upon other Python packages such as NumPy (Numerical Python), and SciPy (Scientific Python). Installing Scikit-learn on Windows using pip To install Scikit-learn on Windows, follow the steps given below − Step1-Make Sure Python and pip is preinstalled Open the command prompt on your system and type the following commands to check whether Python and pip is installed or ... Read More

How to find the solidity and equivalent diameter of an object in an image using OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 12:13:35

951 Views

The solidity of an object is computed as the ratio of contour area to its convex hull area. So to compute the solidity, we first have to find the contour area and convex hull area. The contour area of an object can be found using cv2.contourArea() function. Equivalent Diameter is the diameter of the circle whose area is the same as the contour area. The solidity and equivalent diameter can be computed as below − Syntax area = cv2.contourArea(cnt) hull = cv2.convexHull(cnt) hull_area = cv2.contourArea(hull) solidity = float(area)/hull_area equi_diameter = np.sqrt(4*area/np.pi) Where, cnt is a numpy array of the ... Read More

How to compute the aspect ratio of an object in an image using OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 12:03:24

4K+ Views

The aspect ratio of an object is computed as the ratio between the width and height of the bounding rectangle of the object. So, to compute the aspect ratio, we first have to find the bounding rectangle of the object. Bounding rectangle of an object can be found using cv2.boundingRect() function. It accepts the contour points of the object and returns top-left coordinate (x, y) and (width, height) of the bounding rectangle. We use the width and height to compute the aspect ratio. Syntax x, y, w, h = cv2.boundingRect(cnt) aspect_ratio = float(w)/h Here, "cnt" is a numpy array ... Read More

How to compute the extent of an object in image using OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 12:05:42

1K+ Views

The extent of an object is computed as the ratio of contour area to its bounding rectangle area. So, to compute the extent, we first have to find the contour area and bounding rectangle area. The contour area of an object can be found using cv2.contourArea() function. Syntax The extent can be computed as follows − area = cv2.contourArea(cnt) x, y, w, h = cv2.boundingRect(cnt) rect_area = w*h extent = float(area)/rect_area Here, "cnt" is a numpy array of the contour points of an object in the image. Steps You can use the following steps to compute extent of an ... Read More

How to perform bilateral filter operation on an image in OpenCV using Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 10:16:11

1K+ Views

A bilateral filter operation is highly effective in smoothing the image and removing noises. The main advantage of the bilateral filtering is that it preserves the edges unlike in average and median filtering. Bilateral filtering operation is slower in comparison to other filters. We can perform bilateral filtering on an image using the cv2.bilateralFilter() method. Syntax Following is the syntax of this method. cv2.bilateralFilter(img, d, sigmaColor, sigmaSpace) This method accepts the following parameters − img − The input image on which the bilateral filter operation to be applied. d − A variable of the type integer representing the ... Read More

How to fit the ellipse to an object in an image using OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 10:08:55

8K+ Views

We can fit an ellipse to an object using the function cv2.fitEllipse(). The ellipse is inscribed in a rotated rectangle. The rotated rectangle is a bounding rectangle with minimum area enclosing the object. Syntax The syntax used for this function is − ellipse = cv2.fitEllipse(cnt) Where, "cnt" is the contour points. It is represented as an array of contour points. Output − It returns a tuple of tuples in ((x, y), (majorAxis, minorAxis), angle) format. (x, y) is the coordinates of center and (majorAxis, minorAxis) is the lengths of minor and major axes and angle is the rotation angle ... Read More

How to perform bitwise OR operation on two images in OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 10:05:46

1K+ Views

In OpenCV, a color (RGB) image is represented as a 3-dimensional numpy array. The pixel values of an image are stored using 8 bit unsigned integers (uint8) in range from 0 to 255. The bitwise OR operation on two images is performed on the binary representation of these pixel values of corresponding images. Syntax Here is the syntax to perform bitwise OR operation on two images − cv2.bitwise_or(img1, img2, mask=None) img1 and img2 are the two input images and mask is a mask operation. Steps To compute bitwise OR between two images, you can use the steps given below ... Read More

How to create a watermark on an image using OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 09:08:18

1K+ Views

To add a watermark to an image, we will use the cv2.addWeighted() function from OpenCV. You can use the following steps to create a watermark on an input image − Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it. import cv2 Read the input image on which we are going to apply the watermark and read the watermark image. img = cv2.imread("panda.jpg") wm = cv2.imread("watermark.jpg") Access the height and width of the input image, and the height, width of the watermark image. h_img, w_img ... Read More

How to find the minimum enclosing circle of an object in OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 09:06:00

3K+ Views

A minimum enclosing circle (circumcircle) of an object is a circle which completely covers the object with minimum area. We can find the minimum enclosing circle of an object using the function cv2.minEnclosingCircle(). Syntax The syntax of this function is − (x, y), radius = cv2.minEnclosingCircle(cnt) Where, "cnt" are the contour points. It is represented as an array of contour points. Output − It returns coordinate of center (x, y) and radius of minimum enclosing circle. (x, y) and radius are of float dtype. So, to draw a circle on the image, we convert them to integers. To draw ... Read More

Advertisements