draw horizontal lines on edges of a image

8 views (last 30 days)
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
  2 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 23 Jan 2021
Please try with other segmentation approaches, may take time, but it is absolutely possible to get those bars band to draw lines.
J. Alex Lee
J. Alex Lee on 23 Jan 2021
I assume one of the greatest challenges with an edge detection is all those vertical polishing lines...maybe you can go a bit beyond edge detection to determine edge angle. In principle, this is taking the arctangent of the gradient vector components, but you'll still be bit by noise...so you can do some more analysis of the eigenvalues of the image gradient (see e.g., structure tensor), and you can get something like below. Combined with some other ways to filter based on other properties of the image, maybe you can achieve your goal

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 23 Jan 2021
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.
  4 Comments
Image Analyst
Image Analyst on 26 Jan 2021
Tommy - what's the status on this? Did my Answer solve it? If so, can you "Accept this answer". If my Answer did not help, what's the remaining problem?
Image Analyst
Image Analyst on 29 Jan 2021
bandStarts and bandStops are the coordinates of the actual lines. Call polyfit if you want them to be smoother.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!