How to transform different shapes to circles in Matlab - matlab

I have this image:
that has different shapes, and I want to transform each shape in a circle. And each circle must have different radius, depending on the size of the shape. How can I do that? With Morphology Operations or there are any function on Matlab that does that?
I used the function Regionprops to detect every individual shape, then I can do operations on each region separately.

I would use bwlabel to first label all of the components. Then I would use regionprops to find the bounding box of each component. You can then use the rectangle with a Curvature value of [1 1] to plot an ellipse at each bounding box.
%// Load the image and convert to 0's and 1's
img = imread('http://i.stack.imgur.com/9wRYK.png');
img = double(img(:,:,1) > 0);
%// Label the image
L = bwlabel(img);
%// Compute region properties
P = regionprops(L);
imshow(img)
for k = 1:numel(P)
%// Get the bounding box
bb = P(k).BoundingBox;
%// Plot an ellipse around each object
hold on
rectangle('Position', bb, ...
'Curvature', [1 1], ...
'EdgeColor', 'r', ...
'LineWidth', 2);
end
If you actually want circles, you will need to decide how exactly you define a circle from a rectangle. For this, I just used the maximum of the width and height for the diameter.
t = linspace(0, 2*pi, 100);
cost = cos(t);
sint = sin(t);
for k = 1:numel(P)
bb = P(k).BoundingBox;
%// Compute the radius and center of the circle
center = [bb(1)+0.5*bb(3), bb(2)+0.5*bb(4)];
radius = max(bb(3:4)) / 2;
%// Plot each circle
plot(center(1) + radius * cost, ...
center(2) + radius * sint, ...
'Color', 'r');
end
Now if you actually want to modify the image data itself rather than simply displaying it, you can use a meshgrid of all of the pixel centers to test whether a given pixel is within a circle or not.
%// Create a new image the size of the old one
newImage = zeros(size(img));
%// Determine the x/y coordinates for each pixel
[xx,yy] = meshgrid(1:size(newImage, 2), 1:size(newImage, 1));
xy = [xx(:), yy(:)];
for k = 1:numel(P)
bb = P(k).BoundingBox;
%// Compute the circle that fits each bounding box
center = [bb(1)+0.5*bb(3), bb(2)+0.5*bb(4)];
radius = max(bb(3:4)) / 2;
%// Now check if each pixel is within this circle
incircle = sum(bsxfun(#minus, xy, center).^2, 2) <= radius^2;
%// Change the values of newImage
newImage(incircle) = k;
end
%// Create a binary mask of all points that were within any circle
mask = newImage > 0;

Related

Draw line and Cut off Circuler area

I have got the below Image after running the below code.
file='grayscale.png';
I=imread(file);
bw = im2bw(I);
bw = bwareaopen(bw,870);
imwrite(bw,'noiseReduced.png')
subplot(2,3,1),imshow(bw);
[~, threshold] = edge(bw, 'sobel');
fudgeFactor = .5;
im = edge(bw,'sobel', threshold * fudgeFactor);
subplot(2,3,2), imshow(im), title('binary gradient mask');
se = strel('disk',5);
closedim = imclose(im,se);
subplot(2,3,3), imshow(closedim), title('Connected Cirlces');
cc = bwconncomp(closedim);
S = regionprops(cc,'Centroid'); //returns the centers S(2) for innercircle
numPixels = cellfun(#numel,cc.PixelIdxList);
[biggest,idx] = min(numPixels);
im(cc.PixelIdxList{idx}) = 0;
subplot(2,3,4), imshow(im), title('Inner Cirlces Only');
c = S(2);
My target is now to draw a red cirle around the circular object(see image) and cut the circle region(area) from the original image 'I' and save the cropped area as image or perform other tasks. How can I do it?
Alternatively, you can optimize/fit the circle with least r that contains all the points:
bw = imread('http://i.stack.imgur.com/il0Va.png');
[yy xx]=find(bw);
Now, let p be a three vector parameterizing a circle: p(1), p(2) are the x-y coordinates of the center and p(3) its radii. Then we want to minimize r (i.e., p(3)):
obj = #(p) p(3);
Subject to all points inside the circle
con = #(p) deal((xx-p(1)).^2+(yy-p(2)).^2-p(3).^2, []);
Optimizing with fmincon:
[p, fval] = fmincon(obj, [mean(xx), mean(yy), size(bw,1)/4], [],[],[],[],[],[],con);
Yields
p =
471.6397 484.4164 373.2125
Drawing the result
imshow(bw,'border','tight');
colormap gray;hold on;
t=linspace(-pi,pi,1000);
plot(p(3)*cos(t)+p(1),p(3)*sin(t)+p(2),'r', 'LineWidth',1);
You can generate a binary mask of the same size as bw with true in the circle and false outside
msk = bsxfun(#plus, ((1:size(bw,2))-p(1)).^2, ((1:size(bw,1)).'-p(2)).^2 ) <= p(3).^2;
The mask looks like:
The convexhull of the white pixels will give you a fairly good approximation of the circle. You can find the center as the centroid of the area of the hull and the radius as the average distance from the center to the hull vertices.

How to find the area of an arbitrary shape contained within a circle using MATLAB

I have an arbitrary shape, of which the exterior boundary has been traced in MATLAB using bwboundaries. Using regionprops, I can calculate the total area enclosed by this shape.
However, I want to know the area for only the parts of the shape that fall within a circle of known radius R centered at coordinates [x1, y1]. What is the best way to accomplish this?
There are a few ways to approach this. One way you could alter the mask before performing bwboundaries (or regionprops) so that it only includes pixels which are within the given circle.
This example assumes that you already have a logical matrix M that you pass to bwboundaries.
function [A, boundaries] = traceWithinCircle(M, x1, y1, R);
%// Get pixel centers
[x,y] = meshgrid(1:size(M, 1), 1:size(M, 2));
%// Compute their distance from x1, y1
distances = sqrt(sum(bsxfun(#minus, [x(:), y(:)], [x1, y1]).^2, 2));
%// Determine which are inside of the circle with radius R
isInside = distances <= R;
%// Set the values outside of this circle in M to zero
%// This will ensure that they are not detected in bwboundaries
M(~isInside) = 0;
%// Now perform bwboundaries on things that are
%// inside the circle AND were 1 in M
boundaries = bwboundaries(M);
%// You can, however, get the area by simply counting the number of 1s in M
A = sum(M(:));
%// Of if you really want to use regionprops on M
%// props = regionprops(M);
%// otherArea = sum([props.Area]);
end
And as an example
%// Load some example data
data = load('mri');
M = data.D(:,:,12) > 60;
%// Trace the boundaries using the method described above
B = traceWithinCircle(M, 70, 90, 50);
%// Display the results
figure;
hax = axes();
him = imagesc(M, 'Parent', hax);
hold(hax, 'on');
colormap gray
axis(hax, 'image');
%// Plot the reference circle
t = linspace(0, 2*pi, 100);
plot(x1 + cos(t)*R, y1 + sin(t)*R);
%// Plot the segmented boundaries
B = bwboundaries(M);
for k = 1:numel(B)
plot(B{k}(:,2), B{k}(:,1), 'r');
end

How to draw a filled circle on a video frame using matlab

I have an "inverted-pendulum" video which I try to find the mid point of moving part. I am using Computer Vision Toolbox
I change the mid point's color using detected coordinates. Assume that X is the frame's row number for the detected mid point and the Y is the col number.
while ~isDone(hVideoFileReader)
frame = step(hVideoFileReader);
...
frame(X-3:X+3, Y-3:Y+3, 1) = 1; % # R=1 make the defined region red
frame(X-3:X+3, Y-3:Y+3, 2) = 0; % # G=0
frame(X-3:X+3, Y-3:Y+3, 3) = 0; % # B=0
step(hVideoPlayer, frame);
end
Then I easily have a red square. But I want to add a red filled circle on the detected point, instead of a square. How can I do that?
You can use the insertShape function. Example:
img = imread('peppers.png');
img = insertShape(img, 'FilledCircle', [150 280 35], ...
'LineWidth',5, 'Color','blue');
imshow(img)
The position parameter is specified as [x y radius]
EDIT:
Here is an alternative where we manually draw the circular shape (with transparency):
% some RGB image
img = imread('peppers.png');
[imgH,imgW,~] = size(img);
% circle parameters
r = 35; % radius
c = [150 280]; % center
t = linspace(0, 2*pi, 50); % approximate circle with 50 points
% create a circular mask
BW = poly2mask(r*cos(t)+c(1), r*sin(t)+c(2), imgH, imgW);
% overlay filled circular shape by using the mask
% to fill the image with the desired color (for all three channels R,G,B)
clr = [0 0 255]; % blue color
a = 0.5; % blending factor
z = false(size(BW));
mask = cat(3,BW,z,z); img(mask) = a*clr(1) + (1-a)*img(mask);
mask = cat(3,z,BW,z); img(mask) = a*clr(2) + (1-a)*img(mask);
mask = cat(3,z,z,BW); img(mask) = a*clr(3) + (1-a)*img(mask);
% show result
imshow(img)
I'm using the poly2mask function from Image Processing Toolbox to create the circle mask (idea from this post). If you don't have access to this function, here is an alternative:
[X,Y] = ndgrid((1:imgH)-c(2), (1:imgW)-c(1));
BW = (X.^2 + Y.^2) < r^2;
That way you get a solution using core MATLAB functions only (no toolboxes!)
If you have an older version of MATLAB with the Computer Vision System Toolbox installed, you can use vision.ShapeInserter system object.
Thanks #Dima, I have created a shapeInserter object.
greenColor = uint8([0 255 0]);
hFilledCircle = vision.ShapeInserter('Shape','Circles',...
'BorderColor','Custom',...
'CustomBorderColor', greenColor ,...
'Fill', true, ...
'FillColor', 'Custom',...
'CustomFillColor', greenColor );
...
fc = int32([Y X 7;]);
frame = step(hFilledCircle, frame, fc);
I then applied it to detected point.

Selecting an ellipse as a region of interest (ROI)

I used imellipse to select an ellipse as my region of interest (ROI). The issue is that the ellipse I want to select is of around 45 degrees, and, when I use imellipse, it seems it is 90 degrees either horizontally or vertically.
How can I change the orientation of the ellipse?
Thanks.
You need to rotate the coordinates of an ellipse. Like this:
npts = 1e4;
t = linspace(0,2*pi,npts);
theta = pi/4;
aspect = [5 1]; % [x y]
x = aspect(1)*sin(t+theta);
y = aspect(2)*cos(t);
plot(x, y);
If you want to use imellipse to draw the ellipse on an image, you can extract the vertices and transform them:
figure, imshow('pout.tif');
h = imellipse;
exy = h.getVertices
theta = pi/12;
M = [cos(theta), sin(theta); -sin(theta), cos(theta)]
exy_centered = bsxfun(#minus,exy,mean(exy))
exyRot = bsxfun(#plus,exy_centered*M,mean(exy));
hold on
plot(exyRot(:,1),exyRot(:,2),'r') % orig: plot(exy(:,1),exy(:,2),'r')
To fill in the ellipse, creating a mask, use roifill or roipoly:
w=getfield(imfinfo('pout.tif'),'Width');
h=getfield(imfinfo('pout.tif'),'Height');
bw = roipoly(zeros(h,w),exyRot(:,1),exyRot(:,2));

on matlab, how can i plot centroid points on top of an rgb image?

i have a list of about 300 centroid points. these points are the centroids of the conncomps of a BW image that i have. Is there a way to plot the points of the centroids over the original rgb image?
No problem. Have this short script:
img = imread('rice.png');
bg = imopen(img,strel('disk',15));
img2 = img - bg;
mask = im2bw(img2, 0.19);
mask = bwareaopen(mask, 40);
cc = bwconncomp(mask, 4);
positionArray = regionprops(cc, {'Centroid'});
positionArray = struct2cell(positionArray);
positionArray = cellfun(#transpose, positionArray, 'UniformOutput',false);
positionArray = cell2mat(positionArray);
imshow(img);
hold on;
scatter(positionArray(1, :), positionArray(2, :), 200, 'g+');
You can vary the markersize and shape as you wish. The points in this case are stored as a 2 by n matrix with x coordinates in the first row ans y in the second.
First, the image itself is plotted using imshow. Then, scatter() is called. To put both items on the same set of axes, you have to call hold on.