How to detect the black line from a captured image?

조회 수: 16 (최근 30일)
WanYu
WanYu 2020년 3월 9일
댓글: WanYu 2020년 3월 10일
Hi,
I have both the same image, while one is printed out and another one is edited through softcopy.
Captured Image
I printed it out and take a photo of it using my phone camera.
Picture a
Softcopy
Picture b
My problem is, I can detect the presence of the black line in Picture b but I couldnt when I use Picture a.
Here is my code,
%% Convert RGB to grayscale image
if numberOfColorChannels > 1
grayImage = rgb2gray(originalImage);
else
grayImage = originalImage;
end
%% Histogram Equalization
subplot(2, 3, 2)
imhist(grayImage);
grid on;
title('Histogram of Gray Scale Image', 'FontSize', fontSize);
% Binarize Image
% Turn it into a binary image.
binaryImage = grayImage < 10;
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binarized Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Remove small objects.
binaryImage = imclearborder(binaryImage);
binaryImage = bwareaopen(binaryImage, 300);
% Display it.
subplot(2, 3, 4);
imshow(binaryImage, []);
title('Cleaned Binary Image', 'FontSize', fontSize);
%% Invert Binarization
binaryImage = ~ binaryImage;
subplot(2, 3, 5);
imshow(binaryImage);
title('Inverted Binary Image', 'FontSize', fontSize);
%% Scanning of black pixels
thresh = 10;
[y1,x1] = find(binaryImage == 0, 1 );
[y2,x2] = find(binaryImage == 0, 1, 'last');
In y1 and x1, the value shows empty for Picture a.
Can someone explain to me why and provide me a solution to solve this problem?
Thank you

답변 (2개)

Benjamin Großmann
Benjamin Großmann 2020년 3월 9일
If you print it out and make a photo, then black is not pure black any more but some kind of grayish or with some kind of color fault. That being said, your limit of 10 inside this line of code
binaryImage = grayImage < 10;
is not picking anythin. You could adapt the limit or look at the color image with the Color Thresholder app. The seperation by color is not that easy anymore with a printed and captured image.
If you give more details about your problem. than i am pretty sure, that you will get more advanced and robust approaches.
  댓글 수: 1
WanYu
WanYu 2020년 3월 9일
Hi,
Thanks for answering my question.
I am not aware of the black colour is no longer the pure black anymore earlier but I got the idea now.

댓글을 달려면 로그인하십시오.


Adam Danz
Adam Danz 2020년 3월 9일
If you zoom in to the lower side of the image histogram you created, you'll notice a hump just before x = 40.
imhist(grayImage);
xlim([0,60])
Based on those values, it looks like a good threshold to detect the black like would be about 40 which results in the following binarized image
binaryImage = grayImage < 40;
imshow(binaryImage, []);
If the line is always expected to be horizontal (or vertical), you could use the "BoundingBox" property of regionprops() to get the position of the black line.
stats = regionprops(binaryImage, 'BoundingBox'); % acting on the cleaned image
hold on
rectangle('Position', stats.BoundingBox, 'EdgeColor', 'r')
  댓글 수: 7
WanYu
WanYu 2020년 3월 10일
Hi Adam,
I got the idea now. Thanks for explaining.
WanYu
WanYu 2020년 3월 10일
Hi Image Analyst,
How is it different by using the Colour Thresholder app? Also, I don't understand the part for HSV colour space, I am new to image processing yet.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by