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

3 ビュー (過去 30 日間)
Pavel Todorov 2020 年 5 月 10 日

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.

#### 1 件のコメント

Akira Agata 2020 年 5 月 10 日
To find circles, can we simply use regionprops and measure the 'Circularity' ?

サインインしてコメントする。

### 回答 (3 件)

Akira Agata 2020 年 5 月 12 日

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
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
figure
imshowpair(I,BW,'montage')

#### 6 件のコメント

Pavel Todorov 2020 年 5 月 13 日
I understand what you are saying, but my job is to write an algorithm which can find even blobs, which are half way there.
Pavel Todorov 2020 年 5 月 13 日
>Akira Agata
Your code seems to work just find with binary image, but can we instead of binary image use Sober operator then something else to close infinished contours and finaly get results even better than these.
Image Analyst 2020 年 5 月 13 日
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);
uiwait(warndlg(errorMessage));
return;
end
end
% 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, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
%--------------------------------------------------------------------------------------------------------
% EXTRACTION OF IMAGE
% Get a binary image
binaryImage = edge(grayImage, 'Sobel');
% Get rid of the surround.
% binaryImage = imclearborder(binaryImage);
subplot(2, 2, 2);
% figure
imshow(binaryImage, []);
impixelinfo;
title('Edge Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
edtImage = bwdist(binaryImage);
subplot(2, 2, 3);
imshow(edtImage, []);
impixelinfo;
title('EDT Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Threshold at 1
% [lowThreshold, highThreshold] = threshold(3, rows, edtImage);
% binaryImage = edtImage > lowThreshold;
binaryImage = edtImage > 15;
subplot(2, 2, 4);
imshow(binaryImage, []);
impixelinfo;
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;

サインインしてコメントする。

Ameer Hamza 2020 年 5 月 10 日
See imfindcircles(): https://www.mathworks.com/help/releases/R2020a/images/ref/imfindcircles.html. Your image is binary; how do you want to assign colors. Randomly?

#### 0 件のコメント

サインインしてコメントする。

Image Analyst 2020 年 5 月 10 日
" 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.
imshow(coloredLabels);
Why use some complicated scanning program when you can do it with two simple built-in function calls?

#### 2 件のコメント

Hyeokjin Jho 2020 年 5 月 11 日
I would suggest thresholding and use regionprop, instead of sobel operation
Image Analyst 2020 年 5 月 11 日
I agree. Using an edge detection probably just complicated it. Post your original image.

サインインしてコメントする。

R2015a

### Community Treasure Hunt

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

Start Hunting!

Translated by