I had divided an image into blocks where each block has 8*8 pixels .Now i want to find the energy of each block,for that i have to consider the energy of each pixels in a block and the calculations are given in the code below.
2 次查看(过去 30 天)
显示 更早的评论
After making the calculation for each block i have to show the enery map of the image that is being divided into blocks ,but i am not geting it .Can you plz help me .Actualy i am trying to do block based seam carving
x=imread('cameraman.tif');
y=double(x);
[r c]=size(y);
bs=8; %size of each block
nob=(r/bs)*(c/bs);%number of blocks
kk=0;
for i=1:(r/bs)
for j=1:(c/bs)
Block(:,:,kk+j)=y((bs*(i-1)+1:bs*(i-1)+bs),(bs*(j-1)+1:bs*(j-1)+bs));
%block creation
%ENERGY CALCULATION PER BLOCK
for q=1:bs
for p=1:bs
if((q-1==0)&&(p-1==0))
a=Block(q,p,kk+j);
b=0;
n=a-b;
c=Block(q,p,kk+j);
d=0;
m=c-d;
elseif((q-1==0)&&(p-1~=0))
n=y(q:q,p)-y(q:q,p-1);
a=Block(q,p,kk+j);
b=0;
m=a-b;
elseif((q-1~=0)&&(p-1==0))
a=Block(q,p,kk+j);
b=0;
n=a-b;
m=y(q:q,p)-y(q-1:q-1,p);
else
n=y(q:q,p)-y(q:q,p-1);
m=y(q:q,p)-y(q-1:q-1,p);
end
s=n+m ;%
en=s/nob;%Here the calculation for energy for block is being done.
imshow(en)
% Here i want to show the energy map for the entier blocks but unfortunatly i am geting only a single spot.
end
end
end
kk=kk+(r/bs);
end
%%%%%%%%%% I GOT UR ANSWER BUT I'M NOT GETING THE CORRECT OUTPUT ,CAN U PLZZZZ HELP ME OUT.THE PBLM THAT I FACED IS PUT AS A COMMENT TO UR ANSWER PLZ CHECK IT..
5 个评论
Amith Kamath
2011-11-4
at the outset: please read http://www.mathworks.com/matlabcentral/about/answers/# , especially the tips on asking a question, for it's quite hard for people to really understand your actual question..
Thanks!
yasmine
2011-12-10
please i want to to know can i use ur method of cutting image into blocks for RGB images ?
Thank you
ROSE MARY KATIKALA
2017-2-8
i want to divide an image into blocks and then calculate energy values for each block.. to make roads and non roads in my project
Image Analyst
2017-2-8
What's your definition of energy? If you go back to first principles, and work out the units, you'll see that gray level has units of energy. So all you need to do is to take the moving mean, like in the attached demos.
采纳的回答
Image Analyst
2011-11-4
Is "en" an image? I don't see any indices being used when you set values for it. It looks to me like it is a scalar so that would show up as a single point.
Maybe you want this
en(p, q)=s/nob;
and then after the q and p loops have finished (when you'll have the complete image), do the imshow.
31 个评论
Soumya
2011-11-10
Sir ,
Firstly I want to make a notification that "en" is the energy of the image and by performing imshow in en I need an energy map figure but I'm not getting it . Now what should I do??????????/////
Thanks & Regards
soumya sadan
Soumya
2011-11-14
sir,
Is it possible to view the entire blocks using mat2cell()?If so plzz explain?
Is it possible to extract individual blocks from an image which is divided using blkproc()?If so how?
I'm not getting the images divided into blocks so I'm thinking to use blkproc or mat2cell(),but the problem is that I want to show the image divided into blocks and to process on each block......
plzzz give me a reply.........................
Image Analyst
2011-11-14
Doesn't the imshow(en), with the now fixed en, work to show the image? If not you'll have to add a drawnow after that so that it will force the screen to update.
Soumya
2011-11-15
sir,
I tried many times but I'm not getting the image divided into blocks.And what do u meant by " to add a drawnow",i can't understand bcozzzz I'm new to this environment .So now I'm thinking to use blkproc or mat2cell,but the problem is that:
1)I have to show the image divided blocks (like that obtained by using blkproc)
2)Take individual blocks and perform some process on each blocks.
can u help me out????????????????/
Image Analyst
2011-11-15
Tell us exactly what you mean by "divided". Do you want hundred of sub images in their own variables, each with a different variable name, or as a cell in a cell array? Look up drawnow in the help. You simply have the letters "drawnow;" on their own line following the imshow line.
Soumya
2011-11-16
actually I want to divide the entire pixels of image to 8x8 submatrices or blocks,and display the image which is divided into blocks and also display the individual blocks.Then we want to perform some calculation on individual blocks ie;perform calculation pixel by pixel in a block.
Image Analyst
2011-11-16
That is what blockproc does. But do you need to keep ALL of the hundreds of submatrices hanging around in memory? I doubt it.
Soumya
2011-11-17
sir,
I'm using matlab2009 ,so I think blockproc won't work.Can u plzzz correct the above given code,so that I can get the image diveided into no:of blocks,and whenever I call en(num) I should get each block and perform processing on each block
Soumya
2011-11-17
sir,
Now I'm thinking that the entire blocks is difficult to be displayed in an image,so how would be the idea of arranging the blocks in a different matrix in the correct order,and give different colors to each row,so that I can understand that a particular block belongs to a particular row.
Walter Roberson
2011-11-17
If your MATLAB does not have blockproc, use blkproc instead. blkproc passes the actual data for the block in to the user routine, not information about the block, so you would not pass in the block number: you would just calculate the energy for the data given and return it and blkproc() will take care of arranging the results in to a matrix in the same shape as the blocks.
Walter Roberson
2011-11-17
Instead of coloring the rows differently, consider just turning the XGrid and YGrid properties of the axes on, with the XTick and YTick properties set to the boundaries of the blocks.
http://www.mathworks.com/help/techdoc/ref/grid.html
Soumya
2011-11-17
I'm thinking like this becozzzz on getting a block am not sure that it is the correct matrix from the image.If I'm not getting the correct blocks from the image ,then how could I perform calculation on individual blocks .plzzz help me.
If u don't mind I want to know that is there any way to directly contact with u sir.So that I can give u the correct idea about wat am doing
Soumya
2011-11-17
sir,
if we are using the blkproc() we can display the image into blocks but how can we access individual blocks (like: on calling a(num) should give each individual blocks within the image)
Walter Roberson
2011-11-17
You can use mat2cell if you need to break the image up in to separate matrices so that you can be sure that you are getting the correct block or so that you can use indexing to access the block.
Soumya
2011-11-18
by using mat2cell I don't know how t access more than one vlocks in corect order can u help me?????????????//
Walter Roberson
2011-11-18
blocks = cell2mat(y, 8 * ones(1,r/8), 8 * ones(1,c/8));
blocks{1,1} %upper left block
blocks{1,2} %block to the right of upper left
blocks{1,3} %block two over from upper left
blocks{2,3} %block below the one two over from upper right
blocks{end,end} %lower right
Or if it is important to be able to index the blocks by a single number, then
blocks{1}
blocks{2}
blocks{3}
and so on, where the block numbers go *down* the first column, then continue from the top of the second column on downwards, then continue from the top of the third column downwards, and so on.
Soumya
2011-11-18
code:
[x,map]=imread('cameraman.tif');
bw=edge(x,'sobel');
[r c]=size(bw)
blocks = mat2cell(bw, 8 * ones(1,r/8), 8 * ones(1,c/8));
arr=magic(256);
for i=1:256
for j=1:256
arr=blocks{i,j};
end
end
figure,imshow(arr);
/////////////////////
I'm using the above given code.
According to ur instruction I used mat2cell.Here I've decalared an array "arr" of size 256x256 and used two loops. Bcozzz I want to access each block and fix it in the "arr" array so that when I display the entire array I would be able to display the entire blocks with in the array in the correct order as in the image.I have got strict instruction to do this can u help me????????????
But I'm getting an error like
????Index exceeds matrix dimensions.
Error in ==> mat4mat2cell at 12
arr=blocks{i,j};
Soumya
2011-11-18
code:
[x,map]=imread('cameraman.tif');
bw=edge(x,'sobel');
[r c]=size(bw);
blocks = mat2cell(bw, 8 * ones(1,r/8), 8 * ones(1,c/8));
arr=magic(256);
for i=1:8:256
for j=1:8:256
for r=1:32
for c=1:32
arr(i,j)=blocks{r,c} ;
end
end
end
end
figure,imshow(arr);
//////
Later I tried this code to assign each blocks to the "arr" in the correct order but I'm getting an error:
??? Subscripted assignment dimension mismatch.
Error in ==> mat4mat2cell at 12
arr(i,j)=blocks{r,c} ;
Can u help me by removing this error plzzzzzzz.
Walter Roberson
2011-11-18
for r=1:size(blocks,1)
for c=1:size(blocks,2)
arr((r-1)*8+1:r*8,(c-1)*8+1:c*8) = blocks{r,c};
end
end
Soumya
2011-11-19
Sir,
Your code was helpfull for me,but I've a small doubt-----> size(blocks,1)&size(blocks,2)=>> I know that the size is 32 but what's the purpose of mentioning the number 1 and 2.Cant we simply specify it like size(blocks).........
Image Analyst
2011-11-19
size(blocks,1) gets the the number of rows and size(blocks,2) gets you the number of columns. You can also do it like this if you want
[rows columns] = size(yourArray);
Soumya
2011-11-19
Now I'm working to find the energy of each block using the equation like: for a pixel (x,y) the calculation is made by
sum=((x,y)-(x-1,y))+((x,y)-(x,y-1))%% calculation made per pixel in a block
energy=1/total numbers of pixels in a block*∑(sum)%%here energy of a block is to be obtained
This is the equation and I tried to implement it but I'm not getting the correct thing.
code----->>
[x,map]=imread('cameraman.tif');
bw=edge(x,'sobel');
[r c]=size(bw);
blocks = mat2cell(bw, 8 * ones(1,r/8), 8 * ones(1,c/8));
arr=magic(256);
for r=1:size(blocks,1)
for c=1:size(blocks,2)
arr((r-1)*8+1:r*8,(c-1)*8+1:c*8) = blocks{r,c};
end
end
figure,imshow(arr);
for r=1:32
for c=1:32
blocks{r,c} ;
for q=1:8 % row in a block
for p=1:8 %col in a block
if((q-1==0)&&(p-1==0)) %%%%%%%%%%%%%%%%%%%%%condition for the top most and left most elements in a block%%%%%%%%%%%%%%%%%%%%%%%%%
a=blocks{r,c}(q,p);
b=0;
n=a-b; %%% n variable indicates column wise calculation
cc=blocks{r,c}(q,p);
d=0;
m=c-d; %%% m variable indicates row wise calculation
elseif((q-1==0)&&(p-1~=0)) %%%%%%%%%%condition for the top most elemnts except the left most elements%%%%%%%%%%%%%%%%%%%%
n=blocks{r,c}(q,p)-blocks{r,c}(q,p-1); %%% n variable indicates column wise calculation
a=blocks{r,c}(q,p);
b=0;
m=a-b; %%% m variable indicates row wise calculation
elseif((q-1~=0)&&(p-1==0)) %%%%%%%%%%condition for the left most elements except the to most elements%%%%%%%%%%%%%%%%%%%%
a=blocks{r,c}(q,p);
b=0;
n=a-b;
m=blocks{r,c}(q,p)-blocks{r,c}(q-1,p);
else
n=blocks{r,c}(q,p)-blocks{r,c}(q,p-1);
m=blocks{r,c}(q,p)-blocks{r,c}(q-1,p);
end
s=n+m ;
en=s/64;
e=en;
end
end
end
end
figure,imshow(e);
//////////////////////
the above given is the code , can u figure out the mistake and help me......................
Thanks 4 ur valuable help sir
Soumya
2011-11-19
sir I have made a mistake in the above given code .Inorder to get the value I should replace the code "figure,imshow(e);" by fprintf("%f",e) [I hope the correction dat I made is correct],but the problem is that where should I print the value to get the energy for an entire block.And like the previously mentioned "arr" array I have to create another array and assign value of each block to the array
can u help me out!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Soumya
2011-11-19
sir can u explain in matlab how the decrementation is made in for loop.
for r=1:size(blocks,1)
for c=1:size(blocks,2)
%%%%%%%%this is the incrementation loop I need the reverse of this
end
end
As I'm new to this environment I'm unaware about many things plzzz give me a guidance..........
Image Analyst
2011-11-19
Replace the last few lines of your code with this and see if that doesn't do it for you:
en=s/64;
e(r, c) = en;
end
end
end
end
% Show the energy map.
figure; % New figure
% Make it big enough to see something.
eBig = imresize(e, [512 512], 'Nearest');
% Display it.
imshow(eBig, []);
Soumya
2011-11-23
Sir.
I'have an image of size 256x256. Now i want to take the minimum value of the pixel from the last row,and after getting the minimum value I need to take the three neighbouring pixels lying on the previous row of the min valued pixel (of the last row)and find min from them.Then again take the three neighbouring pixels on the prevoius row of the min valued pixel(of the second last row) and find minimum from them .In this manner the calculation goes to the first row,thus Iwill get a path of min values fromthe last row to the first row.Inorder to recognize this path I've to specify the pixels in that path with some color like white.
can u help me!!!!!!!!!!!!
Image Analyst
2011-11-23
A simple for loop and the min() function would do it for you - it's trivial. However, if you think about it for just a few seconds, you will realize that your proposed method WILL NOT get you the path with the overall minimum sum or average going from bottom to top. I suggest you read up on optimization methods. Browse through Wikipedia and look up topics like A*, Dijkstra, depth-first and breadth-first searches, seam carving, dynamic programming, and that kind of thing. You need to spend about 2 hours of reading up first before you waste several hours programming up a bad solution.
Soumya
2011-12-5
can u explain the meaning of each component of this loop
[rows cols dim]=size(x);
%sobel operator used to calculate gradient image
Grd=[ -1 -2 -1;
0 0 0;
1 2 1];
% Grd1=[ 0 -1 0; -1 4 -1 ; 0 -1 0];
Emean=zeros(rows,cols);
for i=1:dim
Eh(:,:,i)=conv2(x(:,:,i),Grd,'same');
Ev(:,:,i)=conv2(x(:,:,i),Grd.','same');
E(:,:,i)=abs(Eh(:,:,i))+abs(Ev(:,:,i));
% E(:,:,i)=conv2(X(:,:,i),Grd1,'same');
end
Emean=1/dim*sum(E,3); %finds average gradient image if RGB image
plzzzz
Soumya
2011-12-5
I have an array en() containing energy value where each array of size 8x8 contains energy value per blockie;en(1:8,1:8)-energy of first block,en(9:16,1:8)and on using imshow(en)-I can display an energy map.
the below given code is performed on pixel by pixel basis.Here x is the gradient image instead of it I want to use en array and use additional code if needed so that I can do on block basis . and assign each block value to a seperate array so that on displaying that array I can get an Image.
SeamImg=zeros(rows,cols);
SeamImg(1,:)=x(1,:);
for i=2:rows
for j=1:cols
if j-1<1
SeamImg(i,j)= x(i,j)+min([SeamImg(i-1,j),SeamImg(i-1,j+1)]);
elseif j+1>cols
SeamImg(i,j)= x(i,j)+min([SeamImg(i-1,j-1),SeamImg(i-1,j)]);
else
SeamImg(i,j)= x(i,j)+min([SeamImg(i-1,j-1),SeamImg(i-1,j),SeamImg(i-1,j+1)]);
end
end
end
Help me out from this trouble sirrrr plzzzzzzzzzzzzz
Soumya
2011-12-9
sir,
Acoording to ur guidance i have solved many of my problems.thanks for ur guidance and instructions sir.At the last moment i am facing a small problem,but I'm not able to find wat's wrong can u help me .code----->
[r c dim]=size(SeamImg);%%128 128
[SVrow SVcol]=size(SeamVector);%%128 128
for i=1:SVcol
for k=1:dim
for j=1:r
if SeamVector(j,i)==1
cut(j,:,k)=SeamImg(j,2:c,k);
elseif SeamVector(j,i)==c
cut(j,:,k)=SeamImg(j,1:c-1,k);
else
cut(j,:,k)=[SeamImg(j,1:SeamVector(j,i)-1,k) SeamImg(j,SeamVector(j,i)+1:c,k)];
end
end
end
end
SeamImg=cut;
clear cut
figure
imshow(SeamImg)
%%%error-->
Subscripted assignment dimension mismatch.
Error in ==> blocksfinal at 186
cut(j,:,k)=[SeamImg(j,1:SeamVector(j,i)-1,k) SeamImg(j,SeamVector(j,i)+1:c,k)];
I'm waiting for ur valuable instructions......
Walter Roberson
2011-12-9
That error could occur if you had initialized the cut() array to an incompatible size. You do not _show_ any initialization for cut(), but we don't know that you did not do any in code you did not show us.
更多回答(0 个)
另请参阅
标签
尚未输入任何标签。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)