How to remove few part of a circle?
Show older comments
I am drawing different radius circles over an image object using simple plot function. How can remove circle portion from background and will only left the portion that is going through image object.
Answers (1)
Image Analyst
on 2 Mar 2019
You can make a mask and then use it to erase the part of the image you don't want
binaryImage(mask) = false; % Erase in mask
However your wording is so ambiguous, I don't know what the mask should be :
- the circle perimeter alone,
- the part inside the circle, or
- the part outside the circle.
Please explain better or attach a desired output image.
17 Comments
Image Analyst
on 3 Mar 2019
Assuming when you generate each circle, you have the (x,y) coordinates of it, simply erase them:
for k = 1 : length(x)
col = round(x);
row = round(y);
binaryImage(row, col) = false;
end
Zara Khan
on 3 Mar 2019
Image Analyst
on 3 Mar 2019
Attach 'img.png'
Zara Khan
on 4 Mar 2019
Zara Khan
on 6 Mar 2019
Image Analyst
on 14 Mar 2019
Yes, try this:
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 = 20;
rgbImage = imread('img.png');
% 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(rgbImage)
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(rgbImage);
% 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 = rgbImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's definitely gray scale with range of 0 to 255.
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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')
drawnow;
binaryImage = bwareafilt(~grayImage, 1);
props = regionprops(binaryImage,'Orientation','Centroid','MajorAxisLength','MinorAxisLength');
circleCenterX = props.Centroid(1);
circleCenterY = props.Centroid(2);
% Get the average of the min and max diameter.
diameter = mean([props.MajorAxisLength, props.MinorAxisLength], 2)
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
hold on
drawnow;
circumference = 2 * pi * props.MajorAxisLength;
t = linspace(0, 2*pi, 1.5 * circumference);
fprintf('Writing black to the %d points along the circles.', length(t));
for n = 1 : 8
fprintf('Processing circle #%d.\n', n);
r = n * diameter / 16;
x = circleCenterX + r * sin(t);
y = circleCenterY + r * cos(t);
plot(x,y, 'LineWidth', 2);
% Make the binary image black where it is at the circle.
for k=1:length(x)
col=round(x(k));
row=round(y(k));
binaryImage(row,col) = false;
end
end
% Display the final image.
subplot(2, 2, 3);
imshow(binaryImage);
title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');

madhan ravi
on 14 Mar 2019
+1, really admirable ?
Zara Khan
on 15 Mar 2019
Image Analyst
on 15 Mar 2019
Sure, just change it to an RGB image.
rgbImage(row,col, 1) = redColor;
rgbImage(row,col, 2) = greenColor;
rgbImage(row,col, 3) = blueColor;
Of course you will need to change those values every circle if you want different colors. They should be in the range 0-255.
Zara Khan
on 24 Oct 2019
Image Analyst
on 24 Oct 2019
Not sure what you mean. The code above will work for any color, just put in the right values.
Zara Khan
on 29 Oct 2019
Image Analyst
on 29 Oct 2019
Binary images are only black or white. You can use an RGB image though:
rgbImage(row,col, 1) = redValue;
rgbImage(row,col, 2) = greenValue;
rgbImage(row,col, 3) = blueValue;
Zara Khan
on 31 Oct 2019
Image Analyst
on 31 Oct 2019
Again, you'd have to use an RGB image.
Zara Khan
on 4 Nov 2019
Categories
Find more on Display 2-D Images in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!