matlab 如何提取给定图像中的白环

wd2eg0qa  于 2022-11-15  发布在  Matlab
关注(0)|答案(1)|浏览(183)

我有一个机器人在移动的图像,我需要提取白色的环

才能找到机器人的中点。但是阈值并没有给出正确的结果:

我应该尝试什么方法才能只提取白色的环。

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

sdnqo3pr1#

差不多吧?

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)

产出:

相关问题