Ratio_green = cv2.countNonZero(mask)/(img.size/3) # percentage of green pixels in the original image: # fall in your defined range, that is, every white pixel corresponds # Remember that the white pixels in the mask are those that # You can use the mask to count the number of white pixels. # survive the AND operation, all the black pixels # All the pixels that are white in the mask will # Now, you AND the mask and the input image # be rendered in black, for all three channels: # All the pixels that do not fall inside this interval will # All the pixels that fall inside your interval will be white # cv2.inRange is used to binarize (i.e., render in white/black) an image # You get the lower and upper part of the interval: Img = cv2.resize(img, newSize, None, None, None, cv2.INTER_AREA) # that's why the color values have been adjusted here:īoundaries =, green-diff, green-diff], # Be aware that opencv loads image in BGR format, # in the tuple and are below and above them by 20 # You define an interval that covers the values I've added some comments to explain the code: # Imports I've modified your script so you can find the (approximate) percent of green color in your test images. Print('brown pixel percentage:', np.round(ratio_brown*100, 2))Ĭv2.imshow("images", np.hstack()) Ratio_brown = cv2.countNonZero(mask)/(img.size/3) Output = cv2.bitwise_and(img, img, mask=mask) How should I change this code so that it is capable of calculating the percentage of green instead of brown? And if it isn't too troublesome could someone please explain how the changes affect the code? Below is the link to the image I would like to use.Ĭredit for the code goes to import numpy as npīoundaries = -diff, brown-diff, brown-diff], I have a little experience with OpenCV but am still pretty new to image processing and would like some help with my code. I'm creating a code which can detect the percentage of green colour from an image.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |