need help with accessing every block of image for calculating svd of these blocks
1 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
Walter Roberson
2017-9-17
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 个评论
Walter Roberson
2017-9-18
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!