Connect centroids horizontally and detect missing objects - matlab

The task is to connect the centroids that I have got using regionprops horizontally in rows and then predict missing objects.
Here is the image that I have:
This is what I want to achieve :
All centroids within a certain y-coordinate range should be connected. After that I want to predict the missing objects. For example, there should be more objects/centroids present on the green line in the image above.
My code so far :
BW = rgb2gray(imread('noise_removal_single_25_cropped.png'));
props = regionprops(im2bw(BW), 'Centroid');
centroids = cat(1, props.Centroid);
[B,L] = bwboundaries(BW,'noholes');
imshow(label2rgb(L, #jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
plot(centroids(:,1),centroids(:,2), 'b*')
plot(centroids(:,1),centroids(:,2), 'k-')
The code connects all centroids vertically and I have no idea how to detect missing objects/centroids (maybe based on length of line)?

Let us assume that the rows are perfectly horizontal. It seems that you can easily cluster the points by ordinate, either knowing row separators in advance, or by analysis of the point density.
Take the median ordinate of every cluster and discard the outliers (those farther than a defined tolerance from the median).
Sort the inliers by abscissa. The gap lengths (or point count in a sliding window) will tell you about missing points.
If the rows aren't perfectly horizontal, it remains likely that you can cluster by ordinates and obtain good horizontal separators. In every cluster, use a robust line fitting algorithm that will perform outlier detection, and sort horizontally as before. You can also deskew (using the line equation), but given the small slope, this will make little difference.
Final remark: if all lines are parallel, you can perform skew detection collectively by finding the gravity centers (or medioids) of the clusters and translating the clusters to a common center, giving a single thick line.

Related

Reducing the area of the voronoi cells and determining the coordinates of new vertices

I have written a MATLAB code to create the figure attached using the voronoi. My region of interest is the red circle. Hence the seeds for voronoi were kept within the region.
Idea: One approach would be to use a homothetic transformation of the Voronoi cell C{k} about the corresponding point X(k,:), with a ratio R such as 0 < R < 1. The shape of the cells—the number of corners and their associated angles—will be preserved, and the areas will be reduced proportionally (i.e. by a factor R2, and not by a constant value).
Please note that this will "destroy" your cells, because the reduced Voronoi cells will not share anymore vertices/edges, thus the [V,C] representation doesn't work anymore as it is. Also, the distances between what were once common edges will depend on the areas of the original cells (bigger cells, bigger distances between adjacent edges).
Transformation example for 2 2D points:
A = [1,2]; %'Center'
B = [10,1]; %'To be transformed'
R = 0.8; %'Transformation ratio'
trB = A + R*(B-A); %'Transformed'
couldn't follow your implementation of CST-link's idea, but here is one that works (i tested it in matlab, but not yet in abaqus, the code it spits out looks like abaqus should be happy with it)
rng(0);
x=rand(40,2);
plot(x(:,1),x(:,2),'x')
[v,c]=voronoin(x);
fact=0.9;
for i=1:length(c)
cur_cell=c{i};
coords=v(cur_cell,:);
if isfinite(coords)
%fact=somefunctionofarea?;
centre=x(i,:); % i used the voronoi seeds as my centres
coords=bsxfun(#minus,coords,centre); %move everything to a local coord sys centred on the seed point
[theta,rho] = cart2pol(coords(:,1),coords(:,2));
[xnew, ynew]= pol2cart(theta,rho*fact);
xnew=xnew+centre(1); % put back in real coords.
ynew=ynew+centre(2);
xnew2=circshift(xnew,1);
ynew2=circshift(ynew,1);
fprintf('s1.Line(point1=(%f,%f),point2=(%f,%f))\n',...
[xnew, ynew, xnew2,ynew2]');
line(xnew,ynew); %testing purposes - doesn't plot last side in matlab
end
end
Having seen the results of this one, i think you will need a different way factor to shrink your sides. either to subtract a fixed area or some other formula.

Segmenting a grayscale image

I am having trouble achieving the correct segmentation of a grayscale image:
The ground truth, i.e. what I would like the segmentation to look like, is this:
I am most interested in the three components within the circle. Thus, as you can see, I would like to segment the top image into three components: two semi-circles, and a rectangle between them.
I have tried various combinations of dilation, erosion, and reconstruction, as well as various clustering algorithms, including k-means, isodata, and mixture of gaussians--all with varying degrees of success.
Any suggestions would be appreciated.
Edit: here is the best result I've been able to obtain. This was obtained using an active contour to segment the circular ROI, and then applying isodata clustering:
There are two problems with this:
The white halo around the bottom-right cluster, belonging to the top-left cluster
The gray halo around both the top-right and bottom-left cluster, belonging to the center cluster.
Here's a starter...
use circular Hough transform to find the circular part. For that I initially threshold the image locally.
im=rgb2gray(imread('Ly7C8.png'));
imbw = thresholdLocally(im,[2 2]); % thresold localy with a 2x2 window
% preparing to find the circle
props = regionprops(imbw,'Area','PixelIdxList','MajorAxisLength','MinorAxisLength');
[~,indexOfMax] = max([props.Area]);
approximateRadius = props(indexOfMax).MajorAxisLength/2;
radius=round(approximateRadius);%-1:approximateRadius+1);
%find the circle using Hough trans.
h = circle_hough(edge(imbw), radius,'same');
[~,maxIndex] = max(h(:));
[i,j,k] = ind2sub(size(h), maxIndex);
center.x = j; center.y = i;
figure;imagesc(im);imellipse(gca,[center.x-radius center.y-radius 2*radius 2*radius]);
title('Finding the circle using Hough Trans.');
select only what's inside the circle:
[y,x] = meshgrid(1:size(im,2),1:size(im,1));
z = (x-j).^2+(y-i).^2;
f = (z<=radius^2);
im=im.*uint8(f);
EDIT:
look for a place to start threshold the image to segment it by looking at the histogram, finding it's first local maxima, and iterating from there until 2 separate segments are found, using bwlabel:
p=hist(im(im>0),1:255);
p=smooth(p,5);
[pks,locs] = findpeaks(p);
bw=bwlabel(im>locs(1));
i=0;
while numel(unique(bw))<3
bw=bwlabel(im>locs(1)+i);
i=i+1;
end
imagesc(bw);
The middle part can now be obtained by taking out the two labeled parts from the circle, and what is left will be the middle part (+some of the halo)
bw2=(bw<1.*f);
but after some median filtering we get something more reasonble
bw2= medfilt2(medfilt2(bw2));
and together we get:
imagesc(bw+3*bw2);
The last part is a real "quick and dirty", I'm sure that with the tools you already used you'll get better results...
One can also obtain an approximate result using the watershed transformation. This is the watershed on the inverted image -> watershed(255-I) Here is an example result:
Another Simple method is to perform a morphological closing on the original image with a disc structuring element (one can perform multiscale closing for granulometries) and then obtain the full circle. After this extracting the circle is and components withing is easier.
se = strel('disk',3);
Iclo = imclose(I, se);% This closes open circular cells.
Ithresh = Iclo>170;% one can locate this threshold automatically by histogram modes (if you know apriori your cell structure.)
Icircle = bwareaopen(Ithresh, 50); %to remove small noise components in the bg
Ithresh2 = I>185; % This again needs a simple histogram.

Shape preserving clamped ends interpolation in Matlab

I'm looking for an algorithm in Matlab that can preserve the shape of my data while allowing me to clamp the ends. I'm trying to generate the camber line from the chord line, the leading edge angle, trailing edge angle, and the position of the max camber. See Airfoil terminology for definitions. Using that information, I want to generate any number of points between the leading edge and the trailing edge, evenly spaced on the chord.
Here are the algorithms I've evaluated so far:
'pchip' doesn't seem to allow clamping, unless I mistyped repeatedly when searching, but does offer proper shape preservation.
'spline' doesn't preserve shape. Using 3 points of data, the middle data point being the max camber and both ends clamped, a spline can't guarantee the middle data to be the highest point on the generated curve. See this answer for an example of that behavior.
'csape' provides adequate end conditions, but I cannot be sure it is adequately shape preserving.
If your data will only ever have those three points, you can do it in two stages, one for the first half, and the next for the second half. You can use the fact that at the point furthest from the chord line (the middle point), the gradient of the line will be zero.
Generate each line as a spline between two points, with each end at the designated angle.
X = [0 5 10];
Y = [0 3 2];
start_slope = 0;
end_slope = -0.7;
xx1 = linspace(X(1), X(2), 100);
xx2 = linspace(X(2), X(3), 100);
yy1 = spline(X(1:2), [start_slope, Y(1:2), 0], xx1);
yy2 = spline(X(2:3), [0, Y(2:3), end_slope], xx2);
plot([xx1, xx2], [yy1, yy2]);
hold on
scatter(X, Y, 'filled')
I've posted my question on the mathematics stack exchange and got the following answer. Essentially, I can use the Fritsch-Carlson scheme to calculate/set slopes at my data points. If I want to set the slope to my middle point, I will separate my interval in two parts, like Bill Cheatham suggests.
I can also wrap my data and use pchip or a spline for the points that apply instead of reimplementing the whole method.

Drawing a network of nodes in circular formation with links between nodes

I would like to draw a circular graph of nodes where certain nodes have a link between them. Here are a few examples from social network graphs:
(source: wrightresult.com)
(source: twit88.com)
How can this be done with MATLAB? Is it possible without installing a separate package?
Here is one way you can do what you want. First, generate points on the circle that you are interested in
clear;
theta=linspace(0,2*pi,31);theta=theta(1:end-1);
[x,y]=pol2cart(theta,1);
Next, if you know the pairs of nodes that are connected, you can skip this step. But in many cases, you get a connectivity matrix from other computations, and you find the indices of the connected nodes from that. Here, I've created a Boolean matrix of connections. So, if there are N nodes, the connectivity matrix is an NxN symmetric matrix, where if the i,jth element is 1, it means you have a connection from node i to node j and 0 otherwise. You can then extract the subscripts of the non-zero pairs to get node connections (only the upper triangle is needed).
links=triu(round(rand(length(theta))));%# this is a random list of connections
[ind1,ind2]=ind2sub(size(links),find(links(:)));
This is the connectivity matrix I generated with the code above.
Now we just need to plot the connections, one at a time
h=figure(1);clf(h);
plot(x,y,'.k','markersize',20);hold on
arrayfun(#(p,q)line([x(p),x(q)],[y(p),y(q)]),ind1,ind2);
axis equal off
which will give you a figure similar to your examples
Inspired by the latest blog post by Cleve Moler, you could also use the gplot function to draw a graph given an adjacency matrix and node coordinates.
Here is an example using bucky; a demo function part of MATLAB that generates the graph of a truncated icosahedron (looks like a soccer ball). We will only use its adjacency matrix for this example since we are laying out the vertices in a circular shape:
%# 60-by-60 sparse adjacency matrix
A = bucky();
N = length(A);
%# x/y coordinates of nodes in a circular layout
r = 1;
theta = linspace(0,2*pi,N+1)'; theta(end) = [];
xy = r .* [cos(theta) sin(theta)];
%# labels of nodes
txt = cellstr(num2str((1:N)','%02d'));
%# show nodes and edges
line(xy(:,1), xy(:,2), 'LineStyle','none', ...
'Marker','.', 'MarkerSize',15, 'Color','g')
hold on
gplot(A, xy, 'b-')
axis([-1 1 -1 1]); axis equal off
hold off
%# show node labels
h = text(xy(:,1).*1.05, xy(:,2).*1.05, txt, 'FontSize',8);
set(h, {'Rotation'},num2cell(theta*180/pi))
We can take this a step further and try to minimize edge crossings. That is we want to rearrange the nodes so that the edges are as close as possible to the circumference of the circle.
This can be done by finding a symmetric permutation of the matrix that minimizes its bandwidth (non-zeros are closer to the diagonal)
p = symrcm(A);
A = A(p,p);
txt = txt(p);
The result in this case:
Other improvements include replacing straight lines with curved splines to draw the edges, (that way you get a nicer graph similar to the second one you've shown), or using different colors to show clusters of vertices and their edges (obviously you'll need to do graph clustering). I will leave those steps to you :)

Detecting center point of cross using Matlab

Hello, I have an image as shown above. Is it possible for me to detect the center point of the cross and output the result using Matlab? Thanks.
Here you go. I'm assuming that you have the image toolbox because if you don't then you probably shouldn't be trying to do this sort of thing. However, all of these functions can be implemented with convolutions I believe. I did this process on the image you presented above and obtained the point (139,286) where 138 is the row and 268 is the column.
1.Convert the image to a binary image:
bw = bw2im(img, .25);
where img is the original image. Depending on the image you might have to adjust the second parameters (which ranges from 0 to 1) so that you only get the cross. Don't worry about the cross not being fully connected because we'll remedy that in the next step.
2.Dilate the image to join the parts. I had to do this twice because I had to set the threshold so low on the binary image conversion (some parts of your image were pretty dark). Dilation essentially just adds pixels around existing white pixels (I'll also be inverting the binary image as I send it into bwmorph because the operations are made to act on white pixels which are the ones that have a value of 1).
bw2 = bwmorph(~bw, 'dilate', 2);
The last parameter says how many times to do the dilation operation.
3.Shrink the image to a point.
bw3 = bwmorph(bw2, 'shrink',Inf);
Again, the last parameter says how many times to perform the operation. In this case I put in Inf which shrinks until there is only one pixel that is white (in other words a 1).
4.Find the pixel that is still a 1.
[i,j] = find(bw3);
Here, i is the row and j is the column of the pixel in bw3 such that bw3(i,j) is equal to 1. All the other pixels should be 0 in bw3.
There might be other ways to do this with bwmorph, but I think that this way works pretty well. You might have to adjust it depending on the picture too. I can include images of each step if desired.
I just encountered the same kind of problem, and I found other solutions that I would like to share:
Assume image file name is pict1.jpg.
1.Read input image, crop relevant part and covert to Gray-scale:
origI = imread('pict1.jpg'); %Read input image
I = origI(32:304, 83:532, :); %Crop relevant part
I = im2double(rgb2gray(I)); %Covert to Grayscale and to double (set pixel range [0, 1]).
2.Convert image to binary image in robust approach:
%Subtract from each pixel the median of its 21x21 neighbors
%Emphasize pixels that are deviated from surrounding neighbors
medD = abs(I - medfilt2(I, [21, 21], 'symmetric'));
%Set threshold to 5 sigma of medD
thresh = std2(medD(:))*5;
%Convert image to binary image using above threshold
BW = im2bw(medD, thresh);
BW Image:
3.Now I suggest two approaches for finding the center:
Find find centroid (find center of mass of the white cluster)
Find two lines using Hough transform, and find the intersection point
Both solutions return sub-pixel result.
3.1.Find cross center using regionprops (find centroid):
%Find centroid of the cross (centroid of the cluster)
s = regionprops(BW, 'centroid');
centroids = cat(1, s.Centroid);
figure;imshow(BW);
hold on, plot(centroids(:,1), centroids(:,2), 'b*', 'MarkerSize', 15), hold off
%Display cross center in original image
figure;imshow(origI), hold on, plot(82+centroids(:,1), 31+centroids(:,2), 'b*', 'MarkerSize', 15), hold off
Centroid result (BW image):
Centroid result (original image):
3.2 Find cross center by intersection of two lines (using Hough transform):
%Create the Hough transform using the binary image.
[H,T,R] = hough(BW);
%ind peaks in the Hough transform of the image.
P = houghpeaks(H,2,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
%Find lines and plot them.
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(BW), hold on
L = cell(1, length(lines));
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
%http://robotics.stanford.edu/~birch/projective/node4.html
%Find lines in homogeneous coordinates (using cross product):
L{k} = cross([xy(1,1); xy(1,2); 1], [xy(2,1); xy(2,2); 1]);
end
%https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
%Lines intersection in homogeneous coordinates (using cross product):
p = cross(L{1}, L{2});
%Convert from homogeneous coordinate to euclidean coordinate (divide by last element).
p = p./p(end);
plot(p(1), p(2), 'x', 'LineWidth', 1, 'Color', 'white', 'MarkerSize', 15)
Hough transform result:
I think that there is a far simpler way of solving this. The lines which form the cross-hair are of equal length. Therefore it in will be symmetric in all orientations. So if we do a simple line scan horizontally as well as vertically, to find the extremities of the lines forming the cross-hair. the median of these values will give the x and y co-ordinates of the center. Simple geometry.
I just love these discussions of how to find something without defining first what that something is! But, if I had to guess, I’d suggest the center of mass of the original gray scale image.
What about this;
a) convert to binary just to make the algorithm faster.
b) Perform a find on the resulting array
c) choose the element which has either lowest/highest row/column index (you would have four points to choose from then
d) now keep searching neighbours
have a global criteria for search that if search does not result in more than a few iterations, the point selected is false and choose another extreme point
e) going along the neighbouring points, you will end up at a point where you have three possible neighbours.That is you intersection
I would start by using the grayscale image map. The darkest points are on the cross, so discriminating on the highest values is a starting point. After discrimination, set all the lower points to white and leave the rest as they are. This would maximize the contrast between points on the cross and points in the image. Next up is to come up with a filter for determining the position with the highest average values. I would step through the entire image with a NxM array and take the mean value at the center point. Create a new array of these means and you should have the highest mean at the intersection. I'm curious to see how someone else may try this!