Reputation: 636
I'm trying to segment the football field. I have found the largest contour that represents the field area. I need to generate a binary image using that area.
I'm Following a research paper and followed all the steps including
I have done it using Contours and I have the largest contour which represents the field area.
I need to use this specific contour to generate a new binary image which will contain only the area of this contour.
# Find Largest Blob
# mask is the processed binary image
# using that mask I find the contours and draw them on original
#image
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
largest_blob = max(contours, key=cv2.contourArea)
cv2.drawContours(image, largest_blob, -1, (0, 0, 255), 2)
Upvotes: 0
Views: 822
Reputation: 636
I have done it using the cv2.fillPoly(noiseless_mask, [largest_blob], 255)
function. Where noiseless_mask = np.zeros_like(mask)
Upvotes: 1
Reputation: 1753
You may do this, first find the bounding rectangle for those contours,
The bounding rectangle is indicated by the green box
x,y,w,h = cv2.boundingRect(cnt)
Then use these to crop the image
field = img[y:y+h, x:x+w, :]
Then you may apply binarization to the field
object, more info can be found here
https://docs.opencv.org/3.1.0/dd/d49/tutorial_py_contour_features.html
Upvotes: 0