Found 184 Articles for OpenCV

Performing truncate thresholding on an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:43:20

662 Views

In this program, we will perform truncate thresholding on an image using openCV. Thresholding is a process in which the value of each pixel is changed in relation to a threshold value.The pixel is given a certain value if it is less than the threshold and some other value if it is greater than the threshold. In truncate thresholding, values greater than the threshold are reduced to the threshold value. Every other pixel remains the same.Original ImageAlgorithmStep 1: Import cv2. Step 2: Define threshold and max_val. Step 3: Pass these parameters in the cv2.threshold value and specify the type of ... Read More

Performing inverse binary thresholding on an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:44:08

1K+ Views

In this program, we will perform inverse binary thresholding on an image using openCV. Thresholding is a process in which the value of each pixel is changed in relation to a threshold value.The pixel is given a certain value if it is less than the threshold and some other value if it is greater than the threshold. In inverse binary thresholding, if the value of the pixel is less than the threshold, it will be given a maximum value i.e. white. If it is greater than the threshold, it will be assigned 0, i.e., black.Original ImageAlgorithmStep 1: Import cv2. Step ... Read More

Performing binary thresholding on an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:44:44

596 Views

In this program, we will perform binary thresholding on an image using openCV.Thresholding is a process in which the value of each pixel is changed in relation to a threshold value. The pixel is given a certain value if it is less than the threshold and some other value if it is greater than the threshold. In binary thresholding, if the value of the pixel is less than the threshold, it will be given a 0 value, i.e., black. If it is greater than the threshold, it will be assigned 255, i.e., white.Original ImageAlgorithmStep 1: Import cv2. Step 2: Define ... Read More

Downsampling an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:28:17

6K+ Views

In this program, we will down sample an image. Downsampling is decreasing the spatial resolution while keeping the 2D representation of an image. It is typically used for zooming out of an image. We will use the pyrdown() function in the openCV library to complete this task.Original ImageAlgorithmStep 1: Fead the image. Step 2: Pass the image as a parameter to the pyrdown() function. Step 3: Display the output.Example Codeimport cv2 image = cv2.imread('testimage.jpg') print("Size of image before pyrDown: ", image.shape) image = cv2.pyrDown(image) print("Size of image after pyrDown: ", image.shape) cv2.imshow('DownSample', image)OutputSize of image before pyrDown:  (350, ... Read More

Upsampling an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:27:02

3K+ Views

In this program, we will up sample an image. Up sampling is increasing the spatial resolution while keeping the 2D representation of an image. It is typically used for zooming in on a small region of an image. We will use the pyrup() function in the openCV library to complete this task.Original ImageAlgorithmStep 1: Read the image. Step 2: Pass the image as a parameter to the pyrup() function. Step 3: Display the output.Example Codeimport cv2 image = cv2.imread('testimage.jpg') print("Size of image before pyrUp: ", image.shape) image = cv2.pyrUp(image) print("Size of image after pyrUp: ", image.shape) cv2.imshow('UpSample', image)OutputSize ... Read More

Performing white BlackHat operation on images using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:26:42

272 Views

In this program, we will perform the Blackhat operation on an image using OpenCV. BlackHat transform is used to enhance dark objects of interest in a bright background. We will use the morphologyEx(image, cv2.MORPH_BLACKHAT, kernel) function.Original ImageAlgorithmStep 1: Import cv2. Step 2: Read the image. Step 3: Define the kernel size. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 5: Display the output.Example Codeimport cv2 image = cv2.imread('image_test.jpg') filter_size = (5,5) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filter_size) image = cv2.morphologyEx(image, cv2.MORPH_BLACKHAT, kernel) cv2.imshow('BlackHat', image)Output

Performing white TopHat operation on images using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:26:06

429 Views

In this program, we will perform the TopHat operation on images. TopHat operation is a morphological operation that is used to extract small elements and details from given images. TopHat is used to enhance bright objects in a dark background. We will use the morphologyEx(image, cv2.MORPH_TOPHAT, kernel) functionOriginal ImageAlgorithmStep 1: Import cv2. Step 2: Read the image. Step 3: Define the kernel size. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 5: Display the output.Example Codeimport cv2 image = cv2.imread('tophat.jpg') filter_size = (5, 5) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filter_size) image = cv2.morphologyEx(image, cv2.MORPH_TOPHAT, kernel) cv2.imshow('TopHat', image)OutputExplanationAs ... Read More

Performing a closing operation on an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:17:42

1K+ Views

In this program, we will perform the closing operation using the cv2.morphologyEx() function. Closing removes small holes in the foreground, changing small holes of background into foreground. This technique can also be used to find specific shapes in an image. The function we will use for this task is cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel).Original ImageAlgorithmStep 1: Import cv2 and numpy. Step 2: Read the image. Step 3: Define the kernel. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 4: Display the output.Example Codeimport cv2 import numpy as np image = cv2.imread('testimage.jpg') kernel = np.ones((5, 5), np.uint8) image = ... Read More

Performing an opening operation on an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:18:29

484 Views

In this program, we will perform the opening operation on image. Opening removes small objects from the foreground of an image, placing them in the background. This technique can also be used to find specific shapes in an image. Opening can be called erosion followed by dilation. The function we will use for this task is cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel).Original ImageAlgorithmStep 1: Import cv2 and numpy. Step 2: Read the image. Step 3: Define the kernel. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 4: Display the output.Example Codeimport cv2 import numpy as np image = ... Read More

Dilating images using the OpenCV function dilate()

Prasad Naik
Updated on 17-Mar-2021 08:18:49

332 Views

In this program, we will dilate an image using the dilate function in the OpenCV library. Dilation adds pixels to the boundaries of objects in an image, i.e., it expands the image on all sides.Original ImageAlgorithmStep 1: Import cv2 and numpy. Step 2: Read the image using opencv.imread(). Step 3: Define the kernel using np.ones() function. Step 4: Pass the image and kernel to the dilate() function. Step 5: Display the imageExample Codeimport cv2 import numpy as np image = cv2.imread('testimage.jpg') kernel = np.ones((3, 3), np.uint8) image = cv2.dilate(image, kernel) cv2.imshow('Dilated Image', image)OutputExplanationAs you can see, the image ... Read More

Advertisements