Shahid Akhtar Khan

Shahid Akhtar Khan

169 Articles Published

Articles by Shahid Akhtar Khan

Page 3 of 17

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 7K+ Views

The discrete cosine transform (DCT) is a mathematical technique used in image processing for frequency domain analysis and compression. OpenCV provides cv2.dct() to compute DCT of an image and cv2.idct() to apply inverse DCT. Syntax cv2.dct(src, flags) cv2.idct(src, flags) Parameters src − Input image as float32 array flags − Transformation flags (cv2.DCT_INVERSE or cv2.DCT_ROWS) Steps to Apply DCT To find discrete cosine transform of an input image, follow these steps − Import the required libraries OpenCV and NumPy Read the input image using cv2.imread() and convert to grayscale ...

Read More

How to compare histograms of two images using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 6K+ Views

The histograms of two images can be compared using cv2.compareHist() function. This function accepts three input arguments: hist1, hist2, and compare_method. The hist1 and hist2 are histograms of the two input images, while 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. Comparison Methods OpenCV provides four different histogram comparison methods: Method Description Perfect Match No Match HISTCMP_CORREL Correlation 1 0 HISTCMP_CHISQR Chi-Square 0 ∞ HISTCMP_INTERSECT Intersection Higher values = better match ...

Read More

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 1K+ Views

Distance transformation is a morphological operation that calculates the distance from every pixel of the foreground to the nearest pixel of the background in a binary image. OpenCV provides the cv2.distanceTransform() method to perform this operation. Syntax cv2.distanceTransform(src, distanceType, maskSize) Parameters This method accepts the following parameters ? src − 8-bit, single-channel (binary) source image. distanceType − Type of distance calculation (cv2.DIST_L1, cv2.DIST_L2, cv2.DIST_C). maskSize − Size of the distance transform mask (3, 5, or cv2.DIST_MASK_PRECISE). Steps to Perform Distance Transform Import required libraries (OpenCV, NumPy, Matplotlib) Read ...

Read More

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 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, you 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 ...

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 26-Mar-2026 5K+ Views

In OpenCV, we can compute the shortest distance between a point and a contour using cv2.pointPolygonTest(). This function takes contour coordinates and a point coordinate as arguments and returns the shortest Euclidean distance between them. Syntax distance = cv2.pointPolygonTest(contour, point, measureDist) Parameters contour − The input contour point − The point coordinates as a tuple (x, y) measureDist − If True, returns actual distance. If False, returns +1, 0, or -1 Return Value Positive − Point is inside the contour Negative − Point is outside the contour Zero − ...

Read More

How to normalize an image in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 55K+ Views

Image normalization in OpenCV rescales pixel values to a specific range, improving image processing and machine learning model performance. The cv2.normalize() function provides various normalization techniques to transform pixel intensities. Syntax The cv2.normalize() function accepts the following parameters ? cv2.normalize(src, dst, alpha, beta, norm_type, dtype, mask) Parameters src − Input image array dst − Output array of the same size as src alpha − Lower norm value for range normalization beta − Upper norm value for range normalization norm_type − Normalization type (NORM_MINMAX, NORM_L2, etc.) dtype − Data type of output array ...

Read More

How to mask an image in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 53K+ Views

We can apply a mask to an image by computing the cv2.bitwise_and() between the mask and the image. To track a color, we define a mask in HSV color space using cv2.inRange() passing lower and upper limits of color values in HSV. To track a part of the image we can define a mask using np.zeros() and slicing the entries with white (255) for the region in the input image to examine. Follow the given steps to mask an image − Import required libraries − OpenCV and NumPy Read the input image using cv2.imread() method. Convert the ...

Read More

How to flip an image in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 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. Syntax cv2.flip(image, flipCode) ...

Read More

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 15K+ Views

In OpenCV, you can access and modify pixel values in images using NumPy-style indexing. Since OpenCV images are stored as NumPy arrays, you can use array indexing to read pixel values and assignment operations to modify them. Understanding Image Structure OpenCV loads color images in BGR (Blue, Green, Red) format. Each pixel contains three values representing the intensity of each color channel (0-255). Accessing a Single Pixel Value To access a pixel at coordinates (row, column), use array indexing ? import cv2 import numpy as np # Create a sample image for demonstration ...

Read More

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 9K+ Views

A color image consists of three color channels − Red, Green, and Blue. These color channels can be split using cv2.split() function in OpenCV Python. This technique is useful for analyzing individual color components or applying channel-specific processing. Syntax blue, green, red = cv2.split(image) The cv2.split() function takes a BGR image as input and returns three separate arrays representing the Blue, Green, and Red channels respectively. Steps to Split Color Channels Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you have already installed ...

Read More
Showing 21–30 of 169 articles
« Prev 1 2 3 4 5 17 Next »
Advertisements