Some .jpeg photos can not be processed by Matlab code.
2 views (last 30 days)
Show older comments
The program test is taken, with minor modifications, from a larger program I have written for image analysis. This larger program has worked well when some .jpg images are input. However, when the attached .jpg image is input, it fails completely. I have traced the problem to the inability of the test after the if statement to work correclty. The test_array shows the values of red, green and blue intensity are about 160 to 200 each so their sum must be about 480 to 600. However, even though the sum of the intensities of the colours of the pixels is greater than 300, 400, or usually, 500, the red colour of the Inclusion Darkness titled images shows the pixel somehow passes the if test. I have no idea how pixels this bright could pass such a test.
0 Comments
Answers (2)
John D'Errico
on 11 Oct 2024
Edited: John D'Errico
on 11 Oct 2024
I've not even looked at your code. But you have jpg images, uint8 code values. So I wonder if adding the code values caused an overflow.
x = uint8([200 200])
x(1) + x(2)
And now that I looked at your code, that is exactly what you did.
The images where it works are those that do not overflow the 255 boundary of uint8.
xsum = sum(x)
whos xsum
I would note that had you used sum, it would have solved your problem, converting to a double on the fly.
So you CAN process those images. But you needed to write code that is robust to what can happen.
DGM
on 11 Oct 2024
Edited: DGM
on 11 Oct 2024
The sum of the elements will always be <=255. You're adding uint8 values, so they'll be truncated.
% inputs
inclusion_darkness = 500;
inpict = imread("Wabi_P2-1_100X.jpg");
% create a mask
mask = sum(inpict,3) <= inclusion_darkness;
imshow(mask)
% if you want to do an overlay for visualization
% that's fine, but i don't think it's nearly as readable
% and the mask is what's used for processing anyway
outpict = imoverlay(inpict,mask,[1 0 0]);
imshow(outpict)
% if you want, save the image with imwrite
% instead of using a screenshot
% don't use JPG for anything of technical purpose
imwrite(outpict,'op.png')
I don't know that using the sum of channels is a great way to segment this image, and I would expect that you'd be doing something to flatten the background (imflatfield(), imtophat(), imbothat())
Something like:
% inputs
inclusion_darkness = 0.75; % unit-scale
inpict = imread("Wabi_P2-1_100X.jpg");
% strip color
inpict = im2gray(inpict);
% flatten and normalize the image
% there are other ways to do this
% normalization might not always be desirable
se = strel('disk',10);
B = imbothat(inpict,se);
inpict = 1-mat2gray(B); % this is now in unit-scale
imshow(inpict)
% create a mask
% this is the only part that we really need
mask = inpict <= inclusion_darkness;
imshow(mask)
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!