Frame Capture in external file Matlab - image

I captured a frame using capture() function in Matlab, However it returns a black image with a small area of captured whole image, I dont know why this is happening. I want to do this in every 5th iteration of loop.
I want to store that image in file, what syntax should I follow in order to check the image in file.
Further more, what are the suggestions for capturing frames in Matlab and save them in file for later estimation?
EDIT1: My code is working perfect now, but not capturing the frame, a black image instead.
My Code:
w = vrworld('myworld');
open(w);
c = vr.canvas(w, gcf, [30 30 300 200]);
z=capture(c);
image(z);
savename = [...
'C:\Users\My-PC\Documents\MATLAB\Project\image_', ...
num2str(1), '.jpg'];
imwrite(z, savename);

Related

how save the result of imagesc and getframe in Matlab?

I'm wondering how can I save or read all the frames of the image as a image. I wrote the following code, but the result from the movie_frame(fr) is a structure while I need a image with all frames as I need it for doing block matching between different frames of the image.
Thank you very much.
I tried imwrite() which is not working, because Movie_Frames(fr) type was struct. Movie_Frames is giving me a cdata which is different with the size of the image.
clc
clear
close all
load im_sim
load info
for fr =1:size(im_sim,3);
clf;imagesc(info.x,info.y,im_sim(:,:,fr)),colormap gray, axis image,
hold on; title(num2str(fr));
pause(0.05);
Movie_Frames(fr) = getframe;
end

Save output image in matlab window to image

I have a image and some contours as bellow figure. I want to save the output into image (png or jpg). The saved image only contains the image region without the matlab window. Let see my example in the figure. Could you have me implement it by matlab? This is my code to make output figure
img = imread('coins.png');
mask_red=zeros(size(img));
mask_green=zeros(size(img));
mask_red(30:160,40:170)=1;
mask_green(70:100,60:130)=1;
imagesc(uint8(img),[0 255]),colormap(gray),axis off;axis equal,
hold on;
[c1,h1] = contour(mask_red,[0 0],'r','Linewidth',3);
[c2,h2] = contour(mask_green,[0 0],'g','Linewidth',3);
hold off;
%% Save output figure
Use the getframe and cdata idiom. If the figure is open, simply do this:
f = getframe;
im = f.cdata;
im will contain the image that was contained inside your frame as a RGB image. Running your code in your post, then the above code, then doing imshow(im), we get:
If you want to save the image, just use imwrite:
imwrite(im, 'coins_final.png');
The image will be saved in a file called coins_final.png.

Matlab: pixel values of multiple .jpg images

I have a couple hundred images like this one:
They are in a single folder and am trying to figure out a way in Matlab to automatically analyze each image's pixel values at point (700,755). I know how to do this one-at-a-time, as such:
rgb=impixel(p,700,755)
This returns the red-green-blue values at that particular point for the image. I'm very new to Matlab...what piece of code would analyze each image in the folder and save the RGB value on separate lines in a table/array?
Also, I have selected an area using the pixel region tool: '[696.463836477986 750.095011390851 19.9889937106933 13.3672527600921]' How do I analyze all the pixel values in that area and get the statistics (min, max, mean, etc.)...plus do that for all 200 images I have in the folder?
I appreciate the help!
AP
impixel can work for all the image as well:
impixel(I)
or for a specific (pixel) column and row:
impixel(I,c,r)
But you need first to read the image into a matrix. The imread function, returns all the RGB data of an image in an array:
A = imread(filename, fmt)
it reads a grayscale or color image from the file specified by the string filename. A is an array containing the image data. If the file contains a 'grayscale' image, A is an 'M-by-N' array. If the file contains a 'truecolor' image, A is an 'M-by-N-by-3' array. (3: R-G-B)
To read a bunch of files in a folder do this:
files = dir('*.jpg');
for i=1:length(files)
eval(['imread ' files(i).name]);
end
You can use imcrop function to crop the images you have:
using mouse:
I2 = imcrop(I)
or using dimensions:
I2 = imcrop(I,[75 68 130 112]);

gray levels are changed after using imsave function

I used these codes to produce an RGB image with gray levels between 50 and 170.
a='C:\Users\sepideh\Desktop\IP_abadpour\S45C-113050518040.jpg';
b=imread(a);
b=b+50;
b(b>170)=170;
and you'll see when I call functions max and min, it is proved that the gray levels are between 50 and 170.
max(max(max(b)))
ans =
170
min(min(min(b)))
ans =
50
then I used imshow and imsave functions to save the image with the name "50to170"
c=imshow(b);
d=imsave(c);
Now I read the written image in this way:
a='C:\Users\sepideh\Desktop\IP_abadpour\50to170.jpg';
b=imread(a);
This time when I call max and min functions,I see:
max(max(max(b)))
ans =
235
min(min(min(b)))
ans =
16
I mean it seems that gray levels have been changed after using imshow and imsave functions!
Why does it happen?
Is it because of the format (.jpg) that I'm using when employing imsave function?
Instead of using imsave, use imwrite
b=imread(a);
b=b+50;
b(b>170)=170;
imwrite(b,'50to170.png','png')
Notice that I am saving it as a png file instead of a jpg to prevent compression. Bitmap also saves it without compression.
This method is a more direct way to save raw image matrices than using imshow and imsave.
If you want the same functionality of imsave (selecting where the file goes) check out "uiputfile" to get file name and location.
I tested it.
1.First of all you can't use imsave(b) because the function imsave expects its first input argument to be a valid handle to a single graphics object.So first you should show the image in form c=imshow(b) and then use c as an input for function imsave.
2.If you want to save the image without showing it first use function "imwrite" and pass b as the first input argument to it.
Note that if you don't want gray levels be changed after saving, you should use 'bmp' as the save format not 'jpg'.
Because 'jpg' does not support indexed images and "imwrite" converts indexed images to RGB before writing data to JPEG files so the gray levels might be changed.It's not related to the contrast stretching when showing the image in matlab.It's related to the format used to save the image.

HowTo put region of Pyglet image into PIL Image?

My app reads frames from video (using pyglet), extracts a 'strip' (i.e. region - full width by 1 to 6 video lines), then pastes each strip (appends it) into an image that can later be written out an *.png file.
Problem is Pyglet GL images are stored in graphics memory, so are very limited re size.
My current klunky workaround is build up the to-be png image as small tiles (i.e. within the pyglet GL size limit), when the tile is full, write it out to a *png file, read that back in as a PIL Image file, then paste/append the tile image to the final PIL Image * png file.
I reckon it should be possible to convert the pyglet-GL strip to a PIL Image-friendly format, and paste/append that straight into the final PIL Image, thus having no need for the tile business, which greatly complicates things and has performance impact.
But I can't find how to get the strip data into a form that I can paste into a PIL Image.
Have seen many requests re converting the other way, but I've never run with the herd.
I know it's bad form to reply to one's own question, but I worked out an answer which may be useful to others, so here it is...
nextframe = source.get_next_video_frame() # get first video frame
while nextframe != None:# returns None at EOF
#
### extract strip data, conver to PIL Image-friendly form, paste itn into travo Image
#
strip = nextframe.get_region(0, (vid_frame_h//2)-(strip_h//2), strip_w, strip_h) # Extract strip from this frame
strip_image_data = strip.get_image_data()
strip_data = strip_image_data.get_data(strip_image_data.format, strip_image_data.pitch)
strip_Image = Image.frombuffer("RGB", (strip_w, strip_h), strip_data)
travo_image.paste(strip_Image, (0, travo_filled_to, strip_w, travo_filled_to + strip_h)) # paste strip image into travo_image
travo_filled_to = travo_filled_to + strip_h # update travo_filled pointer
nextframe = source.get_next_video_frame() # get next video frame (EOF returns None)
end of "while nextframe != None:"

Resources