Can somebody explain the following code ?
Show older comments
Please explain the following code.
sout=imresize(inp,[256,256]);
t0=60;
th=t0+((max(inp(:))+min(inp(:)))./2);
for i=1:1:size(inp,1)
for j=1:1:size(inp,2)
if inp(i,j)>th
sout(i,j)=1;
else
sout(i,j)=0;
end
end
end
Answers (1)
Just turns into binary image at the threshold value, th
NB; the double loop construct in MATLAB should be written as vector logical operation and convert the result from a logical array to the same as the input array.
t0=60;
th=t0+mean([max(inp,[],'all') min(inp,[],'all']));
sout=(inp>th);
10 Comments
Image Analyst
on 21 May 2022
Yeah but chances are that if he created a binary image it would most likely be better as a logical than converting it back to the class of the input image.
t0 = 60;
th = t0 + mean([max(inp,[],'all'), min(inp,[],'all']));
sout = inp > th; % A logical (binary) image.
dpb
on 21 May 2022
Really, @Image Analyst? I've no image processing knowledge at all to speak of, but I'd think a general principal would be to keep an image as the same numeric type as started with while processing...
The original code turns whatever it was into the default double...
Image Analyst
on 21 May 2022
Yes, really. You would, in every case I can imagine, not want to cast it to the type of the original.
dpb
on 21 May 2022
Interesting -- altho in MATLAB w/ the loose typing, it really would make no functional difference.
Walter Roberson
on 22 May 2022
It would make a difference for imshow()
We can tell from the logic that the code expects uint8 images, but it is storing 0 and 1 values into the uint8 that results from imresize() of the uint8 image.
The code also has the oddity that it resizes the input to 256 x 256, but then uses the original size of the image for the thresholding. Depending on whether the image was smaller or larger than 256 x 256 that could overwrite all of sout, or it might end up only changing one edge or one corner of it.
"It would make a difference for imshow() "
That's kinda' what I thought and why I cast back to the input -- though looking at the doc for imshow it does indicate the logical type for a BW image.
It says
imshow(BW) displays the binary image BW in a figure. For binary images, imshow displays pixels with the value 0 (zero) as black and 1 as white.
Says nothing about type there although the Input for BW does say
Input binary image, specified as a matrix.
Data Types: logical
OK. That's fair enough; if I had been familiar-enough w/ imshow I'd'a left it as logical.
I guess that's the only way it has to know it's supposed to be BW rather than grayscale if only pass a single argument; otherwise one would have to use the [0 1] range optional argument.
That would come to be normal if did this routinely; as can tell, I don't/haven't.
I patched the Answer to leave as logical.
Image Analyst
on 22 May 2022
You can use imshow() with either logical or uint8 matrices. However if you have a matrix with only 0 and 1 values and do
imshow(BW);
it will look completely black since 1 is really too dark to see in most cases. You can do
imshow(BW, []);
and then it will scale the 1's up to 255 for display and then you'd be able to see it. However if the data type is logical it will know to automatically scale the 1 up to 255 without needing to use the [].
OK, that's another way to force...still think the doc ought to explicitly mention that behavior in the description for BW argument for the uninitiated.
It makes some sense after stumbling over it, but isn't totally obvious on first blush -- certainly wasn't what was expecting as I thought it would do so effectively automagically, having never (in 30 yrs w/ MATLAB use) tried it before, since doing anything at all with images has never been in my arena of application in all that time.
Michael Van de Graaff
on 22 May 2022
why is this question tagged with 'brain-tumor'?
Walter Roberson
on 22 May 2022
The poster was providing a bit of context for the code, which is information that might potentially have helped readers understand the code better.
Categories
Find more on Images 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!