How to find and color circle in a binary image of circles using sliding window through out the image?

15 views (last 30 days)
Pavel Todorov
Pavel Todorov on 10 May 2020
Edited: Image Analyst on 13 May 2020
I have a binary image in which there are circles and I want to make a sliding window which will go through out the image and when it finds circle it colors the circle. The result I want is the same image but with colored circles.

Answers (3)

Akira Agata
Akira Agata on 12 May 2020
Edited: Image Analyst on 13 May 2020
Looking at the original image, target regions are filled with plane color.
So I tried to apply the entropyfilt to extract the ROI. I believe the following code could extract the target regions.
Then, what should the next step should be? (filled with specific color? or approximate the regions with ellipse?)
% Read the image and convert to grayscale
I = imread('image.jpeg');
Igray = rgb2gray(I);
% Apply entropy filter
J = entropyfilt(Igray);
J = mat2gray(J);
% Binarize and extract the low-entropy regions
BW = imbinarize(J);
BW = ~BW;
% Extract and clean-up the ROI
BW = imclearborder(BW);
BW = imfill(BW,'holes');
BW = bwareafilt(BW,[100 Inf]);
% Show the result
Image Analyst
Image Analyst on 13 May 2020
Then don't call imclearborder().
Since Agata-san's answer works, why is it so important that you use a different one based on the Sobel filter that does not work as well and everyone has been recommending against?
Besides, your edge image does not look like the one from the edge() function.
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 = 22;
folder = pwd;
baseFileName = 'image.jpeg';
% Get the full filename, with path prepended.
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);
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.
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
% Get a binary image
binaryImage = edge(grayImage, 'Sobel');
% Get rid of the surround.
% binaryImage = imclearborder(binaryImage);
subplot(2, 2, 2);
% figure
imshow(binaryImage, []);
title('Edge Image', 'FontSize', fontSize, 'Interpreter', 'None');
edtImage = bwdist(binaryImage);
subplot(2, 2, 3);
imshow(edtImage, []);
title('EDT Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Threshold at 1
% [lowThreshold, highThreshold] = threshold(3, rows, edtImage);
% binaryImage = edtImage > lowThreshold;
binaryImage = edtImage > 15;
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');

Sign in to comment.

Image Analyst
Image Analyst on 10 May 2020
" I want to make a sliding window which will go through" Why??? Why not simply use bwlabel() and label2rgb()?
% For one two-character sub-image, now need to split apart into the left character and the right character.
[labeledImage, numberOfRegions] = bwlabel(binaryImage);
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% Display this line - both characters - with each separate blob in its own unique color.
Why use some complicated scanning program when you can do it with two simple built-in function calls?




Community Treasure Hunt

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

Start Hunting!

Translated by