I have a script that performs calculations on a image. I want to have the pixels on the edge to have different weighing in this calculation.
I believe the eta20 being produced normally (without the *2) is correct. I'd like to be able to manipulate its value by changing the weight assigned to the boundary pixels
a = ['1.jpg'];
b = imread(a);
a = double(a);
[M, N] = size(a);
[x, y] = meshgrid(1:N, 1:M);
[p, q] = size(b); % Turning image into matrix or p by q dimensions
% Turn x, y, and a into column vectors.
x = x(:);
y = y(:);
a = a(:);
BW = edge(b,'sobel'); %inbuilt matlab function that gives '1' for boundary pixels
imshow(BW)
% Loop through the matrix which is assigning different moments if the pixel
% is on the boundary or not.
for r = 1:p
for c = 1:q
if BW(r,c)==1 %If value is 1 pixel is on the boundary
m.m00 = sum(a);
m.m10 = sum(x .* a);
m.m01 = sum(y .* a);
m.m20 = sum(x.^2 .* a)*2; % *2 has no on effect on eta20
else
m.m00 = sum(a);
m.m10 = sum(x .* a);
m.m01 = sum(y .* a);
m.m20 = sum(x.^2 .* a)*2; %*2 has effect on eta20
end
end
end
xbar = m.m10 / m.m00;
ybar = m.m01 / m.m00;
e.eta20 = (m.m20 - xbar*m.m10) / m.m00^2
If someone could explain what I did wrong? I know its probably more math than matlab.
Note: The final purpose is to calculate Hu's moments which consider the pixels at the edge of the image to be more important in the final calculations but that's exactly what isn't happening

 Réponse acceptée

Image Analyst
Image Analyst le 22 Déc 2013
BW equals 1 not just for the boundary but everywhere it's true. If you want to find pixels on the boundary, use bwperim:
boundaryPixels = bwperim(BW);
[boundaryRows, boundaryCols] = find(boundaryPixels);

3 commentaires

Minhal
Minhal le 23 Déc 2013
Okay now I replaced BW = edge(b,'sobel') by BW = bwperim(b) but now its just the opposite now and the part in the else has no effect now.
I see ([boundaryRows, boundaryCols] = find(boundaryPixels);) gives the location of all pixels on the boundary. Is there a way to change my if statement so if the pixel is on the boundary it does the first calculation else it does the other? I tried several ways but it didn't work.
It will not be the opposite. Edges will still be indicated by white lines.
There is nothing to change. If you do
if BW(r,c)==1
then it will do what follows ("the first calculation") if the pixel is on the boundary, just like you asked.
Minhal
Minhal le 23 Déc 2013
Thanks a lot. So the the calculation now considers the boundary pixels. Perfect I really appreciate your help!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by