现在有一幅图像,我要通过hough变换,先检测直线,然后通过检测直线的连续性,分割出条码上的缺陷图像。效果如下:
但是有以下几个问题:
第一:为什么在语句[H,theta,rho]=hough(BW,'RhoResolution',0.1);改变"0.1“的数值,对直线检测没什么效果?
第二:P = houghpeaks(H,10,'threshold',ceil(0.1*max(H( )));改变“10”的大小检测效果也不理想,ceil(0.1*max(H( )也是如此。
第三:为什么通过hough变换后,最后检测的直线(红色线)只有断断续续的几条,而且都是一些小的短线,根本没有将所有条码的边缘线检测出来。
第四:算法需要怎样的改进,才能将“原图像”中黑色码道上的“白色小缺陷”分割出来?
谢谢!
附代码:
RGB=imread('X3.0.bmp');
figure,imshow(RGB,[]);
I=rgb2gray(RGB);
figure,imshow(I,[]);
[BW,t]=edge(I,'sobel','both');
% figure,imshow(BW);
% BW=double(BW);
% [H,theta,rho]=hough(BW);
[H,theta,rho]=hough(BW,'RhoResolution',0.1);
figure,imshow(H,[],'XData',theta,'YData',rho,...
'InitialMagnification','fit');
xlabel('\theta'),ylabel('\rho');
axis on,axis normal,hold on;
colormap(hot);
P = houghpeaks(H,10,'threshold',ceil(0.1*max(H( )));
x = theta(P(:,2)); y = rho(P(:,1));
plot(x,y,'linestyle','none',...
'marker','s','color','y');
% Find lines and plot them
lines = houghlines(BW,theta,rho,P,'FillGap',5,'MinLength',1);
figure,imshow(BW),hold on
for k = 1:length(lines)
xy = [lines(k).point1;lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','r');
end
![Matlab 霍夫变换检测直线来分割图像问题(附代码)]()
原图像.jpg
![Matlab 霍夫变换检测直线来分割图像问题(附代码)-1]()
hough变换图像.jpg
![Matlab 霍夫变换检测直线来分割图像问题(附代码)-2]()
直线检测图像.jpg |