Son Vo
Son Vo

Reputation: 31

find coordinate of object in a image after using grabcut (python)

I have lots of lobster images (with the same background).

My goal is to measure the size of each lobster.

To do this, I use Grabcut to extract the main object (lobster) from the background. However, after this step, I don't know how to find the coordinates of extracted object for cropping purpose. I need to crop the object to support size measuring.

I don't know whether:

If we don't have either of the above I'd appreciate it if you tell me about other methods to do object cropping from Grabcut output.

This is original image This is original image

This is the output image after Grabcut This is the output image after Grabcut.

The output image is still the same size as the original image.

I need to save the extracted object to a new image that fits enough the object's size.

Upvotes: 1

Views: 1443

Answers (1)

user3808268
user3808268

Reputation: 36

You need some thing called as min Bounding Rectangle from OpenCV Library. It forms a smallest rectangle and gives the coordinates of the rectangle.

Bounding Rect : It gives the un-rotated rectangle (green rectangle)

x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

Rotated Bounding Rect : It gives the rotated bounding rectangle (red rectangle)

rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img,[box],0,(0,0,255),2)

Image from OpenCV

After this you can take the coordinates and extract the rectangle with the lobster.

Upvotes: 1

Related Questions