How do I cropped the image once I have 2 horizontal lines detected?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jun Han Tan
el 7 de Oct. de 2021
Comentada: Jun Han Tan
el 9 de Oct. de 2021
Hi, i have a image of a rail surface which i want to crop away the outsides of the images and remain only the surface of the rail. I have detected the 2 horizontal lines using hough transformation but i don't know how to cropped them. Please see the codes and images attached. Please help and thanks in advance.
clc;
close all;
%Prompt to select rail frame
[baseFileName, folder] = uigetfile({'*'});
fullFileName = fullfile(folder, baseFileName);
%Reading of image
Img=imread(fullFileName);
%Convert image to grayscale
grayscale=rgb2gray(Img);
%Sobel Edge detection & filter weak edges.
DetectedEdges = edge(grayscale,'Sobel','horizontal');
FilteredImage = bwareaopen(DetectedEdges,60);
%Hough Transformation with Peak & Lines identification.
[H,theta,rho] = hough(DetectedEdges);
Peaks = houghpeaks(H,5,'threshold',ceil(0.5*max(H(:))));
Lines = houghlines(DetectedEdges,theta,rho,Peaks,'FillGap',10,'MinLength',150);
%Display Filtered Weak Edges & Draw Detected Lines on Grayscaled Frame.
figure('Name','Filtered Weak Edges & Lines Detection','NumberTitle','off');
subplot(2,1,1), imshow(FilteredImage);
subplot(2,1,2), imshow(grayscale), hold on
%Identify Frame dimensions & line coordinates in both upper & lower rail edge.
[size_x,size_y] = size(grayscale);
min_y = size_y;
max_y = 0;
%Set condition to determine upper & lower edge.
for k = 1:length(Lines)
xy = [Lines(k).point1;Lines(k).point2];
if xy(1,2) <= min_y
min_y = xy(1,2);
min_y_line = xy;
end
if xy(1,2) >= max_y
max_y = xy(1,2);
max_y_line = xy;
end
end
%Use straight line formula to determine y-intercepts & gradients.
min_y_gradient = (min_y_line(2,2) - min_y_line(1,2)) / (min_y_line(1,2) - min_y_line(1,1));
min_y_intercept = min_y_line(1,2) - min_y_gradient*min_y_line(1,1);
plot(min_y_line(:,1),min_y_line(:,2),'LineWidth',2,'Color','green');
max_y_gradient = (max_y_line(2,2) - max_y_line(1,2)) / (max_y_line(1,2) - max_y_line(1,1));
max_y_intercept = max_y_line(1,2) - max_y_gradient*max_y_line(1,1);
plot(max_y_line(:,1),max_y_line(:,2),'LineWidth',2,'Color','red');
hold off
%Note that 'hold on' in Line 45 retains plots in the current axes.
0 comentarios
Respuesta aceptada
Image Analyst
el 7 de Oct. de 2021
Try indexing/masking:
topRow = min(min_y_line(:,2))
bottomRow = max(max_y_line(:,2))
croppedImage = grayscale(topRow:bottomRow, :);
Más respuestas (1)
yanqi liu
el 8 de Oct. de 2021
sir,please check the follow code to get some information
clc;
close all;
clear all;
%Prompt to select rail frame
% [baseFileName, folder] = uigetfile({'*'});
% fullFileName = fullfile(folder, baseFileName);
%Reading of image
Img=imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/760911/Frame%202898.jpeg');
%Convert image to grayscale
grayscale=rgb2gray(Img);
%Sobel Edge detection & filter weak edges.
DetectedEdges = edge(grayscale,'Sobel','horizontal');
FilteredImage = bwareaopen(DetectedEdges,60);
%Hough Transformation with Peak & Lines identification.
[H,theta,rho] = hough(DetectedEdges);
Peaks = houghpeaks(H,5,'threshold',ceil(0.5*max(H(:))));
Lines = houghlines(DetectedEdges,theta,rho,Peaks,'FillGap',10,'MinLength',150);
%Display Filtered Weak Edges & Draw Detected Lines on Grayscaled Frame.
figure('Name','Filtered Weak Edges & Lines Detection','NumberTitle','off');
subplot(2,1,1), imshow(FilteredImage);
subplot(2,1,2), imshow(grayscale), hold on
%Identify Frame dimensions & line coordinates in both upper & lower rail edge.
[size_x,size_y] = size(grayscale);
min_y = size_y;
max_y = 0;
%Set condition to determine upper & lower edge.
for k = 1:length(Lines)
xy = [Lines(k).point1;Lines(k).point2];
if xy(1,2) <= min_y
min_y = xy(1,2);
min_y_line = xy;
end
if xy(1,2) >= max_y
max_y = xy(1,2);
max_y_line = xy;
end
end
%Use straight line formula to determine y-intercepts & gradients.
min_y_gradient = (min_y_line(2,2) - min_y_line(1,2)) / (min_y_line(1,2) - min_y_line(1,1));
min_y_intercept = min_y_line(1,2) - min_y_gradient*min_y_line(1,1);
plot(min_y_line(:,1),min_y_line(:,2),'LineWidth',2,'Color','green');
max_y_gradient = (max_y_line(2,2) - max_y_line(1,2)) / (max_y_line(1,2) - max_y_line(1,1));
max_y_intercept = max_y_line(1,2) - max_y_gradient*max_y_line(1,1);
plot(max_y_line(:,1),max_y_line(:,2),'LineWidth',2,'Color','red');
% hold off
%Note that 'hold on' in Line 45 retains plots in the current axes.
% gen mask
mask = zeros(size(grayscale));
xt = linspace(1, size(grayscale,2), 1e3);
kt = polyfit(min_y_line(:,1), min_y_line(:,2), 1);
yt = polyval(kt, xt);
hold on;
plot(xt, yt, 'r-', 'LineWidth', 2);
mask(round(yt), round(xt)) = 1;
xt = linspace(1, size(grayscale,2), 1e3);
kt = polyfit(max_y_line(:,1), max_y_line(:,2), 1);
yt = polyval(kt, xt);
hold on;
plot(xt, yt, 'g-', 'LineWidth', 2);
mask(round(yt), round(xt)) = 1;
mask = logical(mask);
mask = imclose(mask, strel('line', size(mask,1), 90));
[r,c] = find(mask);
rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)];
% crop
Img1 = Img(:,:,1); Img2 = Img(:,:,2); Img3 = Img(:,:,3);
Img1(~mask) = 0; Img2(~mask) = 0; Img3(~mask) = 0;
Imgt = cat(3, Img1, Img2, Img3);
figure; imshow(Imgt);
hold on; rectangle('Position', rect, 'EdgeColor', 'r', 'LineWidth', 2, 'LineStyle', '-');
Imgt2 = imcrop(Imgt, rect);
figure; imshow(Imgt2);
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!