matlab - How do I save an image generated by imshow(image) into a variable? -
this code. want save image displayed on imshow(img) variable use later. thanks!
img=imread('image1.bmp'); figure(1), imshow(img); [r c]=ginput(4); bw=roipoly(img,r,c); % figure,imshow(bw) [r c]=size(bw); i=1:r j=1:c if bw(i,j)==1 img(i,j)=img(i,j); else img(i,j)=0; end end end figure, imshow(img); title ('output image');
you can use classic getframe / cdata
idiom. figure window open, this:
figure; imshow(img); title('output image'); h = getframe; im = h.cdata;
h
handle current frame open, , cdata
field contains image data frame. above code stores frame image data variable called im
use later.
minor comment
that for
loop code set output bit inefficient. can vectorized , you'll notice significant speedups.
this code:
for i=1:r j=1:c if bw(i,j)==1 img(i,j)=img(i,j); else img(i,j)=0; end end end
... can replaced with:
img(~bw) = 0;
i don't understand why you'd need store image data inside frame of imshow
... when img
contains data , showing data contained in img
. why can't use img
directly application? nevertheless, above solution work.
Comments
Post a Comment