Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Finding value of a matrix

Asked by FIR on 19 Dec 2012

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

Walter Roberson on 19 Dec 2012

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

FIR

Products

No products are associated with this question.

3 Answers

Answer by Walter Roberson on 19 Dec 2012
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

Walter Roberson on 19 Dec 2012

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

http://matlabbyexamples.blogspot.in/2011/12/median-filtering-using-matlab.html

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.

Walter Roberson on 19 Dec 2012
B(i,j)=median(K(:));
Walter Roberson
Answer by Muruganandham Subramanian on 19 Dec 2012
Edited by Muruganandham Subramanian on 19 Dec 2012
   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 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

Walter Roberson on 19 Dec 2012

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.

Muruganandham Subramanian on 19 Dec 2012

@FIR, I din't worked on images, anyway, by using imread() read your image and do the calculation

Muruganandham Subramanian
Answer by Image Analyst on 19 Dec 2012

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.

0 Comments

Image Analyst

Contact us