Matlab - Opening more then one image extension - working with images - image

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

Related

Use imwrite to save processed images to different folder in Matlab

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

Why get_full_filename error happens in matlab?

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));

matlab imwrite new image instead of overriding it

I have function that checks for bottle cap. If there is no cap detected, it writes it as an image into folder. My problem is that if I pass different image, the old one get overridden with new one, is there a way to make new image instead of overwriting old one such as nocap0,jpg, then new one nocap1.jpg etc?
Code:
function [ ] = CHECK_FOR_CAP( image )
%crop loaction of cap
imageCROP = imcrop(image,[130 0 100 50]);
%turn to BW
imageBW=im2bw(imageCROP);
%count black pixels
answer = sum(sum(imageBW==0));
%if <250 black, save it to folder NOCAP
if answer<250
imwrite(image, 'TESTINGFOLDERS/NOCAP/nocap.jpg', 'jpg');
disp('NO CAP DETECTED');
end
UPDATE
I changed the code a bit now. Everytime I give a different image it now writes new one, BUT it overwrites the previous one aswell like so: http://imgur.com/a/KIuvg
My new code:
function [ ] = CHECK_FOR_CAP( image )
folder = 'TESTINGFOLDERS/NOCAP';
filePattern = fullfile(folder, '/*.*');
ImageFiles = dir(filePattern);
%crop loaction of cap
imageCROP = imcrop(image,[130 0 100 50]);
%turn to BW
imageBW=im2bw(imageCROP);
%count black pixels
answer = sum(sum(imageBW==0));
%if <250 black, save it to folder NOCAP
if answer<250
a = length(ImageFiles)-1;
for j = 1:a
baseFileName = [num2str(j),'.jpg'];
filename = fullfile(folder,baseFileName);
if exist(filename,'file')
imwrite(image,filename);
end
imwrite(image, fullfile(filename));
end
disp('NO CAP DETECTED');
end
You write
for j = 1:a
baseFileName = [num2str(j),'.jpg'];
filename = fullfile(folder,baseFileName);
if exist(filename,'file')
imwrite(image,filename);
end
imwrite(image, fullfile(filename));
end
This means that whenever you find a file, you overwrite it. Then you overwrite it again. You do this for as many files as exist (a comes from some dir you do on your folder). What you want is the opposite: find one that does not exist. Something like this:
j = 0;
while true
j = j + 1;
baseFileName = [num2str(j),'.jpg'];
filename = fullfile(folder,baseFileName);
if ~exist(filename,'file')
break
end
end
imwrite(image, fullfile(filename));
This could be further shortened (e.g., by looping while exist(...)) but it conveys the idea...

Transferring a directory contains images in RGB to grayscale

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.

Wand equivalent of ImageMagick "convert -append"

I would like to write the equivalent of
convert left.jpg right.jpg +append ouput.jpg
I found something like it in another post:
files = glob('*.jpg')
with Image() as orig: # create empty Image object
for f in files:
page = Image(filename=f)
orig.sequence.append(page)
orig.save(filename='result.pdf')
and changed it to
with Image() as orig: # create empty Image object
page = Image(filename='left.jpg'); orig.sequence.append(page)
page = Image(filename='right.jpg'); orig.sequence.append(page)
orig.save(filename='output.jpg')
but the output file just shows the first file, rather than a file with the images side-by-side.
My first attempt was completely wrong, it probably makes an animated image. Provided the two images are the same size, this will do it:
with Image() as blankimage:
with Image(filename = 'imageA.tif') as imageA:
w = imageA.width; h = imageA.height
with Image(filename = 'imageB.tif') as imageB:
blankimage.blank(w*2, h)
blankimage.composite(imageA, 0, 0)
blankimage.composite(imageB, w, 0)
blankimage.save(filename = 'output.tif')

Resources