Found 9168 Questions for Python

How to extract the foreground of an image using OpenCV Python?

Shahid Akhtar Khan
Updated on 02-Dec-2022 11:08:18
We apply the cv2.grabCut() method to extract the foreground in an image. For detailed approach please 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 image path. Define the variables: mask, bgdModel and fgdModel. Define the coordinates of a rectangle "rect" which includes the foreground object in the format (x, y, w, h). The correct coordinates are very important to extract the meaningful foreground. Apply grabCut() algorithm to extract the foreground of the input image. Pass mask, ... Read More

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

Shahid Akhtar Khan
Updated on 02-Dec-2022 11:06:22
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

Implementing Shi-Tomasi Corner Detector in OpenCV Python

Shahid Akhtar Khan
Updated on 02-Dec-2022 11:04:42
The Shi-Tomasi Corner Detector is an enhanced algorithm of the Harris Corner Detector. To implement the Shi-Tomasi corner detector, OpenCV provides us with the function, cv2.goodFeaturesToTrack(). It detects N strongest corners in the image. Steps To detect corners in an image using Shi-Tomasi corner detector, you could follow the steps given below − Import 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. Apply cv2.goodFeaturesToTrack() function on the grayscale image. Pass suitable ... Read More

Detecting corners using Harris corner detector in Python OpenCV

Shahid Akhtar Khan
Updated on 02-Dec-2022 11:02:32
In OpenCV, the Harris corner detector is implemented using the function cv2.cornerHarris(). It accepts four arguments: img, blockSize, ksize, and k. Where img is the input image in grayscale and of float32 dtype, blockSize is the size of neighborhood considered for corner detection, ksize is Aperture parameter of Sobel derivative used and k is Harris detector free parameter in the equation. Steps To detect corners in an image using Harris corner detector, you could follow the steps given below Import required libraries OpenCV and NumPy. Make sure you have already installed them. Read the input image using cv2.imread() method. ... Read More

How to compare histograms of two images using OpenCV Python?

Shahid Akhtar Khan
Updated on 02-Dec-2022 11:00:28
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
Updated on 02-Dec-2022 10:58:26
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 implement probabilistic Hough Transform in OpenCV Python?

Shahid Akhtar Khan
Updated on 02-Dec-2022 10:51:28
Probabilistic Hough Transform is an optimization of the Hough Transform. The hough transform takes a lot of computation even for a line with two arguments. Probabilistic Hough Transform doesn't take all the points into consideration, it takes only a random subset of points and that is sufficient for line detection. We could follow the below given steps to implement probabilistic Hough Transform on an image- 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
Updated on 02-Dec-2022 10:49:06
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

How to find the Fourier Transform of an image using OpenCV Python?

Shahid Akhtar Khan
Updated on 02-Dec-2022 10:47:25
The Discrete Fourier Transform (DFT) and Inverse Discrete Fourier Transform (IDFT) are applied on images to find the frequency domain. To find the Fourier transforms of an image we use the functions cv2.dft() and cv2.idft(). We can apply Fourier Transform to analyze the frequency characteristics of various filters. Steps To find Fourier transforms of an input image, 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. Load the input image as a grayscale image ... Read More

OpenCV Python – How to compute and plot the histogram of a region of an image?

Shahid Akhtar Khan
Updated on 02-Dec-2022 10:44:25
In OpenCV, We use the cv2.calcHist() function to compute the histogram of an image. We can use this function to compute the histogram of a region of the image. To compute a histogram of a region in the image first we define a mask. The white color in the maskis for regions to examine in the original input image and the black color in the mask image is for regions to ignore. Now we calculate the histogram passing this mask as a parameter to the function. Steps To compute and plot the histograms of a region of the image, one ... Read More
1 2 3 4 5 6 7 ... 917 Next
Advertisements