how to convert back to tiff image

15 views (last 30 days)
K.G
K.G on 25 Dec 2017
Answered: Image Analyst on 26 Dec 2017
Hi all,
I have written a code that converts an image to a three dimentional matrix and then fills the surrounding area with black color until it becomes a 1000*1000*3(for jpg) or 1000*1000*4(for tif) pixel matrix. In order to make sure that the code is working properly, I have to see the final picture. so I convert it back to the picture using imwrite. The problem is that although both jpg and tif formats are in uint8 class of data type,imwrite works only for jpg format. but for tif format, it will convert back to a grey scale picture. anyone can help fix the problem for tif format?
I could only attach the jpg file. I take it as imageIn.
you can make a tif format out of this file using paint windows app.
object = [1000 1000];
here is the code I wrote :
function imageOut = imageX(imageIn,object );
Image = imread(imageIn );
spaceRow = size(object,1)-size(Image,1 );
spaceAbove = round(spaceRow/2 );
spaceBelow = spaceRow - spaceAbove ;
Image1 = [255.*ones(spaceAbove,size(Image,2),size(Image,3));Image;255.*ones(spaceBelow,size(Image,2),size(Image,3 ))];
spaceColumn = size(object,2)-size(Image,2 );
spaceRight = round(spaceColumn/2 );
spaceLeft = spaceColumn - spaceRight ;
imageOut = [255.*ones(size(Image1,1),spaceLeft,size(Image,3)),Image1,255.*ones(size(Image1,1),spaceRight,size(Image,3 ))];
imwrite(imageOut,'imageOut.jpg '); %if the file is a tif format I am gonna write imwrite(imageOut,'imageOut.tif') instead.
end

Answers (1)

Image Analyst
Image Analyst on 26 Dec 2017
It does not look like imageOut is uint8. Cast it to uint8 and try again.
Also, object is not used so you can remove it from the input argument list.
Do not use Image as the name of your variable. We like to avoid using built-in function names for variable names, even though MATLAB is case sensitive. One typo (i for I) and you could have a hard time tracking down the problem.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!