How to save the RGB image after selecting ROI of the image without affecting the original size?

조회 수: 1 (최근 30일)
I am using the iamge 'peppers.png' whose size is [384*512*3]. After selecting the ROI from the image using the following code, I have saved the image as 'I1'. But the size of the new image 'I1' is [745*1072*3]. How to save the new image with size [384*512*3]? Please help.
I= imread('peppers.png');
ROI=roipoly(I); %select a close polygon
myImage=findall(gcf,'type','image');
set(myImage,'AlphaData',ROI);
saveas(gcf,'newImage.png');
I1=imread('newImage.png');
size(I1)

채택된 답변

Image Analyst
Image Analyst 2019년 1월 21일
Try this, and note that the sizes are the same:
rgbImage = imread('peppers.png');
ROI = roipoly(rgbImage); % Select a closed polygon
subplot(3, 1, 1);
imshow(rgbImage);
axis('on', 'image');
title('Original Image');
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(ROI, 'like', rgbImage));
whos maskedRgbImage
% Display the masked image.
subplot(3, 1, 2);
imshow(maskedRgbImage);
axis('on', 'image');
title('Masked Image');
% Save the masked image to disk.
imwrite(maskedRgbImage, 'newImage.png');
I2 = imread('newImage.png');
whos I2
% Display the image read in from disk.
subplot(3, 1, 3);
imshow(I2);
axis('on', 'image');
title('Image read In From Disk');
0000 Screenshot.png

추가 답변 (2개)

Image Analyst
Image Analyst 2019년 1월 21일
Use imwrite() instead of saveas().
  댓글 수: 1
manami
manami 2019년 1월 21일
편집: manami 2019년 1월 21일
Thank you for your answer, But after using imwrite(), I am getting an error:
Error using imwrite (line 427)
Expected DATA to be one of these types:
numeric, logical
Instead its type was matlab.graphics.primitive.Image.

댓글을 달려면 로그인하십시오.


sarine_nassima
sarine_nassima 2019년 1월 21일
the ( recognition rate= (no. of correctly identified images / Total no. of images)*100)
my probleme it's how to calculate the no. of correctly identified images???????

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by