I am trying to identify the potatoes in the following image and place boxes around them.
Ultimately, the box location would be a target location for a pick and place to pick up the potatoes.
After a lot of experimenting, cutting and pasting and generally learning a little about OpenCV I have this which I think is looks promising, but needs a lot more work.
import numpy as np
import cv2
image = cv2.imread("DSCN0901.JPG")
blurred_image = cv2.GaussianBlur(image, (55,55), 0)
gray = cv2.cvtColor(blurred_image, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
height, width = threshold.shape
min_x, min_y = width, height
max_x = max_y = 0;
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.005*cv2.arcLength(cnt, True), True)
(x,y,w,h) = cv2.boundingRect(approx)
min_x, max_x = min(x, min_x), max(x+w, max_x)
min_y, max_y = min(y, min_y), max(y+h, max_y)
rect = cv2.minAreaRect(approx)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image, [box],0,(0,0,255),2)
cv2.imshow("threshold", threshold)
cv2.imshow("blurred", blurred_image)
cv2.imshow("image",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Resulting in:
As you can see the boxes are catching some of the potatoes ok but get fooled by the brushes in some instances.
Any ideas on how I could more reliably identify the potatoes only?