saveas white border matlab - matlab

I want to save a frame as an image in png format with saveas command. Although my image's size is 640X480 (gcf), the saved images size is 1201x901 and spaces white color.(like a bold white border).So I want the image to be saved 640x480.
I tried transparent background but it didn't work. How can I fix this problem?
F(j) = getframe(gcf);
fname='C:\...'
saveas(gcf, fullfile(fname, 'newImage'), 'png');

Try this,
set(gca,'position',[0 0 1 1],'units','normalized')
or you can try the imcropfunction.

If you aren't overlaying any graphics over the image, then save the image data directly using imwrite, rather than exporting a figure containing the image to png.
If you are overlaying graphics then the export_fig function (available for download on the MATLAB File Exchange) will automatically crop the whitespace around the figure. In this case use:
export_fig(fullfile(fname, 'newImage'), '-png', '-a1', '-native');

Related

Write Large Image as PDF in MATLAB

I am trying to write a large image to PDF (141"x24" at 300 DPI, so 42300 pixels x 7200 pixels). I can write the image as a png without a problem using imwrite(). However, imwrite() does not provide PDF as an output option. So, the alternatives I have seen online all do something like this:
pdffig = figure;
set(pdffig,'Units','Inches','Position',[0 0 141 24],'PaperSize',[141 24]);
pdfaxs = axes;
imshow(Im,'Parent',pdfaxs);
print(pdffig,'largeimage.pdf','-dpdf');
This code is creating a figure, setting some properties to make the figure 141x24 inches in size, showing the image on the figure axis, and then printing the figure contents to pdf.
Unfortunately, what happens is that the figure is resized to fit my screen when imshow() is called and then the printed pdf will have a small version of the image in the center of a large white pdf. What I actually want is for the image to take up the entire 141"x24" pdf. I have tried rearranging when the window size is set and when imshow() is called, but that doesn't help. Note that this works if the desired PDF size fits on my screen (e.g. if I was printing a 12"x12" PDF).
Any suggestions are appreciated!

resize and save transparent PNG file in matlab

I have an transparent png file(colored image not black and white).I want to resize this png file and save that with transparent background.but i cant save that with transparent background.file saved with dark background.
[im,map]=imread('image.png');
im=ind2rgb(im,map);
im2=imresize(im,[200,200]);
imwrite(im2,'image2.png');
in above code image2.png saved with black background and not transparent
Additionaly you have to read the transparency (alpha) value of the Image. Then you have to scale that value as well. Then you can write the Image with the alpha value.
[im, map, alpha] = imread('image.png');
im2=imresize(im,[200,200]);
resizedAlpha = imresize(alpha,[200, 200]);
imwrite(im2, 'image2.png', 'Alpha', resizedAlpha);

With ffmpeg's example extract_mvs.c, I am getting black and white images from color rtsp camera

I am using the extract_mvs.c from ffmpeg:
https://ffmpeg.org/doxygen/2.5/extract__mvs_8c_source.html
I added opencv to imwrite the image.
cv::Mat img(frame->height,frame->width,CV_8UC1,frame->data[0]);
imwrite( "pic.jpg", img );
That works because the image in the frame is in grayscale. The camera is a color camera however, and I dont know why I am getting grayscale. If I cange the above to CV_8UC3, I get segmentation fault.
I tried to save the image with ppm_save function and I still get a black and white frame when there should be a color frame. Any ideas?
Thanks,
Chris
Just read about graphics file formats and such. JPG requires BGR24 format. The raw frame buffer format YUV420P needs to be converted to BGR24 using swscale. Then the output frames height and width needs to be manually set before calling :
cv::Mat img(out_frame->height,out_frame->width,CV_8UC3,out_frame->data[0]);
imwrite( "pic.jpg", img );
Likewise,PPM file format requires RGB24 and the raw format needs to be converted to this before saving the ppm file.
Thanks,
Chris

MATLAB figure convert into PDF without cutting out parts

I didn't use to have problems with this figure, but after enlarging the font size with 'export setup' parts on the left and right side are cut out (if I convert the MATLAB figure into PDF). If I convert the MATLAB figure into JPEG format, the whole figure is displayed.
I made a new MATLAB figure with the old font size, but the parts are still cut out.
did you try print?
F = getframe(gcf);
orient portrait
print ('-dpdf', 'myimage.pdf')
alternatively to portrait you can also use orient landscape or orient fill. Especially fill should resize it so that it fits.

Image resize issue

This appears to be a trivial problem but the result is strange, totally lost where I am going wrong. There is an input RGB image which needs to be converted to gray scale and sized to 1000 x 1000 pixels. This is how I have done
img=imread('flowers.jpg');
flowers_gray=rgb2gray(img);
flowers_resize=imresize(flowers_gray,[1000 1000]);
but strangely the output image is not of 1000 by 1000 pixels. Moreover, matlab did not save the image (tried using SaveAs option and the File --->Export Setup) gray scale mode
and also the size was incorrect since when I opened the saved image by
img1=imread('flowers_resize.jpg')
s=size(img1)
it gave
s=586 665 3
And the image flowers_resize.jpg is saved with a white border surrounding it in the image folder. So, I went to Paint toolbox to select the image A1 and manually deleted the surrounding background and resized the image.But alas, it saved the image with 3 color channels and not in gray scale mode although the size was correct! Can somebody please point out the correct way of resizing to 1000 by 1000 pixels and saving in gray scale mode without the white border surrounding the saved output file? Thank you.
When you use the image export processing, you are saving the entire figure including the space around the figure (white space).
Instead, use the imwrite command. In your case:
imwrite(A1,'flowers_resize.jpg','jpg');