Imwrite within a function

1 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2013-6-26
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().

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by