Imwrite within a function

조회 수: 5 (최근 30일)
cjb b
cjb b 2013년 6월 26일
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