aizuon
aizuon

Reputation: 23

Obtaining object contour inside bounding box

Input image

I would like to detect the outline contour of the meat. I have trained a object detection model for meats. The output of the model can be seen below and the bounding box coordinates are (44,34) for top left and (321,348) for bottom right corners.

Detected meat

How can i segmentate the meat's contour based on this output via opencv or other image processing libraries?

Upvotes: 2

Views: 1093

Answers (2)

user1196549
user1196549

Reputation:

The problem that you face is due to the shadow in the top part, which makes the meat as dark as the background at places.

You can try to binarize the red component of the image, then remove the holes with a morphological closing, though this damages the outline a little.

enter image description here

Upvotes: 0

JoOkuma
JoOkuma

Reputation: 492

You can use the Grabcut [1] algorithm to extract a segment from a bounding-box.

Grabcut tries to cluster the pixels into two groups, outside of the bounding-box and inside, while penalizing label disagreement with adjacent pixels with a similar color.

For example:

import cv2 
import numpy as np

im = cv2.imread('beef.jpg')
mask = np.zeros(im.shape[:2], np.uint8)

bgd_model = np.zeros((1, 65), np.float64)
fgd_model = np.zeros((1, 65), np.float64)

rect = (30, 25, 30 + 318, 25 + 350)  # (x, y, w, h)
cv2.grabCut(im, mask, rect, bgd_model, fgd_model, 10, cv2.GC_INIT_WITH_RECT)                      

mask = np.where((mask == cv2.GC_BGD) | (mask == cv2.GC_PR_BGD), 0, 1).astype(np.uint8)

contour, _= cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contour, -1, (0, 255, 0)) 

cv2.imwrite("output.png", im) 

output.png

[1] Rother, Carsten, Vladimir Kolmogorov, and Andrew Blake. " GrabCut" interactive foreground extraction using iterated graph cuts." ACM Transactions on Graphics (TOG) 23.3 (2004): 309-314.

Upvotes: 3

Related Questions