what is wrong with the blockproc function in my code?

5 visualizaciones (últimos 30 días)
Hadeer tawfik
Hadeer tawfik el 19 de Sept. de 2016
Editada: Hadeer tawfik el 20 de Sept. de 2016
I am trying to write the function 'replace' that accesses every block in the image, keeps the first 2000 cooefficient and sets the rest of them to zero. but every time i do that i find this error message:
Error using blockproc>parse_inputs (line 997)
Invalid block function. BLOCKPROC expects the user function, FUN, to be a valid function handle.
Error in blockproc (line 219)
[source,fun,options] = parse_inputs(source,block_size,fun,varargin{:});
Error in blk2 (line 6)
J3 = blockproc(I3,[8 8],'dct2',f);
here is the function f code:
function features = replace(block)
for( i = 2000:length(block.data) )
% set each element to 0
block.data(i) = 0
end
%and the DCT and blockproc code:
I3 = imread('process/13.jpg');
figure;
imshow(I3);
f = @(block)replace(block.data);
J3 = blockproc(I3,[8 8],'dct2',f);
figure
imshow(J3);
imshow(log(abs(J3)),[]), colormap(jet), colorbar
J3(abs(J3) < 5) = 0;
%B=J;
%B(1:1) = 0;
[M N]=size(J3);
fun3=zigzag(J3);
in4=izigzag(fun3,M,N);
f = @(block)replace(block.data);
J22 = blockproc(in4,[8 8],'idct2',f);
figure
imshow(J22,[0 255]);
figure
imshow(log(abs(J22)),[]), colormap(jet), colorbar

Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de Sept. de 2016
blockproc requires a function handle as its third parameter. You are passing the string 'dct2' or 'idct2' in that position. Those are not function handles.
Perhaps what you want is
f = @(block)replace(dct2(block.data));
J3 = blockproc(I3, [8 8], f);
but keep in mind that each 8 x 8 block will have only 64 entries. So perhaps what you want is
f = @(block)dct2(block.data);
J3 = replace( blockproc(I3, [8 8], f) );
and similar change for idct2
  3 comentarios
Walter Roberson
Walter Roberson el 20 de Sept. de 2016
Change your replace function to
function features = replace(block)
features = block;
features(2000:end) = 0;
However, remember that with your code of invoking replace() for each block, your blocks are only going to be 8 x 8 so replace will have nothing to do.
It does not make sense to zero out all except the first DCT coefficients of a block unless the dct2 of the block returns an array with at least 2000 elements. dct2() returns as output an array the same size as its input, so instead of passing in 8 x 8 blocks you would need to modify that to have at least 2000 elements for it to make sense to zero all but the first 2000 elements. Perhaps you want to apply the dct2 to the entire image? Or perhaps you want to use 64 x 64 blocks, as those would have 4096 entries so you would be zeroing out slightly more than half of them?
I would also suggest that you rethinking what the "first" 2000 coefficients are. In MATLAB, the elements are counted down columns from the beginning, so zeroing out after the first 2000 would involve leaving intact the first 31 columns and the first 16 rows of the 32nd column, and zeroing out the last 16 rows of the 32nd column and all of the 33rd to 64th column. Does that make sense to you?
If you are thinking in terms of what JPEG does, remember that JPEG does a zig-zag unraveling to produce a vector from each block, so the "first" 2000 coefficients for JPEG would be the first 2000 along that zig-zag pattern. The zeros would be distributed in a much different pattern than what you are currently using.
Hadeer tawfik
Hadeer tawfik el 20 de Sept. de 2016
Editada: Hadeer tawfik el 20 de Sept. de 2016
Thank you so much for your detailed answer that i was desperately needing,I was thinking about my image as a group of blocks , then i apply the DCT on each block and since the coefficients of high energy that i need likely are located at the upper left of each block, so i was thinking about zeroing out the coefficients that are far from this place, so i was thinking about trying zeroing out what comes after the 2000 ith element as a first try and then i see the result,so as u mentioned above, i should use 64X64 blocks to achieve this, For the zigzag part, Im trying in this code to take out the best DCT coefficients that describes the image as a feature vector that will be an input to neural network classifier , so i applied the zigzag on the image, then tried to keep only the first 1000 cooefficient or such number of coefficients till i find that the IDCT produces the image with good enough quality,but this didn't work as when i did this parts from the image disappeared, so i will try this approach that i was asking about to keep only the best coefficients and see what i will get.I will also apply the dct on the whole image and compare the result, Thank you soooo much again

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by