How to count annotation dots from am image?

4 views (last 30 days)
h612 on 1 Jun 2017
Commented: Image Analyst on 1 Jun 2017
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

Accepted Answer

Image Analyst
Image Analyst on 1 Jun 2017
Don't use JPG for image analysis. Because you did, that's one reason why Niko's code didn't work. The other reason being that he didn't get the count in a moving window like you requested. Here is a solution:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
% Get the name of the image the user wants to use.
baseFileName = '8.jpg';
% Get the full filename, with path prepended.
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);
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[imageRows, imageColumns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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')
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Threshold to get the blue mask.
blueMask = redChannel < 100 & greenChannel < 100 & blueChannel > 100;
% Display the image.
subplot(2, 2, 2);
grid on;
axis on;
title('Initial Blue Mask Image', 'FontSize', fontSize);
% Fill blobs.
blueMask = imfill(blueMask, 'holes');
% Reduce to single points.
blueMask = bwulterode(blueMask);
% Display the image.
subplot(2, 2, 3);
grid on;
axis on;
title('Final Blue Mask Image', 'FontSize', fontSize);
% Count points in a moving window of size 8 rows by 16 columns.
windowColumns = 16; % Adapt as needed.
windowRows = 8; % Adapt as needed.
% Do the counting
countImage = conv2(double(blueMask), ones(windowRows, windowColumns));
% Display the image.
subplot(2, 2, 4);
axis on;
caption = sprintf('Count Image: count in a %d by %d moving box', windowRows, windowColumns);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
Needless to say, simply adapt the code to use the number of rows and columns that you want in your moving window (box).

More Answers (1)

Niko on 1 Jun 2017
Edited: Niko on 1 Jun 2017
Say the color of the dot is [0, 0, 255]. First convert the image to a logical matrix:
BW = im(:, :, 1) == 0 & im(:, :, 2) == 0 & im(:, :, 3) == 255;
Now BW is 1 where it's blue in the original image and 0 otherwise. Then
[~, num] = bwlabel(BW)
gives you what you need. (You'll need the image processing toolbox for bwlabel.)

Community Treasure Hunt

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

Start Hunting!