MATLAB Answers

Segmenting rectangular features from various images

1 view (last 30 days)
Wookie
Wookie on 10 Sep 2020
I would like to segment the following image from the separation that happens in the middle. I am currently using Hough and it produces a line in the separation but it's not accurate enough to provide the edge right at the separation (see second image). Eventually I want it to work with various images like the third one below that has a different contrast and wider separation in the middle.
The code I am using is as follows:
[I1]=imread(['A_gray.PNG']);
imshow(I1)
B=edge( medfilt2(I1,[30,1]) );
B=bwareafilt(B,[30,inf]); % [50, Inf]
imshow(B)
B=imclose(B,ones(100,5));
imshow(B)
BW2_V = B;
[H_V,theta_V,rho_V] = hough(BW2_V);
figure
imshow(imadjust(rescale(H_V)),[],...
'XData',theta_V,...
'YData',rho_V,...
'InitialMagnification','fit');
xlabel('\theta (degrees)')
ylabel('\rho')
axis on
axis normal
hold on
colormap(gca,hot)
P_V = houghpeaks(H_V,5,'threshold',ceil(0.3*max(H_V(:))));
x_V = theta_V(P_V(:,2));
y_V = rho_V(P_V(:,1));
plot(x_V,y_V,'s','color','black');
lines_V = houghlines(BW2_H,theta_V,rho_V,P_V,'FillGap',500,'MinLength',100);
figure, imshow(I1), title('Line Detection Horizontal'), hold on
max_len_V = 0;
for k = 1:length(lines_V)
xy_v = [lines_V(k).point1; lines_V(k).point2];
plot(xy_v(:,1),xy_v(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy_v(1,1),xy_v(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy_v(2,1),xy_v(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len_V = norm(lines_V(k).point1 - lines_V(k).point2);
if ( len_V > max_len_V)
max_len_V = len_V;
xy_long_V = xy_v;
end
end
% highlight the longest line segment
plot(xy_long_V(:,1),xy_long_V(:,2),'LineWidth',2,'Color','red');

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!