Why my doesn't work well?

3 Ansichten (letzte 30 Tage)
Ouael Chkoundali
Ouael Chkoundali am 23 Jan. 2019
Kommentiert: Ouael Chkoundali am 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?

Akzeptierte Antwort

Omer Yasin Birey
Omer Yasin Birey am 23 Jan. 2019
Bearbeitet: Omer Yasin Birey am 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.
  1 Kommentar
Ouael Chkoundali
Ouael Chkoundali am 23 Jan. 2019
Thanks a lot Omer. It works now

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by