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

1 Ansicht (letzte 30 Tage)
Graham
Graham am 11 Okt. 2024
Kommentiert: Graham am 15 Okt. 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.

Antworten (2)

John D'Errico
John D'Errico am 11 Okt. 2024
Bearbeitet: John D'Errico am 11 Okt. 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 Kommentar
Graham
Graham am 15 Okt. 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.

Melden Sie sich an, um zu kommentieren.


DGM
DGM am 11 Okt. 2024
Bearbeitet: DGM am 11 Okt. 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)

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by