vortiholdings.blogg.se

Easy contour python
Easy contour python







easy contour python

# draw all contours with setting the parameter to -1 #make a copy of the resized image since we are going to draw contours on the resized image

EASY CONTOUR PYTHON CODE

The following code lines are used to detect contours and display them.Ĭontours, hierarchy = cv2.findContours(thresholdImage,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) Figure 6: Histograms of the grayscale and thresholded images (thresholding is performed by using Otsu’s method). The histograms produced by Otsu’s method are shown below (compare these results with Fig. This function now returns (as the first output) an estimate of the threshold value that is computed by Otsu’s algorithm. We last argument “cv2.THRESH_BINARY|cv2.THRESH_OTSU” tells to the function cv2.threshold() to use the Otsu’s method. 5 small white areas are almost completely filtered. Figure 5: Image that is thresholded by using the Otsu’s method.īy comparing Figures 3 and 5, we can observe that in Fig. #estimatedThreshold, thresholdImage=cv2.threshold(grayImage,90,255,cv2.THRESH_BINARY) The following code lines are used to perform Otsu’s automatic thresholding.ĮstimatedThreshold, thresholdImage=cv2.threshold(grayImage,50,255,cv2.THRESH_BINARY|cv2.THRESH_OTSU) We can use Otsu’s method that automatically selects the threshold parameter. That is, instead of allowing the user to select the threshold parameter, an algorithm can determine the threshold value. Instead of guessing this parameter, we can also try another threshold method. The second argument (threshold value) of the function “cv2.threshold()” has to be properly tuned since its value has a significant influence on the performance of the contour detection algorithm. 4, we can observe the histograms of the grayscale and thresholded image. Figure 4: Histograms of the grayscale and thresholded images.įrom Fig. The results are shown in the figure below. Plt.subplot(2,2,4), plt.hist(thresholdImage.ravel(),256), plt.title('Color Histogram of Binary (Thresholded) Image') Plt.subplot(2,2,3), plt.imshow(thresholdImage,'gray'), plt.title('Binary (Thresholded) Image') Plt.subplot(2,2,2), plt.hist(grayImage.ravel(), 256), plt.title('Color Histogram of Grayscale Image') Plt.subplot(2,2,1), plt.imshow(grayImage,'gray'), plt.title('Grayscale Image') # PLOT HISTOGRAM OF THRESHOLDED AND GRAYSCALE IMAGES The following code lines are used to plot the histogram of the grayscale and thresholded image.

easy contour python easy contour python

Figure 3: Image after applying the threshold function. The thresholded image is shown in the figure below. For details about the function “cv2.threshold”, see the official OpenCV tutorial page that can be accessed here. The first argument is the used threshold value, and the second argument is the resulting image. Here it should be noted that image pixel values that are smaller than the threshold value become equal to 0 after the threshold operation. In our case, this flag is “cv2.THRESH_BINARY”, indicating that we want to use binary thresholding. The fourth argument is the threshold flag. The third argument is the value that is assigned to an image pixel that exceeds the threshold (in our case 255). The second value is the threshold value (in our case 90). The function “cv2.threshold()” takes four arguments. #estimatedThreshold, thresholdImage=cv2.threshold(grayImage,50,255,cv2.THRESH_BINARY|cv2.THRESH_OTSU)ĮstimatedThreshold, thresholdImage=cv2.threshold(grayImage,90,255,cv2.THRESH_BINARY)Ĭv2.imwrite("resizedPartsThreshold.png", thresholdImage, )

easy contour python

The following code lines are used to convert the image to grayscale. Note that OpenCV loads color images in the permuted format, that is, in the BGR form (Blue-Green-Red form). The first step in detecting the contours is to convert the RGB (Red-Green-Blue) image to grayscale. The name of the resized image is “resizedParts.png”. This code will create and save the reduced-size image. If you are having problems understanding this code, see our previous post that explains these steps. ResizedImage = cv2.resize(image, newDimension, interpolation = cv2.INTER_AREA)Ĭv2.imwrite("resizedParts.png", resizedImage, ) NewHeight = int(image.shape * scale / 100) NewWidth = int(image.shape * scale / 100) # keep in mind that open CV loads images as BGR not RGB









Easy contour python