Imwrite function in Matlab doesnt save all the images - image

I have a series of images in url obtained from Google, My question is
Why imwrite doesn't not save all the images in the link? More precise, I stored the extracted image in im =imread(images), but the problem occurs when the number of saved images reaches up to 80 images? Even though the image 81 is existed in the "im" variable but does not pass to the imwrite to save it?
Is the problem happening because I have limited access to a number of images from Google? or is it because the imwrite function has a problem?
Is there anyone who can explain to me why such a problem happening?

Try to replace the imwrite line in your loop with something like (with k being your loop increment):
filepath{k}=fullfile(imgpath,T_MODE,keyword,strcat('img_',num2str(count_save),'.jpg');
imageSize{k}=size(im);
Run the loop once and browse through the resulting cells to see if the name of the file is always different (otherwise it will overwrite the existing files) and that the size of the images are not zero. Then you will have ruled out two reasons for your images not saving.

Related

Images turning green-blackish when passing them to dataloaders

I'm working with fastai, trying to pass some images to a dataloader. The original images are kind of pinkish, but after passing them they appear mostly as green-black (see image in link below):
Original pinkish image (up) and example images (down) after passing them to dataloader, and the code.
The code I've used for the datablock and to show the images is:
example = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=GrandparentSplitter(),
get_y=parent_label,
item_tfms=Resize(128)) #already tried it without item_tfms just in case, still black-green
dls = example.dataloaders(path)
dls.show_batch(nrows=1, ncols=3)
I tried with .tif and .jpeg images, and both show the same problem. The only thing that comes to my mind is that somewhat somewhere is not reading correctly the color format (RGB according to my original files), or maybe transforming it; but I'm not able to figure it out.
Just in case it's important, I'm working in a Jupyter notebook with a MackBook Air M1.
Thanks!
Irene

Matlab create image from array and save without displaying

Apologies if this is a dupe, I've been searching for over an hour but the search terms are all really broad and I just keep getting the same results. Also I'm fairly new to matlab so apologies for any misunderstandings.
Anywho, I have a matlab program which needs to frequently save an image generated from a matrix, but I just can't figure out how to do that without displaying it first. Basically I'm caught in between two functions, image and imwrite, both only do half of what I want.
image is able to take my matrix and create the desired output, but it just displays it to a figure window
imwrite is able to save an image to a file without displaying it, but the image is completely wrong and I can't find any parameters that would fix it.
Other questions I've seen deal with using imread and managing figures and stuff, but I'm just doing (for example)
matrix = rand(20);
colormap(winter);
image(matrix, 'CDataMapping', 'scaled');
or
matrix = rand(20);
imwrite(matrix, winter(256), 'filename.png');
Is there some way to call the image function such that it doesn't display a figure window and then gets saved to a file? Something analogous to calling imshow and then savefig in matplotlib.
Just do this:
matrix = rand(20);
f = figure('visible', 'off');
colormap(winter);
image(matrix, 'CDataMapping', 'scaled');
print(f, '-dpng', 'filename.png');

Adding to MATLAB gif images using imwrite() (white images beyond certain point)

I've tried finding the answer to this all over the place, but I've had no luck so far. You see, I've ran into a problem where I'm generating gif files by appending images into a single gif using a for loop. But after a while, imwrite seems to stop actually adding the images into the gif file - it just adds white spaces instead.
So, the file itself is large enough (250MB) to feasibly have the right number of images in, but when I open it and play it, once it gets past a certain point I just have white images. I'm pretty damn sure it's nothing to do with the for loop itself - I've had it print out the image file it's working on for each loop iteration and that's correct. The filenames are all correct.
It just seems to give up after a certain number of images, so my thought is that maybe there's a maximum number? I have over 50 frames that I want to stitch together.
The code snippet I'm working on:
delay_time = 0;
% for each frequency, read in the iso-contour and stitch to the plots
% together to make a gif
for counter_frequency = freq_min:freq_inc:freq_max
im_in = imread(['2D FFT, ' num2str(counter_frequency, '%3.2f') ' GHz.png'], 'png');
[imind,cm] = rgb2ind(im_in,256);
disp([num2str(counter_frequency)]);
if counter_frequency == freq_min
imwrite(imind,cm,[directory.plot.iso_plots '\Iso-frequency Animation.gif'],'gif','DelayTime',delay_time,'LoopCount',inf);
else
imwrite(imind,cm,[directory.plot.iso_plots '\Iso-frequency Animation.gif'],'gif','DelayTime',delay_time,'WriteMode','append');
end;
end;
Solved it! There was nothing wrong with my code after all - it was the program I was using to view the gif! Internet explorer is a bad idea guys. Always. Can't believe I forgot that! Found out the computer I was using for the coding had Chrome, opened it in Chrome out of curiosity - and it was fine.
Damn you, IE!

MATLAB: overwriting images using print function

I'm using the print function in MATLAB to write images of plots, something like that
print(figure(1),'-dpng','-r300',filename);
But apparently the images are not overwritten, and the original images stay. I was using saveas before, which seems to overwrite the images, but print gives me more output options. Any ideas?
UPDATE: I ended up deleting the files before the printing with a different function.
You can use this:
im = frame2im(getframe(gcf,rec)); %Grabs image of plot as an image
imsave(im, filename); %save image
That syntax may not be 100%, its a while since I've used it.
Also be aware that this isn't perfect - I remember having issues with it grabbing a grey border around the edge of the plot. Also, I think the image may be based on a matlab screenshot.... just something to be aware of
Saving figures in matlab is rather troublesome, especially if the saved image should look like the original figure.
For myself i found the solution in using export_fig.
It's one of the most downloaded fileexchange files - maybe you should give it a try:
http://www.mathworks.de/matlabcentral/fileexchange/23629-export-fig
A small introduction to export_fig can be found at:
https://github.com/ojwoodford/export_fig/blob/master/README.md

how we save multiple images by using imwrite

can anyone help me to save my resulted images by using imwrite
source = 'C:\Y\';
im_number=5;
for i=1:5
image{i}=im2double(imread([source,'Carbon_', num2str(i)],'tif'));
image{i}=double(image{i});
B{i}= Sftfun(image{i});
B{i}=uint32(B{i});
imwrite(B{i},[source,'face_', num2str(i)],'tif');
end
The problem with your code is that you are casting your image to uint32. If you are trying to save your image as a TIF file, you can only save it as 8-bit or 16-bit. Consulting the MATLAB documentation, you can only save with these two bit depths. 32-bit depths are not supported.
Consult the MATLAB documentation for more details: http://www.mathworks.com/help/matlab/ref/imwrite.html
As such, either cast the image as 8-bit or 16-bit (through im2uint8 or im2uint16), or normalize your image so that it goes from [0,1] (through im2double).
I also have some comments about your code that do need fixing for readability:
Do not save your images to a cell array called image. MATLAB has a built-in command called image which takes in a matrix and displays it to the screen for you as an image. Bear in mind this is not the same as imshow. By doing this assignment, you will shadow over the actual image command, and so any scripts that rely on this function will result in an error.
im_number seems to be an unused variable. I'm not sure what its purpose is, but I'd say it's safe to remove this statement as well.
Get rid of the following statement as you are already converting the image to a double type in the previous line:
image{i} = double(image{i});
Aside
It seems that you have asked a similar question here: save tif 32 bit images by using imwrite
This question has already been resolved in that you are not able to save 32-bit images using imwrite. However, someone in this thread has posted a workaround for you to use in MATLAB. Try using that instead of imwrite.

Resources