how to recombine subimages from cell2mat?

i have taken grayscale image which is converted into window size of 8x8 by using mat2cell.then later i have to get back original image from cell2mat which i am finding it difficult to do?
a=VideoReader('test.mp4 ');
b = read(a,1 );
c=rgb2gray(b );
nframe=a.NumberOfFrames ;
for i=115:116
prev_frame =read(a,i-1 );
e=rgb2gray(prev_frame );
figure
imshow(e );
pause(0.2)
current_frame =read(a,i);
h=rgb2gray(current_frame );
figure
imshow(h);
rows = 8 ;
columns = 8 ;
[H,W,~] = size(c );
szH = [repmat(fix(H/rows),1,rows )];
szW = [repmat(fix(W/columns),1,columns )]; //to split into window size of 8x8 as per my algorithm
C = mat2cell(e, szH, szW )';
D = mat2cell(h, szH, szW )';
figure
for j=1:rows*columns
subplot(rows,columns,j), imshow( C{j } ) //to plot above cells
end
figure
for j=1:rows*columns
subplot(rows,columns,j), imshow( D{j } )
end
for k=1:1*8
o=1:1*8
G=cell2mat(C(k,o)');
figure
imshow(G);at this point i am getting images which i am not able to recombine.kindly suggest something here
end
for k=1:8
o=1:8
H=cell2mat(D(k,o)');
figure
imshow(H);
end
w=std(double(G));
figure
plot(w);
b=std(double(H));
figure
plot(b);
level=graythresh(G);
level=graythresh(H);
if level>level1
T=im2bw(G);
figure
imshow(T);
else
P=im2bw(H);
figure
imshow(P);
end
end
kindly suggest some code or technique to do?

4 Comments

Besides the points highlighted in Paul's answer below, and the unreadable nature of your post (edit your post, get rid of the blank lines you probably painstakingly inserted, select all the code, then click the {}Code button), you need to explain your problem more clearly.
What are you trying to recombine? What is the expected output?
i should get back same image
Which image? The badly named e that you've split in the badly named C? The badly named h that you've split in the badly named D? Some other image?
Since you haven't modified any image after splitting them why can't you use the original image.
Note that your
o=1:1*8
H=cell2mat(D(k,o)'); %For pete's sake use better variable names than just going through the alphabet!
can be rewritten more simply as
H = [D{k, :}];
and also note that 1*8 is just 8.
However, I'm not sure why I'm commenting. Since you've accepted an answer, your problem is obviously resolved.
as per the algorithm there is need to split and perform operations on them and then later again recombine it to find other parameters.......the original image i have taken at the beginning that i have to get back......and its not the one frame i have to simulate...i have approx 20 frames and so for sure there will be the difference as compared to original image based on threshold and other parameters........and this is just a draft code ..in main code for sure i will add valid names...this s as per my convenience i have variables names like this
so simulate and let me know any errors....s there any other command other than cell2mat to recombine all 8x8 images again into one image which i have got from mat2cell? and please check from here imshow(G); here only i am stuck which is affecting the desired output

Sign in to comment.

Answers (1)

It's a bit difficult to read your code above. If you follow-up with more code, try highlighting the code portion of your post and selecting the "Code" option/button in the Matlab forum formatting menu to make it more legible.
However, when I paste it into Matlab and format, it would appear that you are re-defining "i" within the FOR loop. In other words, you have a FOR loop that loops over i, but then you appear to have another FOR loop within that one that also loops over i. If I'm seeing this correctly, then this will definitely lead to problems and may be the source of what you're seeing.
Paul Shoemaker

2 Comments

Actually, in this code because the outer i is only used before the inner i loop, it will not cause any issue. But yes, this is a really bad idea to have the same index variable in inner loops.
In fact, apart from nframe, rows and columns all the variables are badly named, giving no indication of their purpose. It looks like the OP is just going through the alphabet.
Using meaningful variable names goes a long way towards documenting the purpose of the code.
i have done the modification and i should get back same image which i had read at the starting(bw) but based on graythresh value whereas i am getting a single strip.suggest something in this?

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 5 Mar 2018

Edited:

on 7 Mar 2018

Community Treasure Hunt

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

Start Hunting!