Found 10476 Articles for Python

OpenCV Python – How to convert a colored image to a binary image?

Shahid Akhtar Khan
Updated on 02-Dec-2022 10:39:58

26K+ Views

We use cv2.threshold() to convert a grayscale image to binary image. To convert a color image to binary image, we first convert the color image to grayscale image using cv2.cvtColor() then apply cv2.threshold() on the grayscale image. Steps One could follow the below given steps to convert a color image to a binary 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 the input image using cv2.imread(). The RGB image read using this method is in BGR format. Optionally assign the read ... Read More

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

Shahid Akhtar Khan
Updated on 02-Dec-2022 10:37:06

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

OpenCV Python – How to perform bitwise NOT operation on an image?

Shahid Akhtar Khan
Updated on 02-Dec-2022 10:34:43

2K+ Views

We can perform bitwise NOT operation on an image using cv2.bitwise_not(). Here is the syntax to perform bitwise NOT operation on an image - cv2.bitwise_not(img) Steps To compute bitwise NOT on an image, you can follow the steps given below − 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 as a grayscale image using cv2.imread() method. Specify the full path of the image with the image type (i.e. png or jpg). Compute the bitwise NOT on the input image using ... Read More

How to normalize an image in OpenCV Python?

Shahid Akhtar Khan
Updated on 26-Aug-2023 03:42:32

49K+ Views

We use the function cv2.normalize() to normalize an image in OpenCV. This function accepts the parameters- src, dst, alpha, beta, norm_type, dtype and mask. src and dst are input image and output of the same size as input, alpha is lower norm value for range normalization, beta is upper norm value for range normalization, norm_type is normalization type, dtype is data type of output and mask is optional operation mask. Steps To normalize an image, we could follow the steps given below − Import the required library. In all the following examples, the required Python library is OpenCV. ... Read More

OpenCV Python – How to draw curves using Mouse Events?

Shahid Akhtar Khan
Updated on 02-Dec-2022 07:22:31

2K+ 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_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 cv2.EVENT_LBUTTONDOWN cv2.EVENT_MOUSEMOVE and cv2.EVENT_LBUTTONUP mouse events to draw curves on the image. Steps To draw curves using mouse events, follow the steps given below − ... Read More

OpenCV Python – How to draw a rectangle using Mouse Events?

Shahid Akhtar Khan
Updated on 02-Dec-2022 07:16:05

7K+ Views

There are different types of muse events such as left or right button click, mouse move, left button double click etc. 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 cv2.EVENT_LBUTTONDOWN, cv2.EVENT_MOUSEMOVE and cv2.EVENT_LBUTTONUP mouse events to draw rectangles on the image. Steps To draw rectangles using mouse events, follow the steps given below − Import required library OpenCV. Make sure you have already installed it. Create a black image. We draw the rectangles on this black image. We ... Read More

How to mask an image in OpenCV Python?

Shahid Akhtar Khan
Updated on 25-Aug-2023 01:20:28

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.Also Read: Color Identification in Images using Python and OpenCV 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 − The first step is to import required libraries. The ... Read More

How to flip an image in OpenCV Python?

Shahid Akhtar Khan
Updated on 02-Dec-2022 07:12:23

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
Updated on 02-Dec-2022 07:10:33

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
Updated on 02-Dec-2022 07:08:26

8K+ 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

Advertisements