MATLAB: Need help with accessing every block of image for calculating svd of these blocks

image processingImage Processing Toolbox

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.

Best 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.
Related Question