need help with accessing every block of image for calculating svd of these blocks

Hi everyone, i have done coding for my project related to image forensics. But now i am stuck at finding svd of each block of the image. Here is my code snippet.
clc;
clear;
close all;
%%read image
im = [];
[FileName,PathName] = uigetfile({'*.png';'*.jpeg';'*.bmp';'*.gif';'*.jpg'},'Select the Image file');
if~(isequal(FileName,0)|| isequal(PathName,0))
m= fullfile(PathName,FileName);
im = imread(m);
end
img = im2double(rgb2gray(im));
[f1, f2] = size(img);
%%1- Apply Stationary wavelet transform up to specified level "L" to the gray image.
disp('1-Applying SWT to grayscale image ');
disp('1-Wavelet.....');
LL{1} = img;
k = 1;
[LL{k}, LH{k}, HL{k}, HH{k}] = swt2(LL{k},1,'db6');
%%2- Divide the image into overlapping blocks.
disp('2- Dividing the image into overlapping blocks of size 16x16');
block_size = 16;
patches = im2col(LL{1}(:,:,1),[block_size,block_size],'sliding'); %breaks image into blocks
[m1, n1] = size(patches);
num_blocks = (1:n1);
%%3- Apply SVD to each block
disp('3- Feature extraction by computing the SVD Transform of each 16x16 window');
so i am no getting how to access each block and apply SVD to each block. Plz help me if anybody knows regarding this.

10 Comments

We do not understand why you did not use blockproc()
@TUSHAR MURATKAR: because it would solve the problem.
@jan simon, but how will it solve the problem. Can you explain it by example.
@TUSHAR MURATKAR: Please start with reading the documentation: https://www.mathworks.com/help/images/ref/blockproc.html. Then try it by your own and ask a specific question in the case of problems. I assume you want something like this:
B = blockproc(img, [16, 16], @svd)
@jan simon, i am trying to do SVD on each block of the image but not getting the diagonal matrix. Any help in this topic is highly appreciated.
You should use blockproc() like everyone is saying. I attach some demos that you can adapt as needed.
@image analyst, i am using blockproc only but when i apply SVD to each block i am not getting diagonal matrix of singular value. Can you help me in this.
@image analyst, can you tell me, out of two demo files which one is for overlapping blocks

Sign in to comment.

 Accepted Answer

function [Ses, AllSes] = svdimg(IMG)
if ndims(IMG) ~= 2
error('Cannot handle RGB images');
end
AllSes = blockproc(IMG, [8 8], @(block) svdd(double(block.data)), 'Border', [4 4], 'Trim', false);
[r, c] = size(AllSes);
rb = floor(r/16);
lor = mod(r, 16);
cb = floor(c/16);
loc = mod(c, 16);
Ses = mat2cell(AllSes, [16 * ones(1,rb), lor], [16 * ones(1,cb), loc]);
end
function S = svdd(M)
[U, S, V] = svd(M);
end
First output will be a cell array of SVD diagonals of 16 x 16 sliding blocks overlapping by 4 on each edge. Second output is the matrix of results not broken up into blocks.
Note: zero padding is used at the edges. For example the first block processed might look like
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 156 159 158 155 158 156 159 158 157 158 158 159
0 0 0 0 160 154 157 158 157 159 158 158 158 160 155 156
0 0 0 0 156 159 158 155 158 156 159 158 157 158 158 159
0 0 0 0 160 154 157 158 157 159 158 158 158 160 155 156
0 0 0 0 156 153 155 159 159 155 156 155 155 157 155 154
0 0 0 0 155 155 155 157 156 159 152 158 156 158 152 153
0 0 0 0 156 153 157 156 153 155 154 155 157 156 155 156
0 0 0 0 159 159 156 158 156 159 157 161 162 157 157 159
0 0 0 0 158 155 158 154 156 160 162 155 159 161 156 161
0 0 0 0 155 154 157 158 160 160 159 160 158 161 160 160
0 0 0 0 154 157 157 157 156 155 159 154 159 158 161 158
0 0 0 0 152 150 155 154 152 156 157 156 157 154 157 159
Here 4 rows of padding left and top have been used automatically.

4 Comments

@WALTER , thanks for the answer. I have one doubt. Is this code satisfy the requirement " divide the image into overlapping blocks and apply SVD to each block."
@walter, but your code is not satisfying the equation (M-B+1)*(N-B+1). Because theory says that when we divide the image into overlapping blocks then output must satisfy the equation (M-B+1)*(N-B+1), where M,N are size of input image and B is the block size.
The line
AllSes = blockproc(IMG, [8 8], @(block) svdd(double(block.data)), 'Border', [4 4], 'Trim', false);
divides the images into blocks that are 4+8+4=16 by 4+8+4=16 and slides over by 8 . svd is taken for each block.
"Because theory says that when we divide the image into overlapping blocks then output must satisfy the equation (M-B+1)*(N-B+1), where M,N are size of input image and B is the block size."
That theory is incomplete, valid only if blocks are slid over by 1 each time; it also does not take into account partial blocks.
It is not possible to use blockproc() to slide over by an odd amount while using an even block size. You can call
AllSes = blockproc(IMG, [1 1], @(block) svdd(double(block.data)), 'Border', [7 7], 'Trim', false);
that would create blocks that were 7+1+7 = 15 by 7+1+7 = 15 by 15. If you need to have an even blocksize but slide by an odd amount, some code changes would have to be made to the approach.
@walter, what changes i will have to do in your code to get (M-B+1)*(N-B+1) overlapped blocks and apply SVD to each block.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!