science project on counting shapes

1 次查看(过去 30 天)
I've been using the attached image and blobs demo to look at the attached image. I need to count the number of light colored circles/shapes in the image. THe image I uploaded is natively a TIF, but I converted to it to JPG so I could upload it. I'm over my head on this one. After two weeks, I'm no where on this one. I know the steps based on other posts is below but that has not helped since I cant get any individual steps to complete with any meaningful output.
  • # # # Binarize the image so that it's logical. binaryImage = grayImage > 128
  • # # # Call binaryImage = imclearborder(binaryImage) to get rid of the single line around the perimeter.
  • # # # Invert the image: binaryImage = ~binaryImage, so now black circles are white
  • # # # Call binaryImage = imclearborder(binaryImage) to get rid of the large (now white) background
  • # # # Call bwlabel: [labeledImage, numberOfCircles] = bwlabel(binaryImage). This gives you the count.
  • # # #

回答(1 个)

Image Analyst
Image Analyst 2017-6-8
Maybe try a top hat filter. Or else try adapthisteq() to try to flatten out the background.
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 short g;
format compact;
fontSize = 20;
filename = '10mgml.jpg';
fullFileName = fullfile(filename);
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Crop off the annotation at the bottom.
grayImage = grayImage(1:213, :);
% 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')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Do a tophat filter
se = strel('disk', 2, 0);
filteredImage = imtophat(grayImage, se);
% Display the image.
subplot(2, 2, 3);
imshow(filteredImage, []);
axis on;
title('Top Hat Filtered Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(filteredImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of top hat filtered image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Binarize the image
binaryImage = filteredImage > 10;
% Display the image.
subplot(2, 2, 4);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
  1 个评论
Benjamin Baynard
Benjamin Baynard 2017-6-8
That does pull out the image correctly I believe. How can I add another area on the output for a count of the shapes? That was something on the blob demo that I could not figure out at all.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by