I want to compress 500 images which is in a folder D:\Dr.Ayush Singhal\Ph.D coding and database\compression*.jpg and save the compressed image in other folder having path D:\Dr.Ayush Singhal\Ph.D coding and database\compression\CompressQuality80\image(k).jpg.
I have written one coding in MATLAB.
The compression program is working on all the images simultaneously from the the specified path but while saving the compressed data, the code is unable to save all all the images in specified folder. The coding is
clc
clear all;
close all;
**% IMAGE RETRIVING**
dirlist=dir('D:\Dr.Ayush Singhal\Ph.D coding and database\compression\*.jpg');
NF=length(dirlist)
%f=zeros(NF,1);
for k=1:NF
fname = dirlist(k).name;
[path,name,ext] = fileparts(fname);
im=strcat(path,name,ext);
**% IMAGE COMPRESSION**
im=imread(im);
im=rgb2gray(im);
im=imresize(im,5);
im=imcrop(im,[0 0 480 640]);
whos im
[row,col]=size(im);
row=double(fix(row/8))*8;
col=double(fix(col/8))*8;
width=col;
height=row;
im=imcrop(im,[0 0 width height]);
a22=im;
%a22=im(:,:,3);
var4=a22;
a22=double(a22)-128; %%%%Remember that DCT works only data range of
-128 to %+127%%%%%%%%
fun=@dct2;
a222=blkproc(a22,[8 8],fun); %%%%Shows the DCT2 of pixel value%%%%%%
QU=quntnew13(width,height); %%%%%%Quantization table to be used%%%%%%
a2=a222./QU; %Value After dividing with Quantization table %%
r=1;
while(r<=height)
c=1;
while(c<=width)
a4=a2(r,c);
if(a4<0)
x32(r,c)=-1;
x3(r,c)=abs(a4);
else
if(a4>0)
x4(r,c)=a4;
x42(r,c)=1;
end
end
c=c+1;
end
r=r+1;
end
x3; %%%%%%%%%Negative pixel values%%%%
x4; %%%%%%%%%positive pixel values%%%%
x32=x32+x42; %%%%%%%%%Selecting only negative value as -1%%%%%
x=x3+x4; %%DCT values only posive value(Negative also in posive form)%%%
x11=mod(x,1); %%Removing Fractional part%%%%%
x111=x-x11; %Taking only integer values%%%
X2=x111;
x111=X2;
x333=x111+x11;
x33=x333.*x32;\
a21=x33.*QU;
fun1=@idct2;
x34=blkproc(a21,[8 8],fun1);
X6=x34+128;
X6=uint8(X6);
im1=X6;
*****% COMPRESSED IMAGE WRITING*****
imwrite(im1,'D:\Dr.Ayush Singhal\Ph.D coding and database\compression\CompressQuality80\image(k).jpg','quality',80);
end