Muhammad Ibtihaj Tahir
Muhammad Ibtihaj Tahir

Reputation: 636

Generate binary image of individual contours

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

  1. Convert to HSV
  2. Capture the H channel
  3. Generate Histogram
  4. Some processing (Not mentioning as irrelevant to the question)
  5. Find the largest blob in the binary image

I have done it using Contours and I have the largest contour which represents the field area.

enter image description here

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

Answers (2)

Muhammad Ibtihaj Tahir
Muhammad Ibtihaj Tahir

Reputation: 636

I have done it using the cv2.fillPoly(noiseless_mask, [largest_blob], 255) function. Where noiseless_mask = np.zeros_like(mask)

enter image description here

Upvotes: 1

Imtinan Azhar
Imtinan Azhar

Reputation: 1753

You may do this, first find the bounding rectangle for those contours,

enter image description here

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

Related Questions