我有input image。我想在对象周围创建边界,并将边界像素标记为灰色,即128强度值。我需要this类型的输出图像。我追踪了物体的边界:
level = graythresh(im);
BW = im2bw(im,level);
figure; imshow(BW);
[B,L] = bwboundaries(BW,'noholes');
imshow(im);
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'Color', [0.5 0.5 0.5], 'LineWidth', 5)
end但它只会在图像上绘制边界。我想在输入图像的对象周围创建这个边界。我该怎么做呢?我希望我的问题是清楚的。
发布于 2018-01-19 19:07:23
虽然我不知道你为什么要这样做,但有一句忠告:你可能不想这样做。这样做会丢失图像的信息。对于您的应用程序来说,知道边界的索引就足够了,而且您确实已经知道了它们。
然而,如果你真的想这样做,你需要做的就是:
clear;
clc;
im=imread('https://i.stack.imgur.com/r7uQz.jpg');
im=im(31:677,90:1118);
level = graythresh(im);
BW = im2bw(im,level);
[B,L] = bwboundaries(BW,'noholes');
for k = 1:length(B)
boundary = B{k};
%%%%%%%%% Fill the boundary locations with he desired value.
for ii=1:size(boundary,1)
im(boundary(ii,1), boundary(ii,2))=128;
end
end如果你想要图像中的“胖”线,在后面添加这个:
im128=im==128;
bigboundary=imdilate(im128,ones(5));
im(bigboundary)=128;

发布于 2018-01-19 14:35:37
您可以对图像进行二值化,并使用OpenCV检测轮廓。下面是Python的实现:
img = cv2.imread(r'D:\Image\temp1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,bw = cv2.threshold(gray,100,255,cv2.THRESH_BINARY)
_,contours,hierarchy = cv2.findContours(bw, cv2.RETR_CCOMP, 1)
for cnt in contours:
c = np.random.randint(0,256,3)
c = (int(c[0]), int(c[1]), int(c[2]))
cv2.drawContours(img,[cnt],0,c,2)输出图像:

https://stackoverflow.com/questions/48335154
复制相似问题