Filter löschen
Filter löschen

Size of compressed image is larger than image .... How to solve this problem?

3 Ansichten (letzte 30 Tage)
The following code works well, but the size of compressed image is larger than the image. Need some assistance....
%clearing all variableas and screen
clear all;
close all;
clc;
%Reading image
a=imread('encrypted.jpg');
figure,imshow(uint8(a))
%size of the image
[m,n]=size(a);
Totalcount=m*n;
%variables using to find the probability
cnt=1;
sigma=0;
%computing the cumulative probability.
for i=0:255;
k=a==i;
count(cnt)=sum(k(:))
%pro array is having the probabilities
pro(cnt)=count(cnt)/Totalcount;
sigma=sigma+pro(cnt);
cumpro(cnt)=sigma;
cnt=cnt+1;
end;
%Symbols for an image
symbols = [0:255];
%Huffman code Dictionary
dict = huffmandict(symbols,pro);
%function which converts array to vector
vec_size = 1;
for p = 1:m
for q = 1:n
newvec(vec_size) = a(p,q);
vec_size = vec_size+1;
end
end
%Huffman Encodig
hcode = huffmanenco(newvec,dict);
%vector to array conversion
enco_row=sqrt(length(hcode));
enco_col=enco_row;
%variables using to convert vector 2 array
arr_row = 1;
arr_col = 1;
vec_si = 1;
for x = 1:m
for y = 1:n
back(x,y)=hcode(vec_si);
arr_col = arr_col+1;
vec_si = vec_si + 1;
end
arr_row = arr_row+1;
end
imwrite(back,'encoded5.jpg')
%end of the huffman coding

Akzeptierte Antwort

Image Analyst
Image Analyst am 12 Nov. 2021
Yes, I think that may be possible. If the image has lots of areas with rapidly changing values and few areas where the are long runs of the same value, then that type of encoding may take more bytes than the original image. What happens if the image is just a uniform patch of color? What is the original file size, the final file size, and the actual image size in bytes once read in with imread (the full, uncompressed size)?
[rows, columns, numberOfColorChannels] = size(a)
d = dir('encrypted.jpg');
pixelCount = rows * columns
totalBytes = pixelCount * numberOfColorChannels
fprintf('The image on disk has %d bytes.\n', d.bytes);
fprintf('The uncompressed image in memory has %d pixels and %d bytes.\n', pixelCount, totalBytes);
Also, never do this:
[m,n]=size(a);
Totalcount=m*n;
because n is the number of columns multiplied by the number of color channels. Why?
Do it like this instead
[rows, columns, numberOfColorChannels] = size(a);
pixelCount = rows * columns
totalBytes = pixelCount * numberOfColorChannels
and please don't use single letter variables names because it makes code look like an impenetrable alphabet soup of code that is difficult to maintain. Use descriptive variable names, like rgbImage instead if the badly named "a", rows instead of m, etc.

Weitere Antworten (1)

Jan
Jan am 12 Nov. 2021
If the original image was written with a low quality, the default Quality=75 of imwrite might increase the file size.

Kategorien

Mehr zu Denoising and Compression finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by