Why my arithmetic function not working ?

2 views (last 30 days)
Nur Suhada
Nur Suhada on 22 Jan 2022
Answered: Voss on 22 Jan 2022
function uploadimage_Callback(hObject, eventdata, handles)
%% to import the image into the matlab
global image1
[filename, pathname]=uigetfile('*.*', 'Pick a MATLAB code file');
filename=strcat(pathname,filename);
image1=imread(filename);
image1=im2double(image1);
axes(handles.axes1);
imshow(image1);
title('\fontsize{20}\color[rgb]{0.996, 0.592, 0.0}Kidney Ultrasound')
% --- Executes on button press in convertgrayscale.
function convertgrayscale_Callback(hObject, eventdata, handles)
global image1;
%% change it into gray scale image
axes(handles.axes2);
b=rgb2gray(image1);
imshow(b);
%% make the image into binary image
impixelinfo;
c=imbinarize(b,20/255);
imshow(c);
%% to fill the holes
d=imfill(c,'holes');
imshow(d);
%% remove some lighting remove the unwanted things in the image
rem=bwareaopen(d,1000);
imshow(rem);
PreprocessedImage= uint8(double(image1).*repmat(rem,[1 1 3])); %%% this code when i run it not provided the right image i want
imshow(PreprocessedImage);
PreprocessedImage=imadjust(PreprocessedImage,[0.3 0.7],[])+50;
imshow(PreprocessedImage);

Answers (1)

Voss
Voss on 22 Jan 2022
From the documentation for im2double(): "im2double rescales the output from integer data types to the range [0, 1]."
So if your file contains an image of an integer data type, e.g., uint8, then your variable image1, after passing through im2double(), will be doubles between 0 and 1. Later when you multiply image1 by the logical variable rem, that product is doubles between 0 and 1, so converting those to uint8 will give 0s and 1s only. You have to scale the product by 255 before converting to uint8 in order to cover the range of values a uint8 variable can take. See below for further explanation.
Here I am reproducing each step of your process, with the second-to-last step done twice: once the way you had it and once the new way (mulitplying by 255 before converting to uint8):
filename = 'Saturn_Hexagon.png';
image1=imread(filename);
image1=im2double(image1);
figure();
subplot(2,2,1);
imshow(image1);
% title('\fontsize{20}\color[rgb]{0.996, 0.592, 0.0}Kidney Ultrasound')
subplot(2,2,2);
b=rgb2gray(image1);
imshow(b);
subplot(2,2,3);
% impixelinfo;
c=imbinarize(b,100/255); % I modified this threshold to get something interesting with my image
imshow(c);
subplot(2,2,4);
d=imfill(c,'holes');
imshow(d);
figure();
subplot(2,2,1);
rem=bwareaopen(d,1000);
imshow(rem);
%%%%% The old way %%%%%
% PreprocessedImage= uint8(double(image1).*repmat(rem,[1 1 3])); %%% this code when i run it not provided the right image i want
temp = double(image1).*repmat(rem,[1 1 3]); % image1 is already double; no need to convert it
fprintf('class: %s, min: %f, max: %f\n',class(temp),min(temp(:)),max(temp(:)));
class: double, min: 0.000000, max: 1.000000
PreprocessedImage= uint8(temp);
fprintf('class: %s, min: %f, max: %f\n',class(PreprocessedImage),min(PreprocessedImage(:)),max(PreprocessedImage(:)));
class: uint8, min: 0.000000, max: 1.000000
subplot(2,2,2);
imshow(PreprocessedImage);
title('old');
%%%%%%%%%%%%%%%%%%%%%%%
%%%%% The new way %%%%%
% PreprocessedImage= uint8(255*image1.*repmat(rem,[1 1 3]));
temp = 255*image1.*repmat(rem,[1 1 3]);
fprintf('class: %s, min: %f, max: %f\n',class(temp),min(temp(:)),max(temp(:)));
class: double, min: 0.000000, max: 255.000000
PreprocessedImage= uint8(temp);
fprintf('class: %s, min: %f, max: %f\n',class(PreprocessedImage),min(PreprocessedImage(:)),max(PreprocessedImage(:)));
class: uint8, min: 0.000000, max: 255.000000
subplot(2,2,3);
imshow(PreprocessedImage);
title('new');
%%%%%%%%%%%%%%%%%%%%%%%
subplot(2,2,4);
PreprocessedImage=imadjust(PreprocessedImage,[0.3 0.7],[])+50;
imshow(PreprocessedImage);

Community Treasure Hunt

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

Start Hunting!