How to sum and plot all pixels of an area in image and plot them?
20 views (last 30 days)
Show older comments
Stelios Fanourakis
on 9 Jan 2019
Commented: Walter Roberson
on 12 Jan 2019
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
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.
Accepted Answer
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
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
More Answers (1)
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
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.
See Also
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!