0

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
lokusking
  • 7,396
  • 13
  • 38
  • 57
  • You are rewriting the file`image(k).jpg` over and over again, is this correct? or what you want is to create several files `image(1).jpg`, `image(2).jpg`, `image(3).jpg` and so on? – Noel Segura Meraz Jul 29 '16 at 06:56
  • Yes i want to different name for different images .Actually the value of k in image(k) is simplify a image name. Every time the images are renamed as image(k) and i am getting only the last imwrite image. Is their is syntax to save the multiple images in the same folder. – Swagota Bera Jul 31 '16 at 08:42

1 Answers1

0

Your problem is that you are saving every file with the same name image(k), so at the end of the for loop you will only have the last iteration, in order to save every image, you will have to assign a different name in each iteration, there are several ways to achieve this. One simple solution would be:

file_name=sprintf('D:\Dr.Ayush Singhal\Ph.D coding and database\compression\CompressQuality80\image(%d).jpg',k);

imwrite(im1,file_name,'quality',80);

This will create files image(1), image(2), image(3), etc. One for every iteration in the for loop.

Noel Segura Meraz
  • 2,265
  • 1
  • 12
  • 17