How to do this without blkproc function..?

B = blkproc(I,[8 8],'P1*x*P2',T,T');
B2 = blkproc(B,[8 8],'P1.*x',mask);
% I have this code..
%I want to replace blkproc without using that fuction.. And the code is at follows.
%
%
m = 8;
n = 8;
p =0;
q = 0;
NColBlocks = 0;
for Colnum = 1: n : size(I,2)
NColBlocks = NColBlocks + 1;
NRowBlocks = 0;
for Rownum = 1: m : size(I,1)
NRowBlocks = NRowBlocks + 1;
fun = T*T';
B_temp{NRowBlocks, NColBlocks} = ...
fun(I(Rownum - p: Rownum + m - 1 + p, ...
Colnum - q: Colnum + n - 1 + q));
end
end
B = cell2mat(B_temp);
% But it created error, So, how to correct it..

4 Comments

Nimisha - what is the error? Please include the error message and line number that the error corresponds to. As well, please format all of you code and not just some of it. Highlight all of the code and press the {} Code button.
You have the line
B = cell2mat(B_temp);
but nowhere is B_temp defined. As well, what is T and why do you treat the multiplication of T with it's transpose as some sort of function that does not return anything at
fun(I(Rownum - p: Rownum + m - 1 + p, ...
Colnum - q: Colnum + n - 1 + q));
You may want to review your code line by line and add comments where appropriate. That may help you to figure out where you are going wrong.
Hello, I have added the line for
B_temp
But what is the error? Is it occurring because the number of rows and columns in your input image I are not (evenly) divisible by eight (your m and n) or is it because of the evaluation of fun? What is T, and what are you attempting with
fun(I(Rownum - p: Rownum + m - 1 + p, ...
Colnum - q: Colnum + n - 1 + q));
My understanding, is that fun is a function handle that performs an operation on the block of data. See blokproc fun input arg for details. And yet you are using a matrix instead? Please add some context surrounding this code.
I = imread('scene.jpg');
I = I(:,:,3);
%I = im2double(I);
T = dctmtx(8);
B = blkproc(I,[8 8],'P1*x*P2',T,T');
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 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];
B2 = blkproc(B,[8 8],'P1.*x',mask);
I2 = blkproc(B2,[8 8],'P1*x*P2',T',T);
imshow(I), figure, imshow(I2)
% In this program, when i run it, as a output it gives grayscale image..
% I want coloured image because my input is coloured image..?please modify my code if possible.

Sign in to comment.

 Accepted Answer

Nimisha - it seems as if you are attempting to do some sort of JPG compression (given the use of dctmtx on the image) so try the following
I = imread('scene.jpg');
I = double(I(:,:,3)); % note the conversion to double (diff from im2double)
T = dctmtx(8); % T is of double data type, so that is y I must be double too
m = 8; % block size
n = 8;
B_temp = {};
numCols = size(I,2); % use these rather than access size repeatedly
numRows = size(I,1);
NColBlocks = 0;
for col = 1:n:numCols
NColBlocks = NColBlocks + 1;
NRowBlocks = 0;
% determine where in I we will be extracting the column data
% colStrt will be the starting column (and will always be valid)
colStrt = col;
% colStop will be the end or stopping column and is either n greater than
% colStart (less one) or is the last column in the image (to avoid indexing
% errors)
colStop = min(col+n-1,numCols);
for row = 1:m:numRows
NRowBlocks = NRowBlocks + 1;
% create an empty block of zeros
blk = zeros(m,n);
% determine where in I we will be extracting the row data
% rowStrt will be the starting row (and will always be valid)
rowStrt = row;
% rowStop will be the end or stopping row and is either m greater than
% rowStart (less one) or is the last row in the image (to avoid indexing
% errors)
rowStop = min(row+m-1,numRows);
% extract the block or as much of it as we can
blk(1:rowStop-rowStrt+1,1:colStop-colStrt+1) = ...
I(rowStrt:rowStop,colStrt:colStop);
% do the transformation on the 8x8 block and save it to the cell array
B_temp{NRowBlocks, NColBlocks} = T*blk*T';
end
end
% convert the transformed blocks in the cell array to a matrix
% note that the matrix B may be larger than I due to the zero padding
B = cell2mat(B_temp);
The above code should do what you want for B = blkproc(I,[8 8],'P1*x*P2',T,T');, though as Sean indicated, why not use blockproc instead?
Note how if the number of rows and/or columns in the image are not evenly divisible by eight, we use an 8x8 block of zeros and populate that with as much of the data as we can (so we are zero padding the last block in the final column and in the final row). This means that B will be larger than I because of the additional padding. This is to avoid the Index exceeds matrix dimensions. error that you may have been observing.
You should be able to modify the above to perform the additional transformations on the 8x8 blocks (the application of the mask (though you may have to reshape this to an 8x8 matrix), and the reverse transformation T'*blk*T*.

13 Comments

Thanks Geoff..
I got it.. But still my work not finished. I am doing image compression. And code works well.. But as a result i am getting, gray scale image. But i want coloured image as a output as my input image is coloured. So, how it is possible. Currently i am using the blkproc command program.
If your code is similar to what you posted above, then you are only considering the blue channel
I = double(I(:,:,3));
You have converted the mxnx3 I matrix to an mxnx1 matrix. That is why you are only getting output as grey scale. You probably need to iterate over each of the three dimensions as
myImage = imread('scene.jpg');
if ndims(myImage)==3
numClrDims = size(myImage,3);
else
numClrDims = 1;
end
for k=1:numClrDims
I = double(myImage(:,:,k));
% do your block processing stuff (compression, etc.)
% update B
B(:,:,k) = cell2mat(B_temp);
end
Try doing something similar to the above.
% Can you write the whole code by combining..
% Because i merged, but cant getting results..What is the problem can't trace.!
% I used the following code :
myImage = imread('scene.jpg');
if ndims(myImage)==3
numClrDims = size(myImage,3);
else
numClrDims = 1;
end
for k=1:numClrDims
I = double(myImage(:,:,k));
T = dctmtx(8);
B = blkproc(I,[8 8],'P1*x*P2',T,T');
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 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];
B2 = blkproc(B,[8 8],'P1.*x',mask);
I2 = blkproc(B2,[8 8],'P1*x*P2',T',T);
B(:,:,k) = cell2mat(I2);
end
imshow(B)
Nimisha - the third parameter to the blkproc function should be a function handle that processes a block. So you could define it as
func = @(x)T*x*T';
and then call the blkproc as
B = blkproc(I,[8 8],func);
Unfortunately, I don't have this function so can't test it out, but try it and see.
myImage = imread('scene.jpg');
if ndims(myImage)==3
numClrDims = size(myImage,3);
else
numClrDims = 1;
end
for k=1:numClrDims
I = double(myImage(:,:,k));
T = dctmtx(8);
func = @(x)T*x*T';
B = blkproc(I,[8 8],func);
%B = blkproc(I,[8 8],'P1*x*P2',T,T');
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 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];
B2 = blkproc(B,[8 8],'P1.*x',mask);
func = @(x)T*x*T';
I2 = blkproc(B2,[8 8],func);
%I2 = blkproc(B2,[8 8],'P1*x*P2',T',T);
B(:,:,k) = cell2mat(I2);
end
imshow(B)
% This shows following error..
Cell contents reference from a non-cell array object.
Error in cell2mat (line 43) cellclass = class(c{1});
Error in Untitled3 (line 26) B(:,:,k) = cell2mat(I2);
What is I2? Put a breakpoint at this line and check the class type of this object. The error message is telling you that the input is not a cell array, so perhaps I2 does not need to be converted from a cell array to a matrix.
Cant understand.! As well searched about how to put breakpoint. But cant solve.. Please edit my complete script if possible.
Try
B(:,:,k) = I2;
See if that fixes it.
I tried it. It shows blue coloured image, as well its size increases rather than decrease.!
Actually my program is for image compression and it works well for color image input and gray image output..
Currently i require color image as output. And it not works well..
Nimisha - what do you mean by as well its size increases rather than decrease? The size of what variable (?) increases?
Thanks Geoff :)
My whole problem got solved.. The code is as follows :
I1 = imread('scene.jpg');
I = I1(:,:,1);
I = im2double(I);
T = dctmtx(8);
B = blkproc(I,[8 8],'P1*x*P2',T,T');
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 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];
B2 = blkproc(B,[8 8],'P1.*x',mask);
I2 = blkproc(B2,[8 8],'P1*x*P2',T',T);
I = I1(:,:,2);
I = im2double(I);
T = dctmtx(8);
B = blkproc(I,[8 8],'P1*x*P2',T,T');
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 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];
B2 = blkproc(B,[8 8],'P1.*x',mask);
I3 = blkproc(B2,[8 8],'P1*x*P2',T',T);
I = I1(:,:,3);
I = im2double(I);
T = dctmtx(8);
B = blkproc(I,[8 8],'P1*x*P2',T,T');
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 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];
B2 = blkproc(B,[8 8],'P1.*x',mask);
I4 = blkproc(B2,[8 8],'P1*x*P2',T',T);
L(:,:,:)=cat(3,I2, I3, I4);
This L gives me desired output.
Sorry, I am very amateur in MATLAB. Can anybody please tell me what exactly 'P1*x*P2' and 'P1.*x' here?

Sign in to comment.

More Answers (1)

Why don't you want to use blkproc (or blockproc)?
You could always use two for-loops to do this.

Categories

Find more on Scripts in Help Center and File Exchange

Asked:

on 26 Sep 2014

Commented:

on 29 Mar 2017

Community Treasure Hunt

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

Start Hunting!