Reading the content of the directory and for every JPEG image converting to grey scale
srcFiles = dir('R:\...\images - Copy\*.jpeg');
for i = 1 : length(srcFiles)
filename = srcFiles(i).name;
try
I = imread(filename);
catch ME
continue
end
IGrey = rgb2gray(I);
imshow(IGrey);
pathOfNewFile = strcat(pathOfGSFolder,filename,'jpeg');
imwrite(IGrey,pathOfNewFile,'jpeg');
end
'R:\...\images - Copy\' is not a valid path. A folder cannot be called ...
When trying to execute the first line you will probably get an error and the variable srcFiles will be empty, so the length of this variable will be 0 and therefore the loop will not execute.
Related
I need help with saving processed images to different folder with imwrite.
Currently, I can save all processed images to a single folder.
Img_filename=ls('C:\Users\User\Desktop\Guanlin_CNN1D\CNN1D\GF_BSIF\*.jpg');
imageSize = size(Img_filename);
Img_filenum = size(Img_filename,1);
for img=1:Img_filenum
img_temp=double((rgb2gray(imread(Img_filename(img,:)))));
-----------processing--------
count = count+1;
FileName = fullfile('C:\Users\User\Desktop\Guanlin_CNN1D\CNN1D\GF_BSIF\folder_1',sprintf('%03d_circle_cropped.jpg',count));
imwrite(MM, FileName)
end
However, I have 1000 different images in 1 folder and after processing, it will generate 500 images and I want to save the first 500 processed images into folder_1. And the second 500 processed images to folder_2 and the third 500 images to folder_3 and so on...
How to re-write the imwrite function?
Thank you!
The root folder I used here is named Image_Folder and resides on the desktop. The output folders are named Folder_1, Folder_2 and Folder_3 and also reside on the desktop. I used two nested loops to control the saving of the images. The outer loop controls which folder to write to and the inner loop controls the writing images 1 through 500. The variable Image_File_Names can be used to access the input image file names.
%Folder holding the 1000 images%
Image_Path = "/Users/michael/Desktop/Image_Folder";
%Prefix path for output folders%
Export_Path = "/Users/michael/Desktop/Folder_";
%Adding path with input images%
addpath(Image_Path);
%Listing all images in folder directory%
Image_File_Names = ls(Image_Path);
Image_File_Names = split(Image_File_Names);
Number_Of_Images = length(Image_File_Names) - 1;
for Folder_Index = 1: 3
Export_Folder_Path = Export_Path + num2str(Folder_Index) + "/";
mkdir(Export_Folder_Path);
for Image_Index = 1: 500
%Grab the images as needed%
Input_Image_Index = 1; %Change this index to another variable to grab images within input folder%
Image = imread(string(Image_File_Names(Input_Image_Index)));
%***************************************%
%PROCESS IMAGE%
%***************************************%
%***************************************%
Output_File_Path = Export_Folder_Path + "Image_" + num2str(Image_Index) +".jpg";
imwrite(Image,Export_Folder_Path+num2str(Image_Index)+".jpg");
end
end
Using MATLAB version: R2019b
I want to read all images in specific folder and save it to 64*64 size jpg images.
But the error occur and I don't know why and what to do.
Pleaes help me
Error code :
File "Nikon_D70_0_19458.tif" doesn't exist
Error: imread (line 340) fullname = get_full_filename(filename);
Error: Untitled (line 13) dList(i).data = imread(dList(i).name);
My code :
clc; clear; close all;
imgPath = 'C:\Users\LG\Desktop\TIFF\dataset1\60\'; %open image path
dList = dir([imgPath '*.tif']); name = 1; %save name index
for i=1:length(dList) %open image dList(i).data = imread(dList(i).name);
dList(i).data = dList(i).data(1:256 ,1:256,:); %crop image 256*256
a = dList(i).data;
YCbCr = rgb2ycbcr(a);
Y = YCbCr(:,:,1);
Cb = YCbCr(:,:,2);
Cr = YCbCr(:,:,3);
[height,width] = size(Y);
for q=1:32:height-32
for w= 1:32:width-32
block = Y(q:q+63 , w:w+63);
Resultados='C:\Users\LG\Desktop\TIFF\training\60'; %save image path
imwrite(block, fullfile(Resultados, ['SerieN', num2str(i), '.jpg']),'Quality',60) % save image
name = name+1;
end
end
end
The field dList(i).name does not contain the full path, just the file name. You can get the full path using fullfile:
dList(i).data = imread(fullfile(imgPath, dList(i).name));
I need open and put in the same vector .jpg images and .bmp images.
I can open or .jpg or .bmp, but i need to open both.
This is my code:
image_folder = 'C:\Users\Marco\Desktop\teste';
filenames = dir(fullfile(image_folder, '*.jpg'));
total_images = numel(filenames);
for n=1:total_images
images{n} = imread(sprintf('color%03d.jpg',n));
end;
(in this example i open jpgs but i need to open .jpg And .bmp )
filenames = dir() should give you the filenames you need to cycle through; you shouldn't have to parse the bmp from jpg files. If the directory only contains images you want to you can try something like:
imageFolder = 'C:\Users\Marco\Desktop\teste\';
filenames = dir(imageFolder);
numImages = length(filenames)-2; % exclude counting '.' and '..'
images = cell(numImages,1);
n = 1;
for i = 1:length(filenames)
if filenames(i).name(1) ~= '.'
images{n} = imread([imageFolder filenames(i).name]);
n = n+1;
end
end
myPath= 'C:\Users\Prienka\Documents\MATLAB\frames';
a =dir(fullfile(myPath,'*.png'));
a(1).name
a(end).name
Im1 = imread(a(1).name);
Im2 = imread(a(end).name);
Im1 = rgb2gray(Im1);
Im2 = rgb2gray(Im2);
hn1 = imhist(Im1)./numel(Im1); %initial pixel postion
hn2 = imhist(Im2)./numel(Im2); %final pixel position
d = sum(sqrt(hn2-hn1).^2);
d=sprintf('%.0f',d);
obj = VideoReader('traffic.mp4')
x=obj.FrameRate
velocity=d/x;
velocity=sprintf('%.0f',velocity)
if velocity >= 63
msgbox('SPEED EXCEEDED' )
end
this is my code for calculating the velocity of a vehicle in a vedio... the problem is i have extracted all the movie frames in a different folder named "frames"... here whenever m trying to run the code its showing a error like this
"Error using imread (line 350)
File "001.png" does not exist.
Error in pikz (line 5)
Im1 = imread(a(1).name);"
my file name is pikz.m
In your current working directory (pwd) the file does not exist. Dir only returns relative path like '001.png'. You have to use fullfile to get the absolute path like 'C:\Users\Prienka\Documents\MATLAB\frames\001.png' to pass it to imread
I have a load of data in 100 .sdf files (labelled 0000.sdf to 0099.sdf), each of which contain a still image, and I'm trying to produce a .gif from these images.
The code I use to plot the figure are (in the same directory as the sdf files):
q = GetDataSDF('0000.sdf');
imagesc(q.data');
I've attempted to write a for loop that would plot the figure and then save it with the same filename as the sdf file but to no avail, using:
for a = 1:100
q=GetDataSDF('0000.sdf');
fh = imagesc(q.dist_fn.x_px.Left.data');
frm = getframe( fh );
% save as png image
saveas(fh, 'current_frame_%02d.jpg');
end
EDIT: I received the following errors when trying to run this code:
Error using hg.image/get
The name 'Units' is not an accessible property for an instance of class 'image'.
Error in getframe>Local_getRectanglesOfInterest (line 138)
if ~strcmpi(get(h, 'Units'), 'Pixels')
Error in getframe (line 56)
[offsetRect, absoluteRect, figPos, figOuterPos] = ...
Error in loop_code (line 4)
frm = getframe( fh );
How do I save these files using a for loop, and how do I then use those files to produce a movie?
The reason for the error is that you pass an image handle to getframe, but this function excpects a figure handle.
Another problem is that you always load the same file, and that you saveas will not work for gifs. (For saving figures as static images, maybe print is the better option?)
I tried to modify my own gif-writing loop so that it works with your data. I'll try to be extra explicit in the comments, since you seem to be starting out. Remember, you can always use help name_of_command to display a short Matlab help.
% Define a variable that holds the frames per second your "movie" should have
gif_fps = 24;
% Define string variable that holds the filename of your movie
video_filename = 'video.gif';
% Create figure 1, store the handle in a variable, you'll need it later
fh = figure(1);
for a = 0:99
% Prepare file name so that you loop over the data
q = GetDataSDF(['00' num2str(a,'%02d') 'sdf']);
% Plot image
imagesc(q.dist_fn.x_px.Left.data');
% Force Matlab to actually do the plot (it sometimes gets lazy in loops)
drawnow;
% Take a "screenshot" of the figure fh
frame = getframe(fh);
% Turn screenshot into image
im = frame2im(frame);
% Turn image into indexed image (the gif format needs this)
[imind,cm] = rgb2ind(im,256);
% If first loop iteration: Create the file, else append to it
if a == 0;
imwrite(imind,cm,video_filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,video_filename,'gif','WriteMode','append','DelayTime',1/gif_fps);
end
end
One more note: When the size of the data is the same for each plot, it makes sense to only use the plot(or in this case, imagesc) command once, and in later loop iterations replace it with a set(ah,'Ydata',new_y_data) (or in this case set(ah,'CData',q.dist_fn.x_px.Left.data'), where ah is a handle of the plot axes (not the plot figure!). This is orders of magnitude faster than creating a whole new plot in each loop iteration. The downside is that the scaling (here, the color-scaling) will be the same for each plot. But in every case that I have worked on so far, that was actually desirable.