I am trying to detect objects using color. Below is the code and the image:
import cv2
import numpy as np
img = cv2.imread('image2.jpeg')
img1 = img[157:498, 212:705]
hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)
lower_bound = np.array([0, 20, 20])
upper_bound = np.array([20, 255, 255])
origMask = cv2.inRange(hsv, lower_bound, upper_bound)
kernel = np.ones((7, 7), np.uint8)
mask = cv2.morphologyEx(origMask, cv2.MORPH_CLOSE, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
cv2.imshow("Mask", mask)
cv2.imshow("Crop Image", img1)
cv2.imshow("Orig Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
So in the above code, I am loading the image first. Then cropping it to a desired area and then performing the HSV to find orange color objects.
Below is the original image:
Below is the cropped image:
Below is the mask image after hsv:
I want to know how can I count the number of objects in the mask image. For example, in this case it is 3. And after counting it, how can I draw bounding box over these color objects on the original image.



