简体   繁体   中英

Divide an image into non-overlapping blocks and applying the 2D DWT on each block

I am working on creating an image splicing detection software so I need to divide the image into non-overlapping blocsk and apply Discrete Meyer Wavelet Transform on each block of the image

I have tried the blockproc function to do that but I got no result:

I = imread('pears.png');

fun = @(block_struct)...
    dwt2(block_struct.data,'dmey');

C = blockproc(I,[64 64],fun);

So how can I access the [cA,cH,cV,cD ] of dwt2 using the above code?

blockproc assumes that you are outputting an actual image. You cannot use this for multiple outputs. If you truly want this to work with blockproc , you will unfortunately need to call blockproc four times, with each time extracting the different set of coefficients for the directions. Also note that the 2D DWT only works for grayscale images, so you need to convert to grayscale before actually doing any processing. The pears image you've chosen is a colour / RGB image.

I'd like to reference this post on how to select the N th output given an input function: How do I get the second return value from a function without using temporary variables? . You will need to save this code to a file called nth_output.m , which allows you to programatically extract all output variables from a function and choose only one output.

function value = nth_output(N,fcn,varargin)
  [value{1:N}] = fcn(varargin{:});
  value = value{N};
end

Simply omitting the extra output arguments when you call the function only gives you the first output, which is what your blockproc code is doing. Once you do that, it's a matter of creating 4 anonymous functions to capture each output from dwt2 , and running blockproc 4 times. Make sure you specify which output you want for each of the anonymous functions, so 1 up to 4 and you simply provide a handle to the function you want to run in addition to the input arguments that go into the function.

Therefore, try something like this:

I = rgb2gray(imread('pears.png'));
fun1 = @(block_struct) nth_output(1, @dwt2, block_struct.data,'dmey');
fun2 = @(block_struct) nth_output(2, @dwt2, block_struct.data,'dmey');
fun3 = @(block_struct) nth_output(3, @dwt2, block_struct.data,'dmey');
fun4 = @(block_struct) nth_output(4, @dwt2, block_struct.data,'dmey');
I = rgb2gray(I);
cA = blockproc(I,[64 64],fun1);
cH = blockproc(I,[64 64],fun2);
cV = blockproc(I,[64 64],fun3);
cD = blockproc(I,[64 64],fun4);

cA , cH , cV , and cD contain the DWT coefficients you need for each set of directions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM