MATLAB Answers

## How can I remove few parts of circle perimeter?

Asked by Zara Khan

### Zara Khan (view profile)

on 14 Mar 2019
Latest activity Commented on by Image Analyst

### Image Analyst (view profile)

on 19 Mar 2019
I have drawn few circles using plot function. How can remove few parts of circle perimeter? The concept is that circle perimter will be OFF when gets background and ON when gets image object.

Image Analyst

### Image Analyst (view profile)

on 17 Mar 2019
You have a binary image. You said "off" which I take it to mean means "false" or black. If you want the lines to be colors, you need to use an RGB image and set the values in each color channel. Basically:
rgbImage(row,col, 1) = redValue;
rgbImage(row,col, 2) = greenValue;
rgbImage(row,col, 3) = blueValue;
It should be easy for you to make that change. Let me know if it's too difficult for you and I'll do it.
Zara Khan

### Zara Khan (view profile)

on 19 Mar 2019
Image Analyst:
By 'off' I meant to say that only part will be removed. Circles will be in colors as it is. But parts which going through background will be removed only. It will look like the 'final image'. But instead of black will be the colors.
Sorry I was not able to explain it in a better way.
Image Analyst

### Image Analyst (view profile)

on 19 Mar 2019
To apply colors only to where the binary image was true (the hand), just check the binary image first before you assign the color to your RGB image:
if binaryImage(row, col)
rgbImage(row,col, 1) = redValue;
rgbImage(row,col, 2) = greenValue;
rgbImage(row,col, 3) = blueValue;
end

Sign in to comment.

## 1 Answer

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 14 Mar 2019

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');
Note: same code is also in your other question : Click for older question

#### 0 Comments

Sign in to comment.