How I can change this operation imshow(im16,[WC-WW/2,WC+WW/2]); into a variable in matlab?

71 views Asked by At

I want to save the operation imshow(im16, [WC-WW/2,WC+WW/2]); in a new variable. It is an image that I'm displaying a certain range, but I don't want to use imshow(), I only want to save a new image with a certain range of intensity window result of the operation WC-WW/2,WC+WW/2.

I'm working with CT images in Matlab (in png format), and adjusting window width and window level.

1

There are 1 answers

1
Matteo V On BEST ANSWER

What imshow(im16,[WC-WW/2,WC+WW/2]) does is to put every pixel in im16 with intensity below WC-WW/2 to black (0) and every pixel with intensity above WC+WW/2 to white (255), and then rescale the remaining pixels. One way to do this would be:

im = imread('corn.tif',3); % your image goes here

low = 0.2*255; % lower intensity that you want to put to black (WC-WW/2)
high = 0.8*255; % upper intensity that you want to put to white (WC+WW/2)

im1 = double(im); % turn into double
idxlow = im1 <= low; % index of pixels that need to be turned to black
idxhigh = im1 >= high; % index of pixels that need to be turned to white
im1(idxlow) = 0; % black
im1(idxhigh) = 255; % white
im1(~idxlow & ~idxhigh) = im1(~idxlow & ~idxhigh) - min(im1(~idxlow & ~idxhigh),[],'all'); % rescale low values
im1(~idxlow & ~idxhigh) = im1(~idxlow & ~idxhigh).*255./max(im1(~idxlow & ~idxhigh),[],'all'); % rescale high values
im1 = uint8(im1); % turn back into uint8 (or your initial format)

Just to check:

figure
hold on
% Show the original image
subplot(1,3,1)
imshow(im)
title('Original')
% Show the original image reranged with imshow()
subplot(1,3,2)
imshow(im,[low,high])
title('Original scaled with imshow')
% Show the new reranged image
subplot(1,3,3)
imshow(im1)
title('New image')