Realization of Huffman transformation __OPENCV based on OPENCV

Source: Internet
Author: User
Tags cos scalar sin
Hough Transform Overview

Hough transform (Hough Transform) is a feature extraction technique in image processing, in which a set which accords with the characteristic is obtained by calculating the local maximum value of the cumulative result in a parametric space as a result of Hough transform. Hough Line Detection

Hough Line transform is a method to find the straight line, before looking for the Huffman transformation, we should detect the image edge, i.e. the input of Hough line is two-value image. principle

This means that the line is represented in polar coordinates in the form of r=x*cos (θ) +y*sin (θ), where each pair of passes (r,θ) represents a straight line through (x,y).

If given a point (8,6) then R=8*cos (θ) +6*sin (θ) corresponds to a sine curve.

Do the above for all points in the image, if the curves of two different points intersect on the plane (θ,r), it means that they are passing a straight line, so we can define how many curves intersect by setting the threshold of the point on the line so that a line is detected. Hough Transform in OpenCV

Two kinds of Hough line transform, standard Hough line transform and cumulative probability Hough line transform are introduced. the prototype of the Standard Hough line Transform is:

void Houghlines (inputarray image, Outputarray lines, double rho, double theta, int threshold, double srn=0, double stn=0)

Image: Input images, type binary image
Lines:lines type, storing vectors (p,θ)
Rho: Distance precision in pixels
Theta: angular precision in radians
Threshold: threshold, that is, the value that he must achieve in the cumulative plane when recognized as a line
SRN: Default is 0
STN defaults to 0, for more, please check the official documentation probabilistic Hough line Transform

The cumulative Hough transform is an improvement to the standard Hough transform, in which he carries out the Huffman transformation, calculates the direction and range of the individual segment, thus reducing the calculation and shortening the time.

void Houghlinesp (inputarray image, Outputarray lines, double rho, double theta, int threshold, double minlinelength=0, Dou ble maxlinegap=0)

Different points in the last two parameters.

Minlinelength: Indicates the length of the lowest segment, smaller than this can not be displayed
Maxlinegap: Allows the maximum distance to be connected between the same line of points and points

In the face of the cumulative Hough transform to show C + + code:

#include <opencv2/opencv.hpp> #include <iostream> using namespace std;
using namespace CV;
int thre, minlinelength, Maxlinegap;
Mat SRC, DST;
void On_houghlines (int, void *);
    int main () {Mat ori = imread ("road.jpg");
    Namedwindow ("DST");
    Imshow ("origin", the Ori);
    Canny (Ori, SRC, 50, 200,3);
    Cvtcolor (SRC, DST, CV_GRAY2BGR);
    Imshow ("Canny image", SRC);
    thre = 80;
    Minlinelength = 50;
    maxlinegap=10;
    Createtrackbar ("Threshold:", "DST", &thre, On_houghlines);
    Createtrackbar ("Minline:", "DST", &minlinelength, On_houghlines);

    Createtrackbar ("Maxgap:", "DST", &maxlinegap, On_houghlines);
    On_houghlines (thre, 0);
    On_houghlines (minlinelength, 0);
    On_houghlines (maxlinegap, 0);
    /*HOUGHLINESP (SRC, lines, 1, cv_pi/180, 50, 60, 10);
        for (size_t i = 0; i < lines.size (); i++) {vec4i L = lines[i];
    Line (DST, point (l[0), l[1]), point (L[2], l[3]), Scalar (186,88, 255));
  }*/  Imshow ("DST image", DST);
Waitkey (0);
    }//void on_houghlines (int, void *) {vector<vec4i> lines;
    Mat dstimage = Dst.clone ();
    Mat srcimage = Src.clone ();
    Houghlinesp (Srcimage, lines, 1, cv_pi/180, thre, Minlinelength, maxlinegap);
        for (size_t i = 0; i < lines.size (); i++) {vec4i L = lines[i];
    Line (Dstimage, point (L[0], l[1]), point (L[2), l[3]), Scalar (186, 88, 255), 2);
} imshow ("DST", dstimage); }

You can adjust the parameters so that the line segment of the road is detected and the effect is as follows:
The results of the edge detection of the original and canny operators:

Line Detection Effect Chart:

Hough Circle Detection

A straight line can be represented in polar coordinates (r,θ), a circle requires three parameters (X,y,r) to be represented, and the Hough gradient method is usually used to solve the problem of circle transformation. Hough Gradient Method

The principle of Hough gradient method is to detect the edge of the image first, use the canny, then consider each non 0 points in the edge image, consider its local gradient, calculate the Sobel first derivative of the x,y direction by the Sobel function, get the gradient, and use the obtained gradient, Each point in the line from the slope is added to the accumulator, and then from the accumulator, select the Candidate center circle.

Hough Circle Detection in OpenCV:

C + +: void Houghcircles (Inputarray image, Outputarray circles, int method, double DP, double mindist, double param1=100, do uble param2=100, int minradius=0, int maxradius=0)

Image: Input images, type binary image
Circles: The output vector of the detected circle is stored after the call (X,y,radius)
Method: Methods of detection, the position of Hoffman gradient method
DP: To detect the prototype of the accumulator image resolution and input image ratio of the Tao said, Dp=1, accumulator and input image has the same resolution.
Mindist: The minimum distance of the detected Circle Center, which allows the algorithm to distinguish between different circle diameters
PARAM1: Default 100, which indicates a high threshold passed to the canny edge operator
Param2: said in the detection phase of the cumulative threshold of the center, he is smaller, the more you can detect more simply do not exist in the plan, the bigger, the test plan is closer to the perfect circle
Minradius: Represents the minimum value of a radius
Maxradiux: Represents the maximum value of a radius

example of a C + + version:

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace CV;
int main () {
    Mat src = imread ("planet_glow.jpg");
    Namedwindow ("origin");
    Imshow ("origin", SRC);
    Turn out the grayscale, and smooth, because to carry out edge detection, it is best to smooth
    Mat gray;
    Cvtcolor (src, Gray, cv_bgr2gray);
    Imshow ("Gray", gray);
    Mat DST;
    Medianblur (Gray, DST, 5);
    Imshow ("Median filter", DST);
    Vector<vec3f> circles;
    Houghcircles (DST, circles, hough_gradient,1,120,100,30,0,0);
    for (size_t i = 0; i < circles.size (); i++) {Point
        Center (cvround (circles[i][0)), Cvround (circles[i][1));  
        int radius = Cvround (circles[i][2]);
        Draw Center
        Circle (SRC, center, 2, Scalar (0, 255, 0), 2);
        Draw Contour
        Circle (SRC, center, RADIUS, Scalar (0, 255, 0), 2);
    Imshow ("result", SRC);
    Waitkey (0);
}

python version:

Import cv2
import NumPy as np

planets = cv2.imread (' planet_glow.jpg ')
gray_img = Cv2.cvtcolor (planets, Cv2. Color_bgr2gray)
img = Cv2.medianblur (gray_img, 5)
cimg = Cv2.cvtcolor (img,cv2. COLOR_GRAY2BGR)

circles = Cv2. Houghcircles (Img,cv2. hough_gradient,1,120,
                            param1=100,param2=30,minradius=0,maxradius=0)

circles = np.uint16 (Np.around ( circles)) for

I in Circles[0,:]:
    # Draw the outer circle Cv2.circle
    (Planets, (i[0],i[1)), i[2], (0,255,0), 2
    # Draw the center of the Circle
    cv2.circle (Planets, (i[0],i[1)), 2, (0,0,255), 3)

cv2.imwrite ("Planets_ Circles.jpg ", planets)
cv2.imshow (" Houghcirlces ", Planets) Cv2.waitkey (
)
cv2.destroyallwindows ()

The results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.