How to remove the high frequency DCT coefficients without losing the image dimensions ?

8 次查看(过去 30 天)
Im in stuck in this part of my code, after applying the DCT on the image, I set the coefficients that have energy lower than 5 to zero , then i applied zigzag, now i want to choose only the first strong cooefficients that have high energy, the question is how to do this? since the way i tried gave me horrible result, It gave me only a quarter of the image as an output after idct2, Can you help me please to solve this ? I need to choose only the highest energy coefficients without losing the original image dimensions, is there a way?
<< input:
output:
I = imread('process/13.jpg');
figure;
imshow(I);
J = blkproc(I,[64 64],'dct2');
figure
imshow(J);
imshow(log(abs(J)),[]), colormap(jet), colorbar
J(abs(J) < 5) = 0;
%B=J;
%B(1:1) = 0;
fun=zigzag(J);
B3=fun(1:10000);
A=imresize(B3,[100,100]);
[M N]=size(A);
in1=izigzag(B3,M,N);
J2 = blkproc(in1,[64 64],'idct2');
figure
imshow(J2,[0 255]);
figure
imshow(log(abs(J2)),[]), colormap(jet), colorbar
%zigzag%
function output = zigzag(in)
% initializing the variables
%----------------------------------
h = 1;
v = 1;
vmin = 1;
hmin = 1;
vmax = size(in, 1);
hmax = size(in, 2);
i = 1;
output = zeros(1, vmax * hmax);
%----------------------------------
while ((v <= vmax) & (h <= hmax))
if (mod(h + v, 2) == 0) % going up
if (v == vmin)
output(i) = in(v, h); % if we got to the first line
if (h == hmax)
v = v + 1;
else
h = h + 1;
end;
i = i + 1;
elseif ((h == hmax) & (v < vmax)) % if we got to the last column
output(i) = in(v, h);
v = v + 1;
i = i + 1;
elseif ((v > vmin) & (h < hmax)) % all other cases
output(i) = in(v, h);
v = v - 1;
h = h + 1;
i = i + 1;
end;
else % going down
if ((v == vmax) & (h <= hmax)) % if we got to the last line
output(i) = in(v, h);
h = h + 1;
i = i + 1;
elseif (h == hmin) % if we got to the first column
output(i) = in(v, h);
if (v == vmax)
h = h + 1;
else
v = v + 1;
end;
i = i + 1;
elseif ((v < vmax) & (h > hmin)) % all other cases
output(i) = in(v, h);
v = v + 1;
h = h - 1;
i = i + 1;
end;
end;
if ((v == vmax) & (h == hmax)) % bottom right element
output(i) = in(v, h);
break
end;
end;
%inv zigzag%
function output = izigzag(in, vmax, hmax)
% initializing the variables
%----------------------------------
h = 1;
v = 1;
vmin = 1;
hmin = 1;
output = zeros(vmax, hmax);
i = 1;
%----------------------------------
while ((v <= vmax) & (h <= hmax))
if (mod(h + v, 2) == 0) % going up
if (v == vmin)
output(v, h) = in(i);
if (h == hmax)
v = v + 1;
else
h = h + 1;
end;
i = i + 1;
elseif ((h == hmax) & (v < vmax))
output(v, h) = in(i);
i;
v = v + 1;
i = i + 1;
elseif ((v > vmin) & (h < hmax))
output(v, h) = in(i);
v = v - 1;
h = h + 1;
i = i + 1;
end;
else % going down
if ((v == vmax) & (h <= hmax))
output(v, h) = in(i);
h = h + 1;
i = i + 1;
elseif (h == hmin)
output(v, h) = in(i);
if (v == vmax)
h = h + 1;
else
v = v + 1;
end;
i = i + 1;
elseif ((v < vmax) & (h > hmin))
output(v, h) = in(i);
v = v + 1;
h = h - 1;
i = i + 1;
end;
end;
if ((v == vmax) & (h == hmax))
output(v, h) = in(i);
break
end;
end;

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by