OpenCV Articles

Page 4 of 11

How to find discrete cosine transform of an image using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 02-Dec-2022 7K+ Views

We apply cv2.dct() to find the discrete cosine transform of an image. This function transforms the grayscale image of dtype float32. It accepts two types of flag cv2.DCT_INVERSE or cv2.DCT_ROWS. To convert the transformed image to the original image we use cv2.idct(). Steps To find discrete cosine transform of an input image, you could follow the steps given below − Import the required libraries OpenCV and NumPy. Make sure you have already installed them. Read the input image using cv2.imread() method. Specify the full path of the image. Convert the input image to grayscale image using cv2.cvtColor() metod. Convert ...

Read More

How to compare histograms of two images using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 02-Dec-2022 6K+ Views

The histograms of two images can be compared using cv2.compareHist() function. The cv2.compareHist() function accepts three input arguments- hist1, hist2, and compare_method. The hist1 and hist2 are histograms of the two input images and compare_method is a metric to compute the matching between the histograms. It returns a numerical parameter that expresses how well two histograms match with each other. There are four metrics available to compare the histograms- Correlation, Chi-square, Intersection and Bhattacharyya distance. Steps To compare the histograms of two images one could follow the steps given below − Import the required libraries. In all the following ...

Read More

How to perform distance transformation on a given image in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 02-Dec-2022 1K+ Views

We can perform the distance transform using the method cv2.distanceTransform(). Following is the syntax of this method. Syntax cv2.distanceTransform(src, distanceType, maskSize) This method accepts the following parameters − src − 8-bit, single-channel (binary) source image. distanceType − Type of the distance. maskSize − Size of the distance transform mask. Steps To perform distance transform on the image, we could follow the below steps- Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you have already installed it. Read an input image using cv2.imread(). The RGB image read using ...

Read More

How to find the Fourier Transforms of Gaussian and Laplacian filters in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 02-Dec-2022 1K+ Views

We apply Fourier Transform to analyze the frequency characteristics of various filters. We can apply Fourier transform on the Gaussian and Laplacian filters using np.fft.fft2(). We use np.fft.fftshift() to shift the zero-frequency component to the center of the spectrum. Steps To find Fourier transforms of the Gaussian or Laplacian filters, one could follow the steps given below − Import the required libraries. In all below Python examples the required Python libraries are OpenCV, Numpy and Matplotlib. Make sure you have already installed them. Define a Gaussian or a Laplacian Filter. Apply Fourier transform on the above defined ...

Read More

OpenCV Python – How to find the shortest distance between a point in the image and a contour?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 02-Dec-2022 5K+ Views

We could compute the shortest distance between a point and a contour on the image using cv2.pointPolygonTest() passing the contour points coordinates and the point coordinate as arguments. Before applying cv2.pointPolygonTest() we need to compute the contours in the image. We could follow the below given steps to find shortest distance between a given point and a contour of an object in an image- Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it. Read an input image using cv2.imread(). The RGB image read using ...

Read More

How to flip an image in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 02-Dec-2022 18K+ Views

In OpenCV, an image can be flipped using the function cv2.flip(). Using this function we can flip the image across X-axis, Y-axis and across both axes. It accepts a flag flipCode as an argument to flip the image across the axis. If the flipCode is set to 0, the image is flipped across the x-axis and if the flipCode is set to a positive integer (say 1), the image is flipped across the Y-axis. If the flipCode is set to a negative integer (say "-1"), the image is flipped across both axes. Steps To flip an image, one could ...

Read More

How to access and modify pixel value in an image using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 02-Dec-2022 15K+ Views

To access a single pixel value in an image we can use indexing the same as we do to NumPy array indexing. We can use slicing to access a sequence of pixel values. To modify the pixel values we use the simple Python assignment operator ("="). Steps To access and modify pixel values in an image we could follow the below steps- Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you have already installed it. Read the input RGB image using cv2.imread(). The RGB image read using this method ...

Read More

How to split an image into different color channels in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 02-Dec-2022 9K+ Views

A color image consists of three color channels- Red, Green and Blue. These color channels can be split using cv2.split() function. Let's look at the steps to split an image into different color channels- Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you have already installed it. Read the input image using cv2.imread() method. Specify full path of image with the image type (i.e. png or jpg) Apply cv2.split() function on the input image img. It returns blue, green and red channel pixel values as numpy arrays. Assign ...

Read More

OpenCV Python – How to find and draw extreme points of an object on an image?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 02-Dec-2022 2K+ Views

To find and draw the extreme points of an object in the input image, we could follow the steps given below − The first step is to import required libraries. In all below Python examples the required Python library is OpenCV. Make sure you have already installed it. The next step is to read an input image using the cv2.imread() function. Specify the full image path with image types (.jpg or .png). Convert the input image to grayscale. Apply thresholding on the grayscale image to create a binary image. Adjust the second parameter to get a better contour ...

Read More

OpenCV Python – How to draw circles using Mouse Events?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 02-Dec-2022 3K+ Views

There are different types of muse events such as left or right button click, mouse move, left button double click etc. OpenCV provides us with different types of mouse events such as cv2.EVENT_LBUTTONDOWN for mouse left button down, cv2.EVENT_RBUTTONDOWN for right button down, cv2.EVENT_LBUTTONDBLCLK for left button double click and others. A mouse event returns the coordinates (x, y) of the mouse event. To perform an action when an event happens, we define a mouse callback function. We use mouse events to draw circles on the image. Steps To draw circles using mouse events, follow the steps given below ...

Read More
Showing 31–40 of 107 articles
« Prev 1 2 3 4 5 6 11 Next »
Advertisements