细化过程中不需要的分支 [英] Unwanted branches in thinning process
问题描述
我正在尝试编码以稀疏图像,并且在一定程度上我得到了输出。但最终的输出仍然会产生一些来自不同领域的不必要的小线。我的下一步是寻找交叉点。由于这些小线,我得到的点实际上不是交点。如何改进我的代码以避免这些行。
I was trying to code for thinning an image and to an extent I got an output. But still the final output does have some small unwanted lines emerging from different areas. My next step was to find intersection points. Because of these small lines, I get points which are actually not intersection points. How can I improve my code in order to avoid those lines.
I = imread('img.jpg');
I = rgb2gray(I);
I = uint8(255*mat2gray(I));
I=imresize(I,[128 128]);
I1=edge(I,'canny',0.6);
I2=edge(I,'canny',0.1);
I = imsubtract(I2,I1);
si = imdilate(I,strel('line',3,0));
se = imerode(I,strel('line',3,0));
I = imsubtract(si,se);
si = imdilate(I,strel('disk',1));
se = imerode(I,strel('disk',3));
I = imsubtract(si,se);
I = imfill(I,'holes');
[L num]=bwlabel(I); %%number of connected objects found in binary%%%
A=[];
for i=1:num
a=find(L==i);
A(i)=size(a,1);
end
[b indxA]=max(A);
L2(128,128)=0;
for i=1:num
if A(i)>=0.9*b
L2(find(L==i))=1;
end
end
I = imerode(L2,strel('disk',1));
I = bwmorph(I,'skel',Inf);
[i,j] = ind2sub(size(I),find(bwmorph(bwmorph(I,'thin',Inf),'branchpoint') == 1));
figure,imshow(I); hold on; plot(j,i,'rx');
输入:
输出:
必需img:
推荐答案
我的方法有三个关键步骤
My approach has three key steps
- 识别线之间的交叉点。
bwmorph
有一个branchpoint
选项,但它不够激进。我使用conv2
来计算相邻像素 - 使用
bwconncomp $ c $将像素分组为线段c>
- 使用阈值删除小线段,同时检查图像是否保持连接状态。
- Identify intersections between lines.
bwmorph
has abranchpoint
option, but it wasn't aggressive enough. I usedconv2
to count neighboring pixels instead - Group pixels into line segments using
bwconncomp
- Use threshold to remove small line segments while checking that the image remains connected.
我使用了一个10像素的简单阈值。如果需要自适应阈值,可以使用相对于中值或平均线段长度的度量。选择阈值很大程度上取决于您在数据集中的变化程度。如果变化太多,您可能需要为每张图片进行人工互动。
I've used a simple threshold of 10 pixels. If you want an adaptive threshold, you could use a metric relative to the length of the median or mean line segment. Choosing the threshold depends greatly on how much variation you have in your dataset. If there is too much variation, you might need human interaction for each image.
%Start off with your code above then do the following
%I got a better starting image with the 'thin' option than the 'skel' option
I = bwmorph(I,'thin',Inf);
%Alternative splitting method to 'branchpoint'
%Use convolution to identify points with more than 2 neighboring pixels
filter = [1 1 1;
1 0 1;
1 1 1];
I_disconnect = I & ~(I & conv2(double(I), filter, 'same')>2);
cc = bwconncomp(I_disconnect);
numPixels = cellfun(@numel,cc.PixelIdxList);
[sorted_px, ind] = sort(numPixels);
%Remove components shorter than threshold
threshold = 10;
for ii=ind(sorted_px<threshold)
cur_comp = cc.PixelIdxList{ii};
I(cur_comp) = 0;
%Before removing component, check whether image is still connected
full_cc = bwconncomp(I);
if full_cc.NumObjects>1
I(cur_comp) = 1;
end
end
%Clean up left over spurs
I = bwmoph(I, 'spur');
figure; imshow(I);
输出图像与您想要的输出非常相似
The output image is very similar to your desired output
这篇关于细化过程中不需要的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!