Checking the repeated values, add them and find mean

1 view (last 30 days)
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
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
Image Analyst on 20 Dec 2014
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
Image Analyst
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
Sanghamitra Tripathy on 22 Dec 2014
Edited: Sanghamitra Tripathy on 24 Dec 2014
I have used error-diffusion floyd-steinberg algorithm for generating halftoned image.
Here "w" is a pattern of 0 to 2 to the power 15 values.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!