How to sum and plot all pixels of an area in image and plot them?

21 views (last 30 days)
A new aproach and a NEW question of my previous query.
What I actually want to achieve is to get for every column of the image (see attached image) the sum of all its pixels. But not for the entire image. I want for part inside the red contour.
The counting should start from the bottom of the red segmented contour up until the top red contour. Is there a way to put a marker somewhere in the first one third of the image and tell Matlab to count from that point down to the bottom of the red contour, the total of pixels for every column and plot them.
YOu can see at image A3.jpg, I have marked with green pen the top point and the bottom point, of the area the pixel should be measured.
Is my question clear?
  1 Comment
Adam
Adam on 9 Jan 2019
If you have the red surrounding line in Matlab you can just use it as a mask on your original image to remove all the data outside of it, then just sum down each column as you would for a full matrix.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 10 Jan 2019
From your other question, you already have the masked image. So to sum vertically, just use sum(). To plot, use plot():
horizontalProfile = sum(maskedImage, 1); % Sum vertically.
plot(horizontalProfile, 'b-', 'LineWidth', 2);
xlabel('Column', 'FontSize', 20);
ylabel('Sum of gray levels in column', 'FontSize', 20);
  8 Comments
Stelios Fanourakis
Stelios Fanourakis on 12 Jan 2019
I use this code but I get the error "Assignment has more non-singleton rhs dimensions than non-singleton subscripts" at line s(i,j)=sum(s) .
Remember, I want to sum all pixels of a certain area of the image and the summation will go on until it finds the first red pixel where it should stop and move to the next column. Please help
clc;
s = imread('A2.jpg')
for i = 136:548
for j = 123:length(s)
s(i,j)=sum(s)
if s(i,j)== 100
break
end
s = s+1;
end
end
Image Analyst
Image Analyst on 12 Jan 2019
You have the masked image already from my other answer. If you want to start summing from y = 123 to the bottom of the mask, just do this
row1 = 123; % Whatever...
s = zeros(1, columns); % Initialize sums for every column.
for col = 1 : columns
row2 = find(mask(:, col), 1, 'last'); % Get bottom row of mask
if ~isempty(row2)
% Get the sum from row1 to row2
s(col) = sum(grayImage(row1:row2, col));
end
end

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 9 Jan 2019
You can threshhold on bright white. Suppose you do that getting threshimg that is 1 where the bright white is. Then
counts = sum(cumprod(~threshimg))
will be the per-column count of pixels from the top to the first bright white pixel in the column.
The sum of the cumprod of the negated image is a clever vectorized way of doing the equivalent of
nr = size(threshimg,1); nc = size(threshimg,2);
for K = 1 : nc
counts(K) = find([threshimg(:,K);true], 1) - 1; %force artificial boundary in case no line
end
  6 Comments
Stelios Fanourakis
Stelios Fanourakis on 12 Jan 2019
I use this code but I get the error "Assignment has more non-singleton rhs dimensions than non-singleton subscripts" at line s(i,j)=sum(s) .
Remember, I want to sum all pixels of a certain area of the image and the summation will go on until it finds the first red pixel where it should stop and move to the next column. Please help
clc;
s = imread('A2.jpg')
for i = 136:548
for j = 123:length(s)
s(i,j)=sum(s)
if s(i,j)== 100
break
end
s = s+1;
end
end
Walter Roberson
Walter Roberson on 12 Jan 2019
Your s is 3D, since it is an RGB image. sum(s) is going to be 3D as well, since the default summation dimension is the first non-singular dimension, which is going to be the first dimension,leaving you with 1 x columns x 3 in size. You cannot store that into a scalar location s(i,j) .
Watch out: length(s) means
temp = size(s);
if any(temp == 0)
s_length = 0;
else
s_length = max(temp);
end
It does not mean the number of rows or columns in s.
If, somehow, you were able to store the result of the sum() into the scalar location, then you would be modifying progressively modifying the same array that you are using sum() over, and you would be adding 1 to each location as well. The code just does not make any sense.

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!