Home > Net >  How to extract the white rings in the given image
How to extract the white rings in the given image

Time:08-27

I have an image of a robot moving, I need to extract the white rings

white rings

in order to find the midpoint of the robot. But thresholding is not giving correct result:

result

What method should I try to extract only the white rings.

%code to get second image
img=imread('data\Image13.jpg');
hsv=rgb2hsv(img);
bin=hsv(:,:,3)>0.8;

CodePudding user response:

Something like that?

import cv2
import numpy as np

# get bounding rectangles of contours
img = cv2.imread('img.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# filter contours by area and width
contours = [c for c in contours if (50 < cv2.contourArea(c) < 500) and cv2.boundingRect(c)[2] > 20]

# draw contours on empty mask
out = np.zeros(thresh.shape, dtype=np.uint8)
cv2.drawContours(out, contours, -1, 255, -1)

cv2.imwrite('out.png', out)

Output:

enter image description here

  • Related