Checking the repeated values, add them and find mean
Show older comments
I m working on my project, where i have 2 images as Img1 and Img2. As Img1 is the binary image so i have calculated all decimal values. For Img2 i have taken the pixel values.
For convenience i have taken 10X10 matrix values from the entire image for the below operation.
[row,col] = size(Img1);
m = zeros(row,col);
w = [1 2 4 8; 16 32 64 128; 256 512 1024 2048; 4096 8192 16384 32768];
for i=2:10
for j=2:10
O = double(Img1(i-1:i+2,j-1:j+2));
m(i,j) = sum(sum(O.* w));
end;
end;
[row,col] = size(Img2);
count = row*col;
outMat = zeros(4,4,count);
l=0;
for i=2:10
for j=2:10
l=l+1;
outMat(:,:,l) = Img2(i-1:i+2,j-1:j+2);
vec = outMat(3,3,:);
vec = vec(:);
end;
end;
Now, for Img2 , i have collected all pixel values, and need to store 2 col.as below.
Col1 col2 from Img2
from Img1
44128 162
54960 150
58320 119
31200 120
48240 180
54960 160
44128 163
51109 90
44128 56
Here, 44128 is repeated 3 times,now adding all correspong mapping values from col2 i.e. 162,163,56 add them all divide by 3(becos occurance of 44128 is 3 times) and same procedure to be followed for all values.
44128 (162+163+56)/3
54960 (150+160)/2
58320 (119/1)
31200 (120/1)
48240 (180/1)
51109 (90/1)
Here, I want to create an array N of 1D 1X(size of col) which acts as a counter of Img1 decimal values,repeated values and store the counter values inside N, and then finding mean by dividing corresponding counter values of N to the Img2 pixel values as above.
Please help :-( ,
How can I write the code inside the for loop above?
Answers (2)
Azzi Abdelmalek
on 20 Dec 2014
A=[44128 162
54960 150
58320 119
31200 120
48240 180
54960 160
44128 163
51109 90
44128 56]
[ii,jj,kk]=unique(A(:,1),'stable')
out=[ii accumarray(kk,A(:,2),[],@mean)]
Image Analyst
on 20 Dec 2014
0 votes
Well, your "m" is simply the convolution and you can get that in one line with conv2(). But I don't really know why you want these numbers. Can you take a step back and let us know what the big picture is? Instead of telling us a recipe for how you think you need to do it, why don't you just tell us what do you really want to do? Maybe a different approach would be better.
9 Comments
Sanghamitra Tripathy
on 21 Dec 2014
Edited: Sanghamitra Tripathy
on 21 Dec 2014
Image Analyst
on 21 Dec 2014
I didn't see g in your original code. I don't understand how you're getting a 3-by-3 matrix from the 4-by-4 block. Please explain.
And you talk about "blocks". Usually people mean that the block moves in "jumps" of the block width, but in your code it looks like the block slides along a pixel at a time, not a "jump" of the block width. Which is it? Maybe you should be using blockproc() instead of conv2().
Can you attach images?
Sanghamitra Tripathy
on 22 Dec 2014
Edited: Sanghamitra Tripathy
on 22 Dec 2014
Image Analyst
on 22 Dec 2014
But in your original message you said, and I quote: "where i have 2 images". So what happened to them? Where did they go? You got to give me something to work with.
The poorly named "m" is simply the convolution:
w = [1 2 4 8 0;
16 32 64 128 0;
256 512 1024 2048 0;
4096 8192 16384 32768;
0 0 0 0 0];
m = conv2(double(grayImage), w, 'same');
I can't figure out what you're doing with outMat, like why the value of the third dimension can go up to 9. Can you explain what each of the 9 planes (slices) in outMat represent? Note that you're setting all rows and all columns of outMat in each plane to the same value so the plane is uniform. And that means the (3,3) element of a plane is the same as any other pixel in the plane, like the 3,5 pixel or the 1,7 pixel, or any other pixel you specify - they're all identical.
Sanghamitra Tripathy
on 22 Dec 2014
Image Analyst
on 22 Dec 2014
Perhaps if you attached your images. To get the mean, there is a mean() function you know - can't you use that?
Sanghamitra Tripathy
on 22 Dec 2014
Edited: Sanghamitra Tripathy
on 22 Dec 2014
Image Analyst
on 22 Dec 2014
Yes. And how did you decide on the weights in w? I know there's a pattern, but why is that pattern like that?
Sanghamitra Tripathy
on 22 Dec 2014
Edited: Sanghamitra Tripathy
on 24 Dec 2014
Categories
Find more on Neighborhood and Block Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!