detect horizontal and vertical lines
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
1 개 추천
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
2014년 9월 28일
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
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.
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.
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
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
2014년 9월 28일
Thanks very much
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
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.
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 Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
