Why my doesn't work well?

7 views (last 30 days)
Ouael Chkoundali
Ouael Chkoundali on 23 Jan 2019
Commented: Ouael Chkoundali on 23 Jan 2019
Hello,
I am writing a program for my thesis. The goal is to draw a vertical line on an image (in my case all images has 2 colors: white, another color and all possible combinations of the 2 colors) and get the color intensity of each pixel on the image along this line. Also it is possible to choose a number of lines (columns) left and right of the drawn line and then we build the average... My algorithm works that way: after uploading the image on th GUI, a function will convert it in a gray image and save all pixel color values (that means values between 0 and 255) in a matrix value_matrix. Then the user can choose how many lines he want to select left and right of the drawn line. And then a new_matrix will be built, that contains all relevant columns from value_matrix. Now I want to calculate the average of each line of new_matrix. I used this code:
%calculate the averages of pixel values on the same line
new_dim = size (new_matrix);
sum = 0;
for i = 1 : new_dim(1)
for j = 1 : new_dim(2)
sum = sum + new_matrix(i,j)
end
average(i) = sum/new_dim(2);
sum = 0;
end
The problem is that sum can't have a value greater than 255. I don't understand why. For example, if new_matrix is a 200x3 matrix. and on the line 5 we have the values 150, 200 and 50. Normally sum should be 400. But it doesn't work that way. When the summe is greater then 255, sum will have automatically the value 255. Why?

Accepted Answer

Omer Yasin Birey
Omer Yasin Birey on 23 Jan 2019
Edited: Omer Yasin Birey on 23 Jan 2019
Since you are reading images(from somewhere) it takes uint8 type of variables and for uint8 you can have maximum 255, if you want to make it more than 255 you can change it into some format else that allows more than 255 I recommend double. So change the line in the loop with
sum = sum + double(new_matrix(i,j))
and that should work.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!