I'm trying to localize people from an image with annotations. I need to count number of annotations in amoving window. How can I do that? A sample image is attached
MATLAB: How to count annotation dots from am image
annotation;crowd estimationImage Processing Toolboxlocalization
Related Solutions
Convert to grayscale and then threshold and find the bounding box
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;% Read in the color image.
folder = pwd;baseFileName = 'm2.bmp';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);% Display the original color image.
subplot(2, 3, 1);imshow(rgbImage, []);axis on;title('Original Color Image', 'FontSize', fontSize);drawnow;% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Histogram the red channel, but there is a huge spike at 255 so let's not count those.
subplot(2, 3, 2);grayImage = rgb2gray(rgbImage);imshow(grayImage, []);title('Gray Scale version', 'FontSize', fontSize);subplot(2, 3, 3);histogram(grayImage(grayImage<255))grid on;title('Histogram of Gray Scale Image', 'FontSize', fontSize);someThresholdValue = 150;binaryImage = grayImage > someThresholdValue;% Take largest blob
binaryImage = bwareafilt(binaryImage, 1);subplot(2, 3, 4);imshow(binaryImage);grid on;title('Binary Image', 'FontSize', fontSize);% Label the binary image.
labeledImage = bwlabel(binaryImage);props = regionprops(labeledImage, 'BoundingBox');bbox = props.BoundingBoxcroppedImageRGB = imcrop(rgbImage, bbox);subplot(2, 3, 5);imshow(croppedImageRGB);grid on;title('Cropped RGB Image', 'FontSize', fontSize);% Gray scale version of cropped image
croppedImageGray = imcrop(grayImage, bbox);subplot(2, 3, 6);imshow(croppedImageGray);grid on;title('Cropped Gray Scale Image', 'FontSize', fontSize);
Is one of these what you are looking for? Walter's right about the ambiguity so I picked a couple of possibilities (grayscale changing the image and not using a colormap, and RGB thresholding each channel individually) and programmed up a demo for you. If neither case is what you want, then you must clarify. Don't worry about the length, just copy, paste, and run, then study the code later.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'football.jpg';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);% Display the original color image.
subplot(2, 2, 1);imshow(rgbImage, []);title('Original Color Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);greenChannel = rgbImage(:, :, 2);blueChannel = rgbImage(:, :, 3);% Threshold each color channel to get pixels darker than 40.
redMask = redChannel < 40;greenMask = greenChannel < 40;blueMask = blueChannel < 40;% Apply the mask
redChannel(redMask) = 255;greenChannel(greenMask) = 0;blueChannel(blueMask) = 0;% Recombine into RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);% Display
subplot(2, 2, 2);imshow(rgbImage, []);title('Masked Color Image', 'FontSize', fontSize);promptMessage = sprintf('Now we will do it for a gray scale image?');titleBarCaption = 'Continue with gray scale example?';button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');if strcmp(button, 'Cancel') return;end% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'cameraman.tif';% Get the full filename, with path prepended.fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time. if ~exist(fullFileName, '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; endendgrayImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);% Display the original gray scale image.
subplot(2, 2, 3);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize);% Get red, green, and blue channels.
redChannel = grayImage;greenChannel = grayImage;blueChannel = grayImage;% Threshold each color channel to get pixels darker than 40.mask = grayImage < 40;% Apply the maskredChannel(mask) = 255;greenChannel(mask) = 0;blueChannel(mask) = 0;% Recombine into RGB image.rgbImage = cat(3, redChannel, greenChannel, blueChannel);% Displaysubplot(2, 2, 4);imshow(rgbImage, []);title('Masked Color Image', 'FontSize', fontSize);
Best Answer