Why my doesn't work well?

3 次查看(过去 30 天)
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?

采纳的回答

Omer Yasin Birey
Omer Yasin Birey 2019-1-23
编辑:Omer Yasin Birey 2019-1-23
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Read, Write, and Modify Image 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by