user2272952
user2272952

Reputation: 81

MATLAB variable saturating in addition operation

I've written the code below in MATLAB. After the loop, dSumH is always saturated at 255 when I know its value should exceed this. When I replace d with an arbitrary value, however, it doesn't saturate. Any clues to fix it?

dTh = 127;
dSumH = zeros(w,1);

for(c = 1:w)

    for(r = 2:h)

        d = abs(img(r,c) - img(r-1,c));
        if(d >= dTh)
            dSumH(c) = dSumH(c) + d;
        end

    end

end
dSumH
figure, plot([1:w],dSumH);

Upvotes: 1

Views: 101

Answers (1)

user2272952
user2272952

Reputation: 81

OK, I got it. The variable d is of class uint8, so the addition operation implicitly defines dSumH as uint8 also. By changing d = abs(img(r,c) - img(r-1,c)); to d = double(abs(img(r,c) - img(r-1,c))); it works as expected.

Upvotes: 1

Related Questions