How to sum pixels of a certain area and put a point to stop
3 次查看(过去 30 天)
显示 更早的评论
Hi
I want to sum all pixels of a certain area of the image and the summation will go on (vertically) 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
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) .
5 个评论
采纳的回答
Image Analyst
2019-1-12
If you want to start at some row, and go down to some other row where the red line is for that column (which varies from column to column), use code like this:
% Column 1:
c1 = [0 0 74 74 255 255 65 66 0]'
% Column 2:
c2 = [1 1 0 0 74 74 255 255 23]'
% Column 3:
c3 = [0 0 67 67 67 88 88 112 113]'
% Make image
grayImage = [c1,c2,c3]
% Let's say the red line is at these 3 rows:
redLines = [7, 8, 6];
% Sum from 2 to the red line
[rows, columns] = size(grayImage)
theSums = zeros(1, columns);
theCounts = zeros(1, columns);
startingRow = 2;
for col = 1 : columns
theSums(col) = sum(grayImage(startingRow : redLines(col), col));
theCounts(col) = redLines(col) - startingRow + 1;
end
19 个评论
Image Analyst
2019-1-14
I think this does what you want.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'A2.jpg';
folder = pwd;
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(2, 3, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original Color Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
%===================================================================================
% FIND THE RED MASK
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% get a mask for the red line.
mask = redChannel > greenChannel;
% Display the image.
subplot(2, 3, 2);
imshow(mask);
caption = sprintf('Color Segmentation Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis('on', 'image');
drawnow;
% fill holes.
mask = imfill(mask, 'holes');
% Take the largest blob only.
mask = bwareafilt(mask, 1);
% Shrink two layers of pixels to come inside the red lines.
mask = imerode(mask, true(5));
% Display the image.
subplot(2, 3, 3);
imshow(mask);
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis('on', 'image');
% Get a masked image.
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Display the image.
subplot(2, 3, 4);
imshow(maskedRgbImage);
title('Masked RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis('on', 'image');
% A = contour(mask,'b');
set(gca,'ydir','reverse')
conversion = 0.9183; % in mm/pixel
addMM = @(x) sprintf('%.3fmm', x * conversion);
xticklabels(cellfun(addMM, num2cell(xticks'), 'UniformOutput', false));
yticklabels(cellfun(addMM, num2cell(yticks'), 'UniformOutput', false));
%===================================================================================
% FIND THE HEIGHT OF THE MASK, AND SUM OF GRAY LEVELS FROM ROW 1 TO RED ROW.
topRow = 1; % Whatever you want.
grayLevelSum = zeros(1, columns); % Initialize sums for every column.
heights = zeros(1, columns); % Initialize heights for every column.
% Find the left and right column of the mask so we know where to sum over.
[rows, columns] = find(mask);
col1 = min(columns);
col2 = max(columns);
for col = col1 : col2
row2 = find(mask(:, col), 1, 'last'); % Get bottom row of mask
if ~isempty(row2)
% Get the sum in the GREEN channel, from row1 to row2.
grayLevelSum(col) = sum(greenChannel(topRow:row2, col));
% Compute the height of the mask at every column.
heights(col) = sum(mask(topRow:row2, col));
end
end
% Plot integrated gray level.
subplot(2, 3, 5);
plot(grayLevelSum, 'b-', 'LineWidth', 2);
xlabel('Column', 'FontSize', 20);
ylabel('Sum of gray levels in column', 'FontSize', 20);
title('Sum of gray levels in column', 'FontSize', 20);
grid on;
% Plot heights
subplot(2, 3, 6);
plot(heights, 'b-', 'LineWidth', 2);
xlabel('Column', 'FontSize', 20);
ylabel('Height of mask in column', 'FontSize', 20);
title('Mask Height vs. Column', 'FontSize', 20);
grid on;
% Tell user the answer.
message = sprintf('Done!');
uiwait(helpdlg(message));
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!