I want to extract closest object to the camera from image in Python OpenCV. I have two images of the same object but second one is just a little bit rotated. Now what I want is to create a binary mask based on my disparity map to extract my object (ball without the yellow rectangle behind it).
So after reading my two input images (A) and (B) in the photo) I use:
stereo = cv.StereoSGBM_create(numDisparities=16,blockSize=5)
disparity = stereo.compute(imgL, imgR)
and for some reason I get result like (C). I mean it clearly recognizes the ball in the front but for some reason I get that white part across whole image, which dont make sense to me. So problem is that because of disparity map like this when i use the code for closing contours to get binary mask containing white only where the ball is:
se = np.ones((2,2), dtype='uint8')
image_close = cv.morphologyEx(disparity, cv.MORPH_CLOSE, se)
plt.imshow(image_close,'gray')
plt.show()
cv.waitKey(0)
thresh_image = image_close.astype(np.uint8)
cnt = cv.findContours(thresh_image, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)[0]
mask = np.zeros(disparity.shape[:2], np.uint8)
cv.drawContours(mask, cnt, -1, 255, -1)
I get result like in (D). So the final result of extracting closest object (ball) is in (E), which is not even close to what I need. Can any1 explain to me what I am doing wrong here? Or is there any better method to extract closest object? Yellow rectangle behind the ball is there for a reason. To test if program will recognize only the ball as closest object.