MATLAB: Draw horizontal lines on edges of a image

drawing linesedge detectionhorizontal line on edge

Hello,
i have a picture ("problem.png) and i want to draw horizontal lines on the edges in the image, like "result.png"
I have tried it with edge detection but unfortunately no useful results. Is there another way to do this?
I am grateful for any help. THX

Best Answer

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%--------------------------------------------------------------------------------------------------------

% READ IN IMAGE
folder = pwd;
baseFileName = 'problem.png';
grayImage = imread(baseFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = min(grayImage, [], 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.

subplot(2, 3, 1);
imshow(grayImage, []);
axis('on', 'image');
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
subplot(2, 3, 2);
imhist(grayImage);
grid on;
% Threshold the image.
grayImage = adapthisteq(grayImage);
subplot(2, 3, 3);
imshow(grayImage);
axis('on', 'image');
title('Flattened Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
verticalProfile = mean(grayImage, 2);
% Display the image.
subplot(2, 3, 4);
plot(verticalProfile);
grid on;
threshold = 110;
yline(threshold, 'Color', 'r', 'LineWidth', 2);
title('Vertical Profile', 'FontSize', fontSize);
% Threshold the image
binaryImage = imfill(grayImage < threshold, 'holes');
% Take the 3 largest blobs

binaryImage = bwareafilt(binaryImage, 3);
% Snip off small tendrils using imopen()
binaryImage = imopen(binaryImage, true(1, 3));
% Take the 3 largest blobs
binaryImage = bwareafilt(binaryImage, 3);
subplot(2, 3, 5);
imshow(binaryImage);
axis('on', 'image');
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% If we assume the bands are perfectly horizontal, we cna use strfind() to find the tops and bottoms.
binaryProfile = (verticalProfile > threshold)';
bandStarts = strfind(binaryProfile, [0, 1])
bandStops = strfind(binaryProfile, [1, 0])
for k = 1 : length(bandStarts)
yline(bandStarts(k), 'Color', 'r', 'LineWidth', 2);
yline(bandStops(k), 'Color', 'r', 'LineWidth', 2);
end
% If they are tilted, we can use fitPolynomialRANSAC() in the Computer Vision Toolbox.
% Code for this is not shown but is straightforward.
There are lines going across the binary image in red. I assumed they were perfectly horizontal. If they are tilted, you can find the tops and bottoms of each band and use polyfit or fitPolynomialRANSAC() to get the equation of the tilted lines. I didn't give code for that.