Checking the repeated values, add them and find mean

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)

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)]
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

Well, for a more clear picture, Here i have 2 images. "Img1" is a binary image from which im getting the index value(i.e decimal values,44128,54960..) for each 4x4 blocks of "Img1", which store in "m(i,j)". "Img2" is my original gray image from which , im extracting g(3,3) values from each 4x4 block of "Img2" which stores in "vec". Then, lookup table created for "m(i,j)" and "vec", need to perform above operation. like,counting the repeated values in"m(i,j)" and then corresponding matching values of gray pixel values in "vec".Then by adding those pixel values of "vec" and then finally finding the mean out of it. Its a process of converting binary halftoned image to original gray image.
Thanks, Can you help me on this?
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?
1)"g" is nothing but my original image from which i have extracted g(3,3)(i.e 3rd row, 3rd col) position value from each 4x4(overlapping) blocks and i stored in "vec".
2) Here i have 4x4 overlapping blocks.Yes ,it is sliding over the pixels.
3)I do not have images. I need to recover the original image after the above operations. You can try above code in Img1 for (binary images-eg halftoned image) and Img2 for gray scale image. you can get the desired output.
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.
1) Yes, for 2 images in the sense , Img1 for (binary halftoned image) and Img2 for(original gray image from which i halftoned it).
2) m is my index value which im retrieving from each 4x4 overlapping blocks.
0 0 0 0
0 44128 54960 58320
0 27334 15723 40509
0 50860 58326 31203
0 11370 40509 51102
0 58054 31203 48249
0 36396 51102 27591
0 26850 48249 54972
0 50830 27591 48491
0 44136 22204 43990
3) outMat which first store all 4x4 overlapping blocks.
>> outMat(:,:,1)
ans =
162.0000 161.0000 156.0000 160.0000
162.0000 161.0000 126.6250 119.8828
146.3750 89.8047 147.5029 159.0443
174.8384 175.1339 73.6330 148.3667
>> outMat(:,:,2)
ans =
161.0000 156.0000 160.0000 160.0000
161.0000 126.6250 119.8828 197.4634
89.8047 147.5029 159.0443 90.9630
175.1339 73.6330 148.3667 181.0726
>> outMat(:,:,3)
ans =
156.0000 160.0000 160.0000 159.0000
126.6250 119.8828 197.4634 141.0198
147.5029 159.0443 90.9630 109.8475
73.6330 148.3667 181.0726 168.0671
4) Now , "vec" which stores Img2(3,3) positions.
147.5
159.04
90.963
109.85
145.27
149.57
81.07
151.15
154.8
73.633
148.37
181.07
168.07
64.924
133.5
125.44
5) Now both "m"(indexvalues) and "vec" to be stored in 2 col. and need to find mean gray values. Then original gray image can be recovered.
Perhaps if you attached your images. To get the mean, there is a mean() function you know - can't you use that?
Need to check the repeated index values(from "m") and the corresponding gray values, add the gray values and then mean, like above i have mentioned at the beginning.
I need to add one counter for repeated index values, which stores all repeated no. of index values. all this need to write inside the for loop.
Im just trying to do it.. but is not able to figure out. without using inbuilt functions
shall i attach the binary and original image?
Yes. And how did you decide on the weights in w? I know there's a pattern, but why is that pattern like that?
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!