Finding value of a matrix

I have a 3x3 matrix, for example:
d=rand(3,3)
Now i want to implement the equation
V=1/2(d(q4,q5)+d(q5,q6))
where q5 is the centre pixel
q4,46 are neighbouring pixels
for example
d= q1 q2 q3
q4 q5 q6
q7 q8 q9
please assist

1 Comment

q4 and q5 and so on are values of those pixels?

Sign in to comment.

 Accepted Answer

Hard to tell. You might be asking for
oldMatrix = double(ImageMatrix);
NewMatrix = ( oldMatrix(:,1:end-1) + oldMatrix(:,2:end) ) ./ 2;
But perhaps your "d" matrix is trying to talk about weighting the pixel values ? If so then let w1 be the weight for what you showed as d(q4,q5), and let w2 be the weight for what you showe as d(q5,q6), and then
NewMatrix = ( w1 .* oldMatrix(:,1:end-1) + w2 .* oldMatrix(:,2:end) ) ./ 2;

12 Comments

FIR
FIR on 19 Dec 2012
Walter my image is 'peppers.png'.for each 3x3 window i want to find the neighbour values i have to find
V=1/2(d(q4,q5)+d(q5,q6))
What exactly does
d(q4,q5)
mean here?
Also, peppers.png is an RGB image. Are you processing RGB or did you convert to a single color plane already?
FIR
FIR on 19 Dec 2012
Edited: FIR on 19 Dec 2012
i am processing RGB image
=
[q1 q2 q3
q4 q5 q6
q7 q8 q9
];
q5 is centre pixel value of 3x3 window and q4,q6 are its neighbours of a 3x3 window
Assuming you are going to take the average along each of the color planes independently,
oldMatrix = double(ImageMatrix);
NewMatrix = ( oldMatrix(:,1:end-2,:) + oldMatrix(:,3:end,:) ) ./ 2;
Possibly you will also want to
NewMatrix = cast(Newmatrix, class(ImageMatrix));
if the original was uint8 and you want the new one to be uint8 as well.
Note: you did not indicate what is to be done in the border cases. Your filter is defined horizontally without using any vertical values, so your filter can be applied to all rows but is not defined for the edge columns. The result array NewMatrix is therefore narrower than the original.
I had to guess what d(q4,q5) and d(q5,q6) meant. As it looks like each of those is intended to represent a single value (per color plane), I guessed that d(q4,q5) is intended to represent the value of the pixel to the left of the center pixe, and that q(q5,q6) is intended to represent the value of the pixel to the right of the center pixel. If so then the notation is inconsistent, but whatever.
But of course you might have meant d(q4,q5) to be the difference in values between the pixels q4 and q5. If so then would that be abs(q4-q5) or should the result be signed?
FIR
FIR on 19 Dec 2012
In the filtering window,we calculate the color pixel differences between the center pixel q5 and other two neighboring pixels. For example, the color pixel difference in the direction defined as V1. when q5 is on the edge,at least one value among Vl(1,2,3,4) will be extremely small theoretically.When q5 is in the smooth area of an image, Vl(1,2,3,4) are all equal to zero approximately.However if q5 is corrupted by impulsenoise,all four values will turn to be significantly larger than zero even if the pixel is on the edge. Vl(1,2,3,4) isV1 ,V2 ,V3,V4
Ah! You introduced "d" as something to do with the windowing originally, and then you promptly re-used "d" as meaning "difference"!
So then under one interpretation of "difference",
oldMatrix = double(ImageMatrix);
NewMatrix = ( oldMatrix(:,3:end,:) - oldMatrix(:,1:end-2,:) ) ./ 2;
You might notice there is no explicit reference to the center pixel. That is not a mistake for this code. This code is using d(q4,q5) as if you meant diff([q4 q5]), producing a signed result; the q5 value algebraically cancels out in the sum to produce diff([q4 q6])
If you want the absolute difference rather than the signed difference, then
oldMatrix = double(ImageMatrix);
NewMatrix = ( abs(oldMatrix(:,2:end-1) - oldMatrix(:,1:end-2,:)) + abs(oldMatrix(:,1:end-2,:) - oldMatrix(:,3:end,:)) ) ./ 2;
FIR
FIR on 19 Dec 2012
walter i get error
Error using ==> minus Matrix dimensions must agree.
FIR
FIR on 19 Dec 2012
walter these are to perform quanterion switching filter
FIR
FIR on 19 Dec 2012
I am working on Quaternion switching filter for impulse noise reductionin color image
I am not sure which version you used; I just noticed a typo in the absolute difference version. Corrected is
oldMatrix = double(ImageMatrix);
NewMatrix = ( abs(oldMatrix(:,2:end-1,:) - oldMatrix(:,1:end-2,:)) + abs(oldMatrix(:,1:end-2,:) - oldMatrix(:,3:end,:)) ) ./ 2;
FIR
FIR on 19 Dec 2012
Edited: Walter Roberson on 19 Dec 2012
Thanks walter
another thing i tought of doing it in a loop because ihave to replace some values so i tried for 7x7 from link
in the code for 7x7 matrix
for i=2:6
for j=2:6
K= A(i-1:i+1,j-1:j+1);
B(i,j)=median(K);
end
end
i get error
Subscripted assignment dimension mismatch.
B(i,j)=median(K(:));

Sign in to comment.

More Answers (2)

d=ones(3);
V=1/2*(d(d(2,2),d(2,2))+d(d(2,2),d(2,3)))
but, If you're using rand(), d(2,2),etc.. can't be accessible, if not using ceil() or floor()..Is this you want?

7 Comments

FIR
FIR on 19 Dec 2012
i am getting error
Error: Unbalanced or unexpected parenthesis or bracket.
have you tried this?
d=ones(3);
V=1/2*(d(d(2,2),d(2,2))+d(d(2,2),d(2,3)))
FIR
FIR on 19 Dec 2012
it works for ur ones(3) but for rand(3) get error
??? Attempted to access d(0.959492,0.849129); index must be a positive integer or logical.
and also q4 is in position 2,1 ,why u have used 2,2
@FIR, thanks for corrected error.. i.e. V=1/2*(d(d(2,1),d(2,2))+d(d(2,2),d(2,3))) only..
And I explained about this in my answer previously..
d=rand(3,3)
i.e,
d=
[q1 q2 q3
q4 q5 q6
q7 q8 q9
];
here, q5=>d(2,2)..right? but, If you want to get 'q5' or other data, we're indexing by d(2,2).,and that must be an whole number, shouldn't be a float.So, by using ceil(), floor() command you can get it..but when you're using rand(), it'll be float..to avoid this you can use, randi(3,3)
FIR
FIR on 19 Dec 2012
Thanks a lot,assume i have a coloured image,peppers.png,i want to perform 3x3 window for image with above equation ,can u please tell how to perform
The confusion is between pixel locations and pixel values. You wrote the question with d(q4,q5) which based on the way you wrote the question is trying to access the matrix d() at row index which is the value of the pixel you labeled q4, and column index which is the value of the pixel you labeled q5.
@FIR, I din't worked on images, anyway, by using imread() read your image and do the calculation

Sign in to comment.

Image Analyst
Image Analyst on 19 Dec 2012

0 votes

By chance do you mean just convolution with a [1 2 1]/4 kernel, where you take the average of the left pair of pixels and the right pair of pixels? If so, you'd need to extract each color plane first and then use conv2() to get the output image one color plane at a time.

Community Treasure Hunt

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

Start Hunting!