A list,

Hough Transform is a feature extraction technique in image processing. It detects objects with specific shapes through a voting algorithm. The process computs the local maximum value of the cumulative results in a parameter space to obtain a set that conforms to this particular shape as the Result of the Hough transform. Hough transform was first proposed by Paul Hough in 1962 [53], and then popularized and used by Richard Duda and Peter Hart in 1972 [54]. Classical Hough transform was used to detect straight lines in images, and Hough transform was later extended to the recognition of objects with arbitrary shapes, mostly circles and ellipses.

Hough transform using the transformation between two coordinates to space will have the same shape in a space curve or straight line map to another space coordinates of a point on the peak, it is transformed into the problem of detection of any shape statistics peak, in the previous section has been introduced in the driveway of linear features, this section introduces the principle of hough transform to detect linear and test results.

As we know, a line can be represented by y=kx+b in cartesian coordinate system. The main idea of Hough transformation is to exchange the parameters and variables of the equation, that is, x and y as known quantities k and B as variable coordinates, so the line y=kx+b in cartesian coordinate system is represented as point (k,b) in parameter space. And a point (x1,y1) is represented in rectangular coordinates as a line y1=x1 times k plus b, where (k,b) is any point on the line. For the convenience of calculation, we express the coordinates of the parameter space as γ and θ in polar coordinates. Since the points on the same line correspond to (γ,θ) are the same, the edge detection of the image can be carried out first, and then every non-zero pixel point on the image can be transformed into a straight line in parameter coordinates. Then points belonging to the same line in cartesian coordinates can form multiple lines in parameter space and intersect at a point. Therefore, this principle can be used for linear detection.



As shown in the figure, for any point (x,y) in the original figure, a straight line can be formed in the parameter space. Taking a straight line in the figure as an example, there is a parameter (γ,θ)=(69.641,30°). All points belonging to the same straight line intersect at a point in the parameter space, which is the parameter of the corresponding straight line. The (γ,θ) obtained from all the lines in the figure yields a series of corresponding curves in the parameter space as shown in the Tuhof statistical transformation results.

Ii. Source code

clear all;
close all;
clc;
P= imread('clock - 2. JPG');  %2/5/8/11no problem7/10the13Figure minute hand wrong16I = imresize(P,0.7); % Image reduction70%
% figure('NumberTitle'.'off'.'Name'.'original'); % imshow(I); % remove background [m,n]=size(I);for i=1:m 
    for j=1:n 
        if I(i,j)>100 
            I(i,j)=256; 
        end
    end
end
imshow(I); I1 = im2bw(I); % RGB converted to binary image % figure('NumberTitle'.'off'.'Name'.'Binarized image');
% imshow(I1);

% I2 = edge(I1,'canny'); % Canny operator extraction boundary % figure(3) % imshow(I2) I20 = imcomplement(I1); % Image invert % figure('NumberTitle'.'off'.'Name'.'Invert image');
 imshow(I20);

I2 = bwmorph(I20,'skel'.8); % Extract image skeleton6ci   N=6,7,8,9Will do10Photo taken3  11Photo taken5
% figure('NumberTitle'.'off'.'Name'.'Extract image skeleton'); imshow(I2); % test line [H,T,R] = hough(I2); figure('NumberTitle'.'off'.'Name'.'Peak extraction effect');
imshow(H,[],'XData',T,'YData',R, 'InitialMagnification'.'fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P  = houghpeaks(H,5.'threshold'.ceil(0.3*max(H(:))));
x = T(P(:,2)); 
y = R(P(:,1));
plot(x,y,'s'.'color'.'white'); % find the corresponding line edge lines = houghlines(I2,T,R,P,'FillGap'.5.'MinLength'.7); total = length(lines); % Total number of detected lines Figure, imshow(I2),hold on; % sec_line =0; % second hand length % min_line =0; % Minute hand length % hour_line =0; Array0 =[]; Array1 =[]; % The sorted lengthfor k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth'.2.'Color'.'green'); % marks the starting point of the edge of the line plot(xy(1.1),xy(1.2),'x'.'LineWidth'.2.'Color'.'yellow');
   plot(xy(2.1),xy(2.2),'x'.'LineWidth'.2.'Color'.'red'); Len = norm(lines(k).point1-lines (k).point2); array0(k)=len; Array1 = sort(array0, array0,'descend'); Find the straight line corresponding to the second hand, minute hand and hour hand, and calculate the slope and Anglefor k=1:length(lines)
    xy = [lines(k).point1; lines(k).point2];
    len = norm(lines(k).point1 - lines(k).point2);
    if(len == array1(1)) sec_line = xy; angle_s = lines(k).theta; % k_s = (xy() % k_s = (xy() % k_s = (xy()2.2)-xy(1.2))/(xy(2.1)-xy(1.1)) % second hand slope endif( len == array1(2))
        min_line = xy;
        angle_m = lines(k).theta;
%         k_m = (xy(2.2)-xy(1.2))/(xy(2.1)-xy(1.1))
    end
    if(len  == array1(3))
        hour_line = xy;
        angle_h = lines(k).theta;
%         k_h = (xy(2.2)-xy(1.2))/(xy(2.1)-xy(1.1))
    end      
end
  
%highlight the longest line segment
%plot(xy_long(:,1),xy_long(:,2),'LineWidth'.2.'Color'.'blue')
plot(sec_line(:,1),sec_line(:,2),'LineWidth'.2.'Color'.'b');
plot(min_line(:,1),min_line(:,2),'LineWidth'.2.'Color'.'c');
plot(hour_line(:,1),hour_line(:,2),'LineWidth'.2.'Color'.'r'); % to the usual XY coordinate systemif(angle_m < 0)
    angle_m= -angle_m + 90;
else
    angle_m = 90 - angle_m;
end

if(angle_h < 0)
    angle_h = -angle_h + 90;
else
    angle_h = 90 - angle_h;
end

if(angle_s < 0)
    angle_s = -angle_s + 90;
else
    angle_s = 90 - angle_s;
end
Copy the code

3. Operation results







Fourth, note

Version: 2014 a