Imwrite within a function

1 view (last 30 days)
cjb b
cjb b on 26 Jun 2013
I know there is a simple answer to this but I can't find it and I am on a tight deadline.
I am writing a function where I input an image, modify it, and then save it with a very similar name using imwrite.
For example, I want to put the image 'IA2.bmp' through my MaskFilter function.
img = imread(image);
rgbImage = img;
redBand = rgbImage(:,:, 1);
greenBand = rgbImage(:,:, 2);
blueBand = rgbImage(:,:, 3);
redthreshold = 25;
greenThreshold = 20;
blueThreshold = 25;
redMask = (redBand > redthreshold);
greenMask = (greenBand > greenThreshold);
blueMask =1- (blueBand < blueThreshold);
redObjectsMask = uint8(redMask & blueMask);
maskedrgbImage = uint8(zeros(size(redObjectsMask)));
maskedrgbImage(:,:,1) = rgbImage(:,:,1) .* redObjectsMask;
maskedrgbImage(:,:,2) = rgbImage(:,:,2);
maskedrgbImage(:,:,3) = rgbImage(:,:,3) .* redObjectsMask;
Now I want to save my modified image as 'IA2_MaskFiltered.tiff' (I have added the _MaskFiltered and changed it from a BMP to a Tiff).
I have tried numerous variations of imwrite but I can't get it to save as the modified name. Any suggestions?
Thanks for your help.

Accepted Answer

Image Analyst
Image Analyst on 26 Jun 2013
For some reason you initialized maskedrgbImage to a single color channel. Not sure if that would cause a problem, or even what problem you observed (but didn't share) but I think this untested code should work:
maskedrgbImage = zeros(size(rgbImage), 'uint8');
maskedrgbImage(:,:,1) = rgbImage(:,:,1) .* redObjectsMask;
maskedrgbImage(:,:,2) = rgbImage(:,:,2);
maskedrgbImage(:,:,3) = rgbImage(:,:,3) .* redObjectsMask;
baseFileName = 'IA2_MaskFiltered.tiff';
yourFolder = 'D:\'; % or whatever...
fullFileName = fullfile(yourFolder, baseFileName);
imwrite(maskedrgbImage, fullFileName);
Make sure that maskedrgbImage is still a uint8 or uint16 image when you call imwrite().

More Answers (1)

Nitin
Nitin on 26 Jun 2013
one of the many solutions:
temp = image.name(1:3); new_name = [temp,'_MaskFiltered.tiff'];
imwrite(modified_image,new_name);

Categories

Find more on Image Processing Toolbox 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!