How to detect the black line from a captured image?

9 views (last 30 days)
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

Answers (2)

Benjamin Großmann
Benjamin Großmann on 9 Mar 2020
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 Comment
WanYu
WanYu on 9 Mar 2020
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.

Sign in to comment.


Adam Danz
Adam Danz on 9 Mar 2020
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 Comments
WanYu
WanYu on 10 Mar 2020
Hi Adam,
I got the idea now. Thanks for explaining.
WanYu
WanYu on 10 Mar 2020
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!