Why my doesn't work well?

조회 수: 2 (최근 30일)
Ouael Chkoundali
Ouael Chkoundali 2019년 1월 23일
댓글: Ouael Chkoundali 2019년 1월 23일
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.
  댓글 수: 1
Ouael Chkoundali
Ouael Chkoundali 2019년 1월 23일
Thanks a lot Omer. It works now

댓글을 달려면 로그인하십시오.

추가 답변 (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