How to detect the black line from a captured image?
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275994/image.jpeg)
Softcopy
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275995/image.jpeg)
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
0 Commenti
Risposte (2)
Benjamin Großmann
il 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.
Adam Danz
il 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])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276057/image.png)
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, []);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276058/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276059/image.png)
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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276060/image.png)
7 Commenti
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!