detect horizontal and vertical lines

How to detect horizontal and vertical lines in a given image using imerode function? I have this code but it doesn't detect H and V lines
im=imread('src\ima.tif');
im(:,:,2:4)=[];
im=im2bw(im);
SE = strel('arbitrary',ones(10,1));
im2 = imerode(im,SE);
imwrite(im2,'src\aa.tif');
imshow(im2)

 採用された回答

Image Analyst
Image Analyst 2014 年 9 月 28 日

2 投票

Just call bwconncomp(), then regionprops() and check if the bounding box height is less than some number, like 1 or 2 pixels. Untested code:
binaryImage = grayImage < 128;
cc = bwconncomp(binaryImage);
measurements = regionprops(cc, 'BoundingBox');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
if thisBB(4) <= 3 % If it's shorter than 4 lines tall.
message = sprintf('Blob #%d is horizontal.', k);
sprintf('%s\n', message);
uiwait(helpdlg(message));
end
end

8 件のコメント

Mohammad
Mohammad 2014 年 9 月 28 日
thanks but this doesn't do what I want. As I said i want to detect the H & V lines and create a new image contains these V & H lines.
Image Analyst
Image Analyst 2014 年 9 月 28 日
Not, not completely. I was hoping this little bit was enough for you to finish it. I did not do a complete turnkey application for you. All you have to do to do what you want is to put imcrop into the loop
croppedImages{k} = imcrop(binaryImage, thisBB);
and to change the if statement to look for thisBB(3) less than some width
% If it's shorter than 4 lines tall or narrower than 3 lines.
aspectRatio = thisBB(4)/thisBB(3);
if (thisBB(4) <= 3 || thisBB(3) <= 3) && aspectRatio > 4
I check the aspect ratio to make sure that it must be long and skinny, not just a little round dot or something. If you still can't complete the program, let me know. You might have to debug or change the numbers somewhat. I did not test this - it's just off the top of my head, based on my experience.
Mohammad
Mohammad 2014 年 9 月 28 日
Thanks again. I am new to Matlab and I am trying to learn from your code which will be like the following:
clc
grayImage =imread('src\a.tif');
binaryImage = grayImage < 128;
cc = bwconncomp(binaryImage);
measurements = regionprops(cc, 'BoundingBox');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
aspectRatio = thisBB(4)/thisBB(3);
if (thisBB(4) <= 3 || thisBB(3) <= 3) && aspectRatio > 4
croppedImages{k} = imcrop(binaryImage, thisBB);
end
end
But again how to show this image "croppedImages" I tried imshow and imwrite but it doesn't work?
Image Analyst
Image Analyst 2014 年 9 月 28 日
編集済み: Image Analyst 2014 年 9 月 28 日
Alright. You just forgot to call imshow after you called imcrop. But I did it for you. Below is a full blown demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;
folder = 'C:\Users\Mohammad\Documents\Images';
baseFileName = 'ima.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Save this figure handle.
hFig1 = gcf;
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(1, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
binaryImage = grayImage < 128;
% Display the binary image.
subplot(1, 3, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Display the binary image again where we can put boxes over it.
subplot(1, 3, 3);
imshow(binaryImage, []);
hold on;
title('Binary Image', 'FontSize', fontSize);
% Create a figure for the cropped images.
hCropped = figure;
% Do connected components analysis on it.
cc = bwconncomp(binaryImage);
% Measure the bounding box of all blobs.
measurements = regionprops(cc, 'BoundingBox');
fprintf('Found %d regions\n', cc.NumObjects);
numSkinnyRegions = 0;
for k = 1 : cc.NumObjects
figure(hFig1); % Switch to figure 1.
thisBB = measurements(k).BoundingBox
% Draw a box around the region in cyan.
hRect = rectangle('Position', thisBB, 'EdgeColor', 'c', 'LineWidth', 3);
aspectRatio(k) = thisBB(4)/thisBB(3);
if (thisBB(4) <= 3 || thisBB(3) <= 3) && (aspectRatio(k) > 4 || aspectRatio(k) < 1/4)
numSkinnyRegions = numSkinnyRegions + 1;
% Save it to a cell array, just in case we want to use it after the loop is done.
croppedImages{numSkinnyRegions} = imcrop(binaryImage, thisBB);
% Draw skinny regions in a different color
delete(hRect); % Get rid of old one.
hRect = rectangle('Position', thisBB, 'EdgeColor', 'r', 'LineWidth', 3);
% Switch to figure 2
figure(hCropped);
subplot(2, 3, numSkinnyRegions);
imshow(croppedImages{numSkinnyRegions}, []);
caption = sprintf('Blob #%d', numSkinnyRegions);
title(caption, 'FontSize', fontSize);
end
end
% Enlarge figure to full screen.
set(hCropped, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(hCropped, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
Mohammad
Mohammad 2014 年 9 月 28 日
Thanks very much
Image Analyst
Image Analyst 2014 年 9 月 29 日
You're welcome. Since it seems like it does everything you want, would you mind going ahead and marking my answer as Accepted? Thanks in advance.
Tina
Tina 2015 年 9 月 22 日
Hi, I'm new to Matlab and Matlab Answers. I have a query. While detecting vertical lines, what if I need to detect the lines from the alphabets also? For example quoting the same input image as above, the letter H has two vertical lines, letters R, L, D has one vertical line. How do I detect them? Thanks.
Firdausy angga Permana
Firdausy angga Permana 2017 年 6 月 5 日
Hi Image Analyst. I've tried this code and it's really helping me to solve my problem. But, how can we remove that lines? Thanks Before!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by