This method essentially consists of understanding the characteristics of an image and its various classes and developing methods to classify the image class.
Note that the OpenCV method does not involve any training. For every contour, we define a bounding box using the OpenCV boundingRect property.
We will be using two important characteristics to select the bounding box:
- The size of the region of interest: We will eliminate all contours with a size that is less than 20.
Note that 20 is not a universal number, it just works for this image. For a larger image, the value can be larger.
- The color of the region of interest: Within each bounding box, we need to define the region of interest from 25% to 75% of the width to ensure that we do not consider the empty region of rectangles outside the circle. This is important to minimize variation. Next, we define the mean color by using CV2.mean.
We will determine the color's mean and max thresholds by looking at the three images of oranges that encircle it. The following code uses OpenCV's built-in method to draw the bounding box using cv2.boundingRect. It then draws a region of interest (ROI) based on the width and height selection and finds the mean color within the region:
count=0
font = cv2.FONT_HERSHEY_SIMPLEX
for c in contours:
x,y,w,h = cv2.boundingRect(c)
if (w >20 and h >20):
count = count+1
ROI = img[y+int(h/4):y+int(3*h/4), x+int(h/4):x+int(3*h/4)]
ROI_meancolor = cv2.mean(ROI)
print(count,ROI_meancolor)
if (ROI_meancolor[0] > 30 and ROI_meancolor[0] < 40 and ROI_meancolor[1] > 70 and ROI_meancolor[1] < 105
and ROI_meancolor[2] > 150 and ROI_meancolor[2] < 200):
cv2.putText(img, 'orange', (x-2, y-2), font, 0.8, (255,255,255), 2, cv2.LINE_AA)
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,255),3)
cv2.imshow('Contours', img)
else:
cv2.putText(img, 'apple', (x-2, y-2), font, 0.8, (0,0,255), 2, cv2.LINE_AA)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),3)
cv2.imshow('Contours', img)
In the preceding code, pay attention to the two if statements—size-based (w,h) and color-based (ROI_meancolor[0,1,2]):
- The size-based statement eliminates all contours of less than 20.
- ROI_meancolor [0,1,2] indicates the RGB value of the mean color.
Here, the third, fourth, and eighth lines represent the orange and the if statement constrains the color to look between 30 and 40 for the B component, 70 and 105 for the G component, and 150 and 200 for the R component.
The output is as follows. In our example, 3, 4, and 8 are oranges:
1 (52.949200000000005, 66.38640000000001, 136.2072, 0.0)
2 (43.677693761814744, 50.94659735349717, 128.70510396975425, 0.0)
3 (34.418282548476455, 93.26246537396122, 183.0893351800554, 0.0)
4 (32.792241946088104, 78.3931623931624, 158.78238001314926, 0.0)
5 (51.00493827160494, 55.09925925925926, 124.42765432098766, 0.0)
6 (66.8863771564545, 74.85960737656157, 165.39678762641284, 0.0)
7 (67.8125, 87.031875, 165.140625, 0.0)
8 (36.25, 100.72916666666666, 188.67746913580245, 0.0)
Note that OpenCV processes images as BGR not RGB.