Hough transformation is a feature extraction technology in image processing. This process in a parameter space calculates the local maximum value of the accumulated results to a set that conforms to a specific shape as the hough transformation result.
History of development:
First proposed by Paul Hough in 1962 to detect straight lines and curves.
Promoted by Richard Duda & Peter Hart in 1972, it expanded to the recognition of objects of any shape.
principle:
A straight line is represented in a rectangular coordinate system as y=k*x+b, and in a polar coordinate system as r=x*cos(theta)+y*sin(theta). The idea of hough transformation is that a point in the rectangular coordinate system corresponds to a straight line under the polar coordinate system, and similarly, a point in the polar coordinate system corresponds to a straight line under the rectangular coordinate system. In a straight line in a rectangular coordinate system, the slope and intercept are certain, so that all points on this straight line are focused at a point in the polar coordinate system, and such a focus point represents the straight line in the rectangular coordinate system.
For the straight line x=c, in practical applications, the parameter equation p=x*cos(theta)+y*sin(theta) is used. In this way, a point on the image plane corresponds to a curve on the parameter r-theta plane, and the rest are the same.
Standard hough transformation:
Imgproc.HoughLines(Mat image, Mat lines, double rho, double theta, int threshold, double srn, double stn, double min_theta, double max_theta)
Parameter description:
image: source image
lines: The output vector of the detected line is stored after the hough transformation
rho: distance accuracy in pixels
theta: Angle accuracy in radians
threshold: The value that must be reached when a part is a straight line
srn: The divisor distance of the rho parameter, with a default value of 0
stn: The divisor distance of theta parameter, default value 0
min_theta: Minimum angle of the detected line
max_theta: Maximum angle of the detected line
Sample code:
public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat srcImage = Imgcodecs.imread("F://6597210504144579394.jpg"); Mat dstImage = srcImage.clone(); Imgproc.Canny(srcImage, dstImage, 400, 500, 5, false); Mat storage = new Mat(); Imgproc.HoughLines(dstImage, storage, 1, Math.PI / 180, 200, 0, 0, 0, 10); for (int x = 0; x < storage.rows(); x++) { double[] vec = storage.get(x, 0); double rho = vec[0]; double theta = vec[1]; Point pt1 = new Point(); Point pt2 = new Point(); double a = Math.cos(theta); double b = Math.sin(theta); double x0 = a * rho; double y0 = b * rho; pt1.x = Math.round(x0 + 1000 * (-b)); pt1.y = Math.round(y0 + 1000 * (a)); pt2.x = Math.round(x0 - 1000 * (-b)); pt2.y = Math.round(y0 - 1000 * (a)); if (theta >= 0) { Imgproc.line(srcImage, pt1, pt2, new Scalar(255, 255, 255, 255), 1, Imgproc.LINE_4, 0); } } Imgcodecs.imwrite("F://dst2.jpg", srcImage); } Cumulative probability hough transformation:
Imgproc.HoughLinesP(Mat image, Mat lines, double rho, double theta, int threshold, double minLineLength, double maxLineGap)
Parameter description:
image: source image
lines: The output vector of the detected line is stored after the hough transformation
rho: distance accuracy in pixels
theta: Angle accuracy in radians
threshold: The value that must be reached when a part is a straight line
minLineLength: The length of the lowest line segment, default to 0
maxLineGap: The maximum distance allowed to connect points to points on the same line, default is 0
Sample code:
public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat srcImage = Imgcodecs.imread("F://6597210504144579394.jpg"); Mat dstImage = srcImage.clone(); Imgproc.Canny(srcImage, dstImage, 400, 500, 5, false); Mat storage = new Mat(); Imgproc.HoughLinesP(dstImage, storage, 1, Math.PI / 180, 50, 0, 0); for (int x = 0; x < storage.rows(); x++) { double[] vec = storage.get(x, 0); double x1 = vec[0], y1 = vec[1], x2 = vec[2], y2 = vec[3]; Point start = new Point(x1, y1); Point end = new Point(x2, y2); Imgproc.line(srcImage, start, end, new Scalar(255, 255, 255, 255), 1, Imgproc.LINE_4, 0); } Imgcodecs.imwrite("F://dst2.jpg", srcImage); } Source image:
Standard hough transformation results:
The cumulative probability hough transformation result:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.