Ask Your Question
0

OpenCV Background Subtraction Get Color Objects (Python)

asked 2020-05-10 22:52:14 -0600

Syamim gravatar image

Background subtraction method (BackgroundSubtractorMOG2) will normally return the output in binary image.

Is there a solution on how I can get the original colour of the object after implementing the BackgroundSubtractorMOG2 ?

kernel_dil = np.ones((10,10), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
fgbg = cv2.createBackgroundSubtractorMOG2(history=0, varThreshold=444, detectShadows=False)

while True:
   ret, frame1 = cap.read()
   frame = cv2.resize(frame1,(1364,700),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)

   mask = np.zeros(frame.shape, dtype=np.uint8)
   mask.fill(255)

   roi_corners = np.array([[(11,652), (1353,652), (940,84), (424,84)]], dtype=np.int32)
   mask = cv2.fillPoly(mask, roi_corners, 0)

   masking = cv2.bitwise_or(frame, mask)

   if ret == True:
        fgmask = fgbg.apply(masking,mask)
        fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
        dilation = cv2.dilate(fgmask, kernel_dil, iterations = 1)
       (contours,hierarchy) = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        for pic, contour in enumerate(contours):

           area = cv2.contourArea(contour)
           x,y,w,h = cv2.boundingRect(contour)

           if(area>0):
              cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 2)
              cv2.putText(frame, 'People', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,0,0), 2, cv2.LINE_AA)

This is my current output : Mask

Field

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-05-11 07:00:08 -0600

kbarni gravatar image

updated 2020-05-11 07:00:45 -0600

You are almost there. Use the rectangle to cut the part of the image as the player.

x,y,w,h = cv2.boundingRect(contour)
player = frame[x:x+w,y:y+h] # the sub-image containing the player
meancolor = player.mean(2) # get the mean R,G,B for the ROI, I think that's what you want
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 2) # no need to check if area>0, this should be always the case
cv2.putText(frame, 'Player '+str(meancolor[0]+' '+str(meancolor[1]+' '+str(meancolor[2]+' ', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,0,0), 2, cv2.LINE_AA)

This will display the mean color of the rectangle above the player. If you want to identify the team based on the shirt color, it will be probably more complicated, but you are on the right track.

edit flag offensive delete link more

Comments

Hi, your method is not what I expected, but the one I need. I never thought to use the mean color for each of the player. But this " cv2.putText(frame, 'Player '+str(meancolor[0]+' '+str(meancolor[1]+' '+str(meancolor[2]+' ', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,0,0), 2, cv2.LINE_AA) " line show me the invalid syntax error. Do you know where its wrong ?

Syamim gravatar imageSyamim ( 2020-07-13 00:07:21 -0600 )edit

Check the docs for the syntax. Debug the code. See what's not working.

Generally I don't test the code, I don't have time for that. What's important is that you understand the idea.

kbarni gravatar imagekbarni ( 2020-07-13 08:05:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-05-10 22:52:14 -0600

Seen: 1,336 times

Last updated: May 11 '20