Image Conditional Sum on Boundry

2 views (last 30 days)
Minhal
Minhal on 18 Jan 2014
Commented: Minhal on 19 Jan 2014
Hello, I have the script shown below. Just read in an image into it and it performs some calculations. im2bw has already been used on the images so they are lack and white
function [mom] = central_moments(inimage, 3)
[xpos, ypos, val] = find(im2double(inimage));
[p, q] = size(inimage);
BW = bwperim(inimage);
for r = 1:p
for c = 1:q
moma(1,1) = sum(val(BW(r,c)==1));
momb(1,1) = sum(val(BW(r,c)==0));
mom(1,1)= moma(1,1) + momb(1,1)
% I want this to give the same result as using
% mom(1,1)= sum(val);
end
end
My final objective is to find Hu's moments for an image with a different weighting based on if the pixel is on the boundary/edge or not. If I could give a certain weight to moma or momb I think I could achieve my objective.

Accepted Answer

Image Analyst
Image Analyst on 19 Jan 2014
Have you read the documentation in find for what the third return argument is? Evidently not. You should read it.
And why do you have moma, momb, and mom as arrays but all you ever set is the (1,1) element? So it's really not an array but just a scalar. And you overwrite that every time so the value it has after the loop is the value for r,c, so all the iterations you did before that were completely for nothing. Have you stepped through this with the debugger to see what values are being assigned at each step?
  1 Comment
Minhal
Minhal on 19 Jan 2014
Thank you very much. I'm quite new at Matlab, this script is an edited version of something I found. I didn't realize that sum produce values in one go I though they iterate every time. I'm going to do this with for loops now since you can set 'if' statements in them and they are iterative so the value doesn't reset.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!