
Easy contour python how to#
RELATED: How to Detect Shapes in Images using OpenCV in Python For instance, for a pancakes image, I've decreased the threshold to 127, here is the result:Īlright, this is it for this tutorial, if you want to test this on your live camera, head to this link. To achieve good results on different and real-world images, you need to tune your threshold value or perform edge detection.
Easy contour python code#
The above code finds contours within the binary image and draws them with a thick green line to the image, let's show it: # show the image with the drawn contours Now, this is easy for OpenCV to detect contours: # find the contours from the thresholded imageĬontours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) The above code creates the binary image by disabling (setting to 0) pixels that have a value of less than 225 and turning on (setting to 255) the pixels that has a value of more than 225, here is the output image: _, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV) This is a necessity in OpenCV, finding contours is like finding a white object from a black background, objects to be found should be white and the background should be black. Gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)Īs mentioned earlier in this tutorial, we gonna need to create a binary image, which means each pixel of the image is either black or white. Image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

We gonna use this image for this tutorial:Ĭonverting it to RGB and then grayscale: # convert to RGB Importing the necessary modules: import cv2
Easy contour python install#
First, let's install the dependencies for this tutorial: pip3 install matplotlib opencv-python Related: How to Apply HOG Feature Extraction in Python.Īlright, let's get started. Draw these contours and show the image.Finding the contours using findContours() OpenCV function.Convert the image to a binary image, it is a common practice for the input image to be a binary image (which should be a result of a thresholded image or edge detection).As a result, we can manipulate contours in our programs such as counting the number of contours, using them to categorize the shapes of objects, cropping objects from an image (image segmentation), and much more.Ĭontour detection is not the only algorithm for image segmentation though, there are a lot of others, such as the current state-of-the-art semantic segmentation, hough transform, and K-Means segmentation.įor better accuracy, here is the whole pipeline that we gonna follow to successfully detect contours in an image: However, contours are abstract collections of points and segments corresponding to the shapes of the objects in the image. Well, when we perform edge detection, we find the points where the intensity of colors changes significantly, and then we simply turn those pixels on. In a previous tutorial, we have discussed edge detection using the Canny algorithm and we've seen how to implement it in OpenCV, you may ask, what's the difference between edge detection and contour detection?

Contour detection is a useful technique for shape analysis and object detection and recognition. Both functions need three parameters x,y and z.įig.Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.Ī contour is a closed curve joining all the continuous points having some color or intensity, they represent the shapes of objects found in an image. Matplotlib API contains contour() and contourf() functions that draw contour lines and filled contours, respectively. The shgrid creates a rectangular grid out of an array of x values and an array of y values. The independent variables x and y are usually restricted to a regular grid called meshgrid. A contour line or isoline of a function of two variables is a curve along which the function has a constant value. These contours are sometimes called the z-slices or the iso-response values.Ī contour plot is appropriate if you want to see how alue Z changes as a function of two inputs X and Y, such that Z = f(X,Y). It graphs two predictor variables X Y on the y-axis and a response variable Z as contours. Contour plots (sometimes called Level Plots) are a way to show a three-dimensional surface on a two-dimensional plane.
