Some .jpeg photos can not be processed by Matlab code.

2 vues (au cours des 30 derniers jours)
Graham
Graham le 11 Oct 2024
Commenté : Graham le 15 Oct 2024
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.

Réponses (2)

John D'Errico
John D'Errico le 11 Oct 2024
Modifié(e) : John D'Errico le 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×2
200 200
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x(1) + x(2)
ans = uint8 255
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)
xsum = 400
whos xsum
Name Size Bytes Class Attributes xsum 1x1 8 double
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.
  1 commentaire
Graham
Graham le 15 Oct 2024
With chagrin I thank-you very much for your assistance. I somehow thought the uint8 format would not apply when I added up 3 integers of this data type and I was wrong. Thank-you again.

Connectez-vous pour commenter.


DGM
DGM le 11 Oct 2024
Modifié(e) : DGM le 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)

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by