图像编写在matlab编码图像压缩

时间:2016-07-29 05:26:54

标签: image matlab image-compression

我想要压缩文件夹D:\ Dr.Ayush Singhal \ Ph.D编码和数据库\ compression * .jpg中的500张图像并将压缩图像保存在具有路径D的其他文件夹中:\ Dr.Ayush Singhal \ Ph.D编码和数据库\ compression \ CompressQuality80 \ image(k).jpg。

我在MATLAB中编写了一个编码。

压缩程序正在同时处理来自指定路径的所有图像 保存压缩数据时,代码无法保存指定文件夹中的所有图像。 编码是

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

1 个答案:

答案 0 :(得分:0)

您的问题是您保存的每个文件都具有相同的名称<script type="text/javascript"> google.charts.load('current', {'packages':['line']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('number', 'Ugenr.'); data.addColumn('number', 'Anbefalet pris'); data.addColumn('number', 'Nuværende pris'); data.addRows([ [1, 37.8, 41.8], [2, 6.6, 32.4], [3, 25.4, 25.7], [4, 11.7, 10.5], [5, 11.9, 6.6], [6, 8.8, 7.7], [7, 7.6, 9.6], [8, 12.3, 10.6], [9, 6.6, 14.8], [10, 12.8, 11.6], [11, 5.3, 4.7], [12, 6.6, 5.2], [13, 4.8, 3.6], [14, 4.2, 3.4], [15, 8.8, 31.8], [16, 30.9, 6.6], [17, 25.4, 25.7], [18, 11.7, 10.5], [19, 11.9, 10.4], [20, 6.6, 7.7], [21, 7.6, 9.6], [22, 12.3, 10.6], [23, 16.9, 14.8], [24, 12.8, 11.6], [25, 5.3, 4.7], [26, 6.6, 5.2], [27, 4.8, 3.6], [28, 4.2, 6.6], [29, 37.8, 41.8], [30, 30.9, 32.4], [31, 25.4, 25.7], [32, 11.7, 10.5], [33, 11.9, 10.4], [34, 8.8, 7.7], [35, 7.6, 9.6], [36, 12.3, 10.6], [37, 16.9, 14.8], [38, 12.8, 11.6], [39, 5.3, 4.7], [40, 6.6, 5.2], [41, 6.6, 3.6], [42, 4.2, 6.6], [43, 4.2, 3.4], [44, 37.8, 41.8], [45, 30.9, 32.4], [46, 25.4, 25.7], [47, 11.7, 10.5], [48, 11.9, 6.6], [49, 8.8, 7.7], [50, 7.6, 9.6], [51, 6.6, 10.6], [52, 16.9, 14.8] ]); var options = { chart: { title: 'Anbefalede og nuværende ugepriser', subtitle: 'anbefalede priser = blå, nuværende priser = rød', hAxis: {maxValue: 52} }, legend: { position:'none' }, height: 500, explorer: { actions: ['dragToZoom', 'rightClickToReset'] } }; var chart = new google.charts.Line(document.getElementById('linechart_material')); chart.draw(data, options); } $(window).resize(function(){ drawChart(); }); </script> ,因此在for循环结束时您将只进行最后一次迭代,为了保存每个图像,您必须分配在每次迭代中有不同的名称,有几种方法可以实现这一点。一个简单的解决方案是:

image(k)

这将创建文件file_name=sprintf('D:\Dr.Ayush Singhal\Ph.D coding and database\compression\CompressQuality80\image(%d).jpg',k); imwrite(im1,file_name,'quality',80); image(1)image(2)等。一个用于for循环中的每次迭代。