can any one tell me how to convert an image of type 'unit8' to the image of type 'float'.basicaly i am following a book of 'Digial Image Processing Using Matlab' where they try to enhance a gray image .following is the code.thanks in advance

w=fspecial('laplacian',0);
f=imread('aa.jpg');
g1=imfilter(f,w,'replicate');
imshow(g1,[])
%as we get only a possitve image so he first try to convert the image type from 'unit8'to 'float'.so he write like this%
f2=tofloat(f);
%at here i am getting an error as [UNDEFINED FUNCTION OR METHOD 'tofloat' FOR INPUT ARGUMENTS OF TYPE 'unit8'].at this stage i could not understand what to do.%
%forther code is writen like this %
g2=imfilter(f2,w,'replicate');
imshow(g2,[])
%then he subtract the images like%
g=f2-g2;
imshow(g);

 Accepted Answer

f2 = single(f); %if you want the data type that is most commonly called "float"
f2 = double(f); %if you want the "natural" floating point data type
f2 = im2double(f); %if you also want the uint8 values rescaled to 0-1 range

2 Comments

sir i am using the code "f2=single(f);" %where f is the input image and f2 is the converted image by single(f).%But i am getting an problem with it ,i.e,its writen on the command window as "Warning: Image is too big to fit on screen; displaying at 67% > In imuitools\private\initSize at 73 In imshow at 262 In page77 at 7 Warning: Image is too big to fit on screen;" please tell me what to do? thank you for your support
Do nothing. It's just saying that it's zooming your picture so that it will fit onto your screen and you can see the whole image instead of just a small part of it. Don't worry about it. If you want, you can suppress the warning with this code:
% Turn off this warning "Warning: Image is too big to fit on screen; displaying at 33% "
% To set the warning state, you must first know the message identifier for the one warning you want to enable.
% Query the last warning to acquire the identifier. For example:
% warnStruct = warning('query', 'last');
% messageID = warnStruct.identifier
% messageID =
% MATLAB:Images:initSize:adjustingMag
warning('off', 'Images:initSize:adjustingMag');

Sign in to comment.

More Answers (3)

tofloat is a function provided with the book you are reading. You can find it in Appendix C on page 806.

6 Comments

sorry sir i am using the 2nd edition of "DIGITAL IMAGE PROCESSING using MATLAB" by "R.C.GONZALEZ,R.E.WOODS,S.L.EDDINS" so i could not get it on the right place as you told. but according to you i try to find on Appendix C and get a code wcich is like
function [f2,revertclass]=tofloat(f)
identity=@(X) X;
tosingle=@im2single;
table={'unit8',tosingle,@im2unit8
'unit16',tosingle,@im2unit16
'int16',tosingle,@im2int16
'logical',tosingle,@logical
'double',tosingle,identity
'single',tosingle,identity};
classindex=find(strcmp(class(f),table(:,1)));
if isempty(classindex)
error('unsupported input image class.');
end
out=table{classindex,2}(f);
revertclass=table{classindex,3};
"here%f is the input image by me%" but still i am getting some errors like
"%??? Error using ==> tofloat at 12
unsupported input image class.
Error in ==> page77 at 9
[f2,revertclass]=tofloat(f);
%"
sir what to do now about this sir. thank you for your kind support.
'unit8' should be 'uint8' and likewise 'unit16' should be 'uint16'
Change your code where you currently have
table={'unit8',tosingle,@im2unit8
'unit16',tosingle,@im2unit16
'int16',tosingle,@im2int16
'logical',tosingle,@logical
'double',tosingle,identity
'single',tosingle,identity};
to become
table={'uint8',tosingle,@im2uint8
'uint16',tosingle,@im2uint16
'int16',tosingle,@im2int16
'logical',tosingle,@logical
'double',tosingle,identity
'single',tosingle,identity};
unit and uint are two different words. The middle two characters are swapped.

Sign in to comment.

hello sir i'm too getting the same error how to use [f,revertclass]=tofloat(f)... Undefined function 'tofloat' for input arguments of type 'uint8'. MATLAB has this function or what is the problem please help me out

4 Comments

Did you see Steve's Answer about it being included with the book?
dear the tofloat is an user defined fuction its not exist in MATLAB
the code for tofloat given in that book is bellow which s a user defined function
f u n c t i o n [ out , revert clas s ) = tofloat ( in )
%TOF LOAT Convert image t o floating point
% [ OUT , REVERTCLASS ] = TOFLOAT ( I N ) c o n v e r t s the input image IN t o
% float ing - point . If IN is a d o u b le o r single imag e , then OUT
% equals I N . O t h e rwise , OUT equals IM2SINGLE ( I N ) . REVERTCLASS is
% a f u n ct ion handle t h at can be used to c o n v e rt back to t he class
% of I N .
i d e n t i t y
t o s ingle
@ ( x ) x ;
@im2sing le ;
table = { ' u i n t8 ' , t o s i n g l e , @im2 uint8
' u int 1 6 ' , t o s i n g l e , @im2u in t 1 6
' int 1 6 ' , t o s i n g l e , @im2 int 1 6
' logical ' , t o s in g l e , @log ical
' double ' , identity , identity
' s ingle ' , ident i t y , identit y } ;
c la s s l n d e x = f ind ( st rcmp ( c lass ( in ) , t ab le ( : , 1 ) ) ) ;
if isempt y ( c l as s l nd e x )
e r r o r ( ' Unsupported input image c l a ss . ' ) ;
end
o u t = t a b l e { c l a s s l ndex , 2 } ( in ) ;
r e v e r t c l a s s = t able { c l a s s l ndex , 3 } ;
It is a function that is defined in the book the user was using.

Sign in to comment.

1 Comment

The reference is to a book "Computational Physics: A Guide For Beginners Looking To Speed Up Their Computation" that appears to have been written by the poster.

Sign in to comment.

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!