Find intersection in noisy Hough transform - matlab

I have various sets of points, which are supposed to be part of different lines (see image below). What I want to do is to group the points which are part of the same line. To do so, I made a Hough transform that turns cartesian coordinates into polar one. What I need to do is to locate the position of the intersection in the Hough plane; do you have any suggestion on how to do it?
This is what I am doing now:
Apply a threshold to the image of the Hough transform, so to
consider only the pixels with at least half of the maximum intensity
max_Hough = max(max(Hough));
for ii = 1:dim_X
for jj = 1:dim_Y
if Hough(ii,jj) > max_Hough*0.5
Hough_bin(ii,jj) = Hough(ii,jj);
end
end
end
Locate the blobs in the corresponding image
se = strel('disk',1);
Hough_final = imclose(Hough_bin,se);
[L,num] = bwlabel(Hough_final);
% Let's exclude the single points
counts = sum(bsxfun(#eq,L(:),1:num));
valid_blobs = find(counts>threshold);
number_valid = length(find(counts>threshold));
if (number_valid~=0)
for blob_num = (1:number_valid)
for x = (1:dim_X)
for y = (1:dim_X)
if (L(x,y) == valid_blobs(blob_num))
hist_bin_clean(x,y) = Hough_final(x,y);
end
end
end
end
end
Find the centroids of the blobs
Ilabel = bwlabel(hist_bin_clean);
stat = regionprops(Ilabel,'centroid');
You can see the result in the second image below, with the centroid pointed by the white arrow. I would expect it to be a few pixels higher and to the left... How would you improve the result?

Related

How can I measure the length of line segments in image mask in MATLAB?

I have a binary image (BW_roi.mat), displayed below. And I wanted to measure the length of every line segment.
I tried the solution given in this link hough-transform. But it does not work out for me. It just measured the length of some lines as shown below.
I tried the other code
clc; clear all; close all;
load BW_ROI.mat
boundaries = bwboundaries(BW_roi_nodot);
patchno=1 %Let select patch 1
b = boundaries{patchno}; % Extract N by 2 matrix of (x,y) locations.
x = b(:, 1);
y = b(:, 2);
It though gives me points (x,y) that make up these polygons. But how can I get the line segment length of specific patch?
I'd propose using convexhull in combination with the algorithm here to reduce the number of vertices in the detected polygons. Seems to work pretty well. I'll leave it to you to use the adjacency matrix produced by bwboundaries to throw away duplicates.
load BW_ROI.mat
[B,L,N,A] = bwboundaries(BW_roi_nodot);
for k = 1:N
boundary = fliplr(B{k});
% take a convex hull of the bounded polygon
idxn = convhull(boundary, 'Simplify', true);
% use this algorithm to further simplify the polygon
% https://ww2.mathworks.cn/matlabcentral/fileexchange/41986-ramer-douglas-peucker-algorithm
point_reduced = DouglasPeucker(boundary(idxn,:), 5);
% make the shape, get the side lengths
shp = polyshape(point_reduced);
vertex_deltas = diff([shp.Vertices; shp.Vertices(1,:)], 1);
edge_lengths = sqrt(sum(vertex_deltas.^2, 2));
% plot animation
imshow(BW_roi_nodot), hold on
plot(shp), drawnow, pause(0.5)
end

Searching Across a Line in a Matrix in Octave

The attached image has a line with a break in it.
My code finds the line using a hough transform resulting in r=32 and theta=2.3213. The hough transform isn't perfect, the angle (especially with a more complex image) is always off by a little bit, and in this case, because of the edge detection, the line is offset. I want to read values across the line to find the breaks in it. In order to do this, I will need to be able to sample values on either side of the line to find where the maximum density of the line is.
Further explanation (if you want it):
If you look closely at the image you can see areas where the line crosses a pixel pretty much dead on resulting in a value of nearly 1/white. Other areas have two pixels side by side with values of about .5/gray. I need to find a solution that takes into account the anti-aliasing of the line, and allows me to extract the breaks in it.
%Program Preparation
clear ; close all; clc %clearing command window
pkg load image %loading image analyzation suite
pkg load optim
%Import Image
I_original = imread("C:/Users/3015799/Desktop/I.jpg");
%Process Image to make analysis quicker and more effective
I = mat2gray(I_original); %convert to black and white
I = edge(I, 'sobel');
%Perform Hough Transform
angles = pi*[-10:189]/180;
hough = houghtf(I,"line",angles);
%Detect hot spots in hough transform
detect = hough>.5*max(hough(:));
%Shrink hotspots to geometric center, and index
detect = bwmorph(detect,'shrink',inf);
[ii, jj] = find(detect);
r = ii - (size(hough,1)-1)/2;
theta = angles(jj);
%Cull duplicates. i.e outside of 0-180 degrees
dup = theta<-1e-6 | theta>=pi-1e-6;
r(dup) = [];
theta(dup) = [];
%Compute line parameters (using Octave's implicit singleton expansion)
r = r(:)'
theta = theta(:)'
x = repmat([1;1133],1,length(r)); % 2xN matrix, N==length(r)
y = (r - x.*cos(theta))./sin(theta); % solve line equation for y
%The above goes wrong when theta==0, fix that:
horizontal = theta < 1e-6;
x(:,horizontal) = r(horizontal);
y(:,horizontal) = [1;:];
%Plot
figure
imshow(I)
hold on
plot(y,x,'r-','linewidth',2)
If you are only interested in the length of the gap, this would be very easy:
clear all
pkg load image
img_fn = "input.jpg";
if (! exist (img_fn, "file"))
urlwrite ("https://i.stack.imgur.com/5UnpO.jpg", img_fn);
endif
Io = imread(img_fn);
I = im2bw (Io);
r = max(I);
c = max(I');
ri = find (diff(r));
ci = find (diff(c));
## both should have 4 elements (one break)
assert (numel (ri) == 4);
assert (numel (ci) == 4);
## the gap is in the middle
dx = diff(ri(2:3))
dy = diff(ci(2:3))
# the length is now easy
l = hypot (dy, dx)
gives
dx = 5
dy = 5
l = 7.0711
without any hogh transform. Of course you have to also check the corener cases for horizontal and vertical lines but this should give you an idea

Find the real time co-ordinates of the four points marked in red in the image

To be exact I need the four end points of the road in the image below.
I used find[x y]. It does not provide satisfying result in real time.
I'm assuming the images are already annotated. In this case we just find the marked points and extract coordinates (if you need to find the red points dynamically through code, this won't work at all)
The first thing you have to do is find a good feature to use for segmentation. See my SO answer here what-should-i-use-hsv-hsb-or-rgb-and-why for code and details. That produces the following image:
we can see that saturation (and a few others) are good candidate colors spaces. So now you must transfer your image to the new color space and do thresholding to find your points.
Points are obtained using matlab's region properties looking specifically for the centroid. At that point you are done.
Here is complete code and results
im = imread('http://i.stack.imgur.com/eajRb.jpg');
HUE = 1;
SATURATION = 2;
BRIGHTNESS = 3;
%see https://stackoverflow.com/questions/30022377/what-should-i-use-hsv-hsb-or-rgb-and-why/30036455#30036455
ViewColoredSpaces(im)
%convert image to hsv
him = rgb2hsv(im);
%threshold, all rows, all columns,
my_threshold = 0.8; %determined empirically
thresh_sat = him(:,:,SATURATION) > my_threshold;
%remove small blobs using a 3 pixel disk
se = strel('disk',3');
cleaned_sat = imopen(thresh_sat, se);% imopen = imdilate(imerode(im,se),se)
%find the centroids of the remaining blobs
s = regionprops(cleaned_sat, 'centroid');
centroids = cat(1, s.Centroid);
%plot the results
figure();
subplot(2,2,1) ;imshow(thresh_sat) ;title('Thresholded saturation channel')
subplot(2,2,2) ;imshow(cleaned_sat);title('After morpphological opening')
subplot(2,2,3:4);imshow(im) ;title('Annotated img')
hold on
for (curr_centroid = 1:1:size(centroids,1))
%prints coordinate
x = round(centroids(curr_centroid,1));
y = round(centroids(curr_centroid,2));
text(x,y,sprintf('[%d,%d]',x,y),'Color','y');
end
%plots centroids
scatter(centroids(:,1),centroids(:,2),[],'y')
hold off
%prints out centroids
centroids
centroids =
7.4593 143.0000
383.0000 87.9911
435.3106 355.9255
494.6491 91.1491
Some sample code would make it much easier to tailor a specific solution to your problem.
One solution to this general problem is using impoint.
Something like
h = figure();
ax = gca;
% ... drawing your image
points = {};
points = [points; impoint(ax,initialX,initialY)];
% ... generate more points
indx = 1 % or whatever point you care about
[currentX,currentY] = getPosition(points{indx});
should do the trick.
Edit: First argument of impoint is an axis object, not a figure object.

How to draw a straight across the centroid points of the barcode using best fit points Matlab

This is the processed image and I can't increase the bwareaopen() as it won't work for my other image.
Anyway I'm trying to find the shortest points in the centre points of the barcode, to get the straight line across the centre points in the barcode.
Example:
After doing a centroid command, the points in the barcode are near to each other. Therefore, I just wanted to get the shortest points(which is the barcode) and draw a straight line across.
All the points need not be join, best fit points will do.
Step 1
Step 2
Step 3
If you dont have the x,y elements Andrey uses, you can find them by segmenting the image and using a naive threshold value on the area to avoid including the number below the bar code.
I've hacked out a solution in MATLAB doing the following:
Loading the image and making it binary
Extracting all connected components using bwlabel().
Getting useful information about each of them via regionprops() [.centroid will be a good approximation to the middel point for the lines].
Thresholded out small regions (noise and numbers)
Extracted x,y coordinates
Used Andreys linear fit solution
Code:
set(0,'DefaultFigureWindowStyle','docked');
close all;clear all;clc;
Im = imread('29ekeap.jpg');
Im=rgb2gray(Im);
%%
%Make binary
temp = zeros(size(Im));
temp(Im > mean(Im(:)))=1;
Im = temp;
%Visualize
f1 = figure(1);
imagesc(Im);colormap(gray);
%Find connected components
LabelIm = bwlabel(Im);
RegionInfo = regionprops(LabelIm);
%Remove background region
RegionInfo(1) = [];
%Get average area of regions
AvgArea = mean([RegionInfo(1:end).Area]);
%Vector to keep track of likely "bar elements"
Bar = zeros(length(RegionInfo),1);
%Iterate over regions, plot centroids if area is big enough
for i=1:length(RegionInfo)
if RegionInfo(i).Area > AvgArea
hold on;
plot(RegionInfo(i).Centroid(1),RegionInfo(i).Centroid(2),'r*')
Bar(i) = 1;
end
end
%Extract x,y points for interpolation
X = [RegionInfo(Bar==1).Centroid];
X = reshape(X,2,length(X)/2);
x = X(1,:);
y = X(2,:);
%Plot line according to Andrey
p = polyfit(x,y,1);
xMin = min(x(:));
xMax = max(x(:));
xRange = xMin:0.01:xMax;
yRange = p(1).*xRange + p(2);
plot(xRange,yRange,'LineWidth',2,'Color',[0.9 0.2 0.2]);
The result is a pretty good fitted line. You should be able to extend it to the ends by using the 'p' polynomal and evaluate when you dont encounter any more '1's if needed.
Result:
If you already found the x,y of the centers, you should use polyfit function:
You will then find the polynomial coefficients of the best line. In order to draw a segment, you can take the minimal and maximal x
p = polyfit(x,y,1);
xMin = min(x(:));
xMax = max(x(:));
xRange = xMin:0.01:xMax;
yRange = p(1).*xRange + p(2);
plot(xRange,yRange);
If your ultimate goal is to generate a line perpendicular to the bars in the bar code and passing roughly through the centroids of the bars, then I have another option for you to consider...
A simple solution would be to perform a Hough transform to detect the primary orientation of lines in the bar code. Once you find the angle of the lines in the bar code, all you have to do is rotate that by 90 degrees to get the slope of a perpendicular line. The centroid of the entire bar code can then be used as an intercept for this line. Using the functions HOUGH and HOUGHPEAKS from the Image Processing Toolbox, here's the code starting with a cropped version of your image from step 1:
img = imread('bar_code.jpg'); %# Load the image
img = im2bw(img); %# Convert from RGB to BW
[H, theta, rho] = hough(img); %# Perform the Hough transform
peak = houghpeaks(H); %# Find the peak pt in the Hough transform
barAngle = theta(peak(2)); %# Find the angle of the bars
slope = -tan(pi*(barAngle + 90)/180); %# Compute the perpendicular line slope
[y, x] = find(img); %# Find the coordinates of all the white image points
xMean = mean(x); %# Find the x centroid of the bar code
yMean = mean(y); %# Find the y centroid of the bar code
xLine = 1:size(img,2); %# X points of perpendicular line
yLine = slope.*(xLine - xMean) + yMean; %# Y points of perpendicular line
imshow(img); %# Plot bar code image
hold on; %# Add to the plot
plot(xMean, yMean, 'r*'); %# Plot the bar code centroid
plot(xLine, yLine, 'r'); %# Plot the perpendicular line
And here's the resulting image:

Houghlines in MATLAB

After detecting the lines in an image using Hough lines, how can I use it to calculate the change in angle (rotation) of the lines of a reference image?
Note to readers: This is a follow-up question, refer to these for background:
How to select maximum intensity in Hough transform in MATLAB?
Calculating displacement moved in MATLAB
The process is similar to what I showed before. Below I am using the images from your previous question (since you provided only one, I created the other by rotating the first by 10 degrees).
We start by detecting the lines for the two images. We do this with the help of the Hough transform functions. This what it looks like applied to both images:
Next, we want to perform image registration using the line endpoints as control-points. First, we make sure the points correspond to each other in the two images. I do this by computing the convex hull using convhull which automatically sorts them in counterclockwise-order (or is it in the opposite direction!). The numbers shown above indicate the order.
Finally, we use the function cp2tform to get the transformation matrix, which we use to align the images and extract the translation, rotation, and scaling.
The following is the complete code:
%% # Step 1: read and prepare images
%# (since you provided only one, I created the other by rotating the first).
I1 = imread('http://i.stack.imgur.com/Se6zX.jpg');
I1 = rgb2gray( imcrop(I1, [85 35 445 345]) ); %# Get rid of white border
I2 = imrotate(I1, -10, 'bilinear', 'crop'); %# Create 2nd by rotating 10 degrees
%% # Step 2: detect the cross sign endpoints (sorted in same order)
p1 = getCross(I1);
p2 = getCross(I2);
%% # Step 3: perform Image Registration
%# Find transformation that maps I2 to I1 using the 4 control points for each
t = cp2tform(p2,p1,'affine');
%# Transform I2 to be aligned with I1
II2 = imtransform(I2, t, 'XData',[1 size(I1,2)], 'YData',[1 size(I1,1)]);
%# Plot
figure('menu','none')
subplot(131), imshow(I1), title('I1')
subplot(132), imshow(I2), title('I2')
subplot(133), imshow(II2), title('I2 (aligned)')
%# Recover affine transformation params (translation, rotation, scale)
ss = t.tdata.Tinv(2,1);
sc = t.tdata.Tinv(1,1);
tx = t.tdata.Tinv(3,1);
ty = t.tdata.Tinv(3,2);
scale = sqrt(ss*ss + sc*sc)
rotation = atan2(ss,sc)*180/pi
translation = [tx ty]
And here's the function that extract the lines endpoints:
function points = getCross(I)
%# Get edges (simply by thresholding)
I = imfilter(I, fspecial('gaussian', [7 7], 1), 'symmetric');
BW = imclearborder(~im2bw(I, 0.5));
%# Hough transform
[H,T,R] = hough(BW);
%# Detect peaks
P = houghpeaks(H, 2);
%# Detect lines
lines = houghlines(BW, T, R, P);
%# Sort 2D points in counterclockwise order
points = [vertcat(lines.point1); vertcat(lines.point2)];
idx = convhull(points(:,1), points(:,2));
points = points(idx(1:end-1),:);
end
with the result:
scale =
1.0025
rotation =
-9.7041
translation =
32.5270 -38.5021
The rotation is recovered as almost 10 degrees (with some inevitable error), and scaling is effectively 1 (meaning there was no zooming). Note that there was a translation component in the above example, because rotation was not performed around the center of the cross sign).
I am not sure what the MATLAB implementation of the Hough transform is, but the orientation of the line will be simply be at a right angle (90 degrees or pi/2 radians) to the angle you've used to identify the line in the first place.
I hope that helps. There's decent coverage of Hough transforms on the web and Wikipedia is a good place to start.