Find error in Prompt user to select image file
% Prompt user to select image file
[filename, pathname] = uigetfile('*.bmp', 'Open image');
if isequal(filename, 0) || isequal(pathname, 0)
disp('Image input canceled.');
X = [];
map = [];
else
[X, ~] = imread(fullfile(pathname, filename));
end
% Check if image is grayscale or color
if ndims(X) == 3
img = rgb2gray(X);
else
img = X;
end
?fine wavelet filter
wavelet_filter = 'haar';
% Perform 2D discrete wavelet transform
[cA, cH, cV, cD] = dwt2(img, wavelet_filter);
% Perform ZigZag scan on each subband
z_ca = ZigZag(cA);
z_ch = ZigZag(cH);
z_cv = ZigZag(cV);
z_cd = ZigZag(cD);
% Concatenate all ZigZag scanned subbands
z_img = [z_ca, z_ch, z_cv, z_cd];
% Get size of ZigZag scanned image
%[m, n] = size(z_img);
%Totalcount = m*n;
%size of the image
[m,n]=size(img);
Totalcount=m*n;
%variables using to find the probability
cnt=1;
sigma=0;
%computing the cumulative probability.
for i=0:255
k=img==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
%Huffman Encodig
encoded_img = huffmanenco(z_img,dict);
%Huffman Decoding
dhsig1 = huffmandeco(encoded_img,dict);
%convertign dhsig1 double to dhsig uint8
dhsig = uint8(dhsig1);
%vector to array conversion
dec_row=sqrt(length(dhsig));
dec_col=dec_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)=dhsig(vec_si);
arr_col = arr_col+1;
vec_si = vec_si + 1;
end
arr_row = arr_row+1;
end
%converting image from grayscale to rgb
[deco, map] = gray2ind(back,256);
RGB = ind2rgb(deco,map);
%figure,imshow(RGB)
figure
subplot(1,2,1);imshow(a);title('Image');
subplot(1,2,2);imshow(RGB);title('Image Compression');
imwrite(RGB,'F:bmw321.JPG');
?fine ZigZag scan function
function [z_img] = ZigZag(in)
N = size(in, 1);
z_img = zeros(1, N^2);
i = 1;
for s = 2 : N + 1
for x = 1 : s - 1
y = s - x;
if mod(s, 2) == 0
z_img(i) = in(x, y);
else
z_img(i) = in(y, x);
end
i = i + 1;
end
end
for s = N + 1 : 2 * N - 1
for x = s - N : N
y = s - x;
if mod(s, 2) == 0
z_img(i) = in(x, y);
else
z_img(i) = in(y, x);
end
i = i + 1;
end
end
end
All reactions:
11