霍夫线变换以在图像中查找多边形
Hough line transform to find polygons in an image
我试图找到下图中的所有多边形(包括填充的多边形)。目前,我正在尝试使用 Hough Transform 来完成此操作,但它没有检测到图像中的所有线条。此外,由于线条有多宽,它会将每条线条计算两次。有没有一种方法可以对图像应用一些过滤器以使霍夫变换性能更好,或者是否有一种完全不同的方法来找到我丢失的多边形?谢谢!
这是我正在处理的图片,
下面是我的代码。
import cv2
import numpy as np
img = cv2.imread('test.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
for num in range (0, len(lines)):
for x1,y1,x2,y2 in lines[num]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imwrite('houghlines.jpg',img)
我认为使用 findContours 更容易解决这个问题。
解决方法如下,我把意见写下来,有什么问题欢迎追问。
void detect_by_contour()
{
//Following comments are written for non c++ programmers
auto img = cv::imread("../forum_quest/data/yd8pA.png");
if(img.empty()){
throw std::runtime_error("cannot open image");
}
cv::Mat gray_img;
cv::cvtColor(img, gray_img, CV_BGR2GRAY);
cv::Mat thin_img;
//make your lines as thin as possible
morphology_skeleton(gray_img, thin_img);
std::vector<std::vector<cv::Point>> contours;
cv::findContours(thin_img, contours, cv::RETR_EXTERNAL,
cv::CHAIN_APPROX_SIMPLE);
//remove contour if the area less than 100
auto it = std::remove_if(std::begin(contours), std::end(contours),
[](std::vector<cv::Point> const &a)
{
return cv::boundingRect(a).area() < 100;
});
//remove_if move unwanted elements to the backyard of the containers
//you need to call the erase function of the containers to remove
//unwanted elements
contours.erase(it, std::end(contours));
//contour_analyzer is a class used to print out statistic info
//of the contour
ocv::contour_analyzer analyzer;
//print_contour_attribute_name print out the attribute names
//of the contours as following
//CArea | BArea | Perimeter | Aspect | Extent | Solidity | PolySize
ocv::print_contour_attribute_name(std::cout);
for(size_t i = 0; i != contours.size(); ++i){
cv::drawContours(img, contours, static_cast<int>(i), {0,255,0}, 2);
std::cout<<analyzer.analyze(contours[i], 0.1);
cv::imshow("img", img);
cv::waitKey();
}
cv::imwrite("polygon.jpg", img);
}
如果你运行程序(我正在使用从github克隆的opencv3)。你会发现有5个轮廓
他们的属性是
您可以尝试通过这些属性来判断多边形的类型。
detect_by_contour和morphology_skeleton的代码位于forum_quest。
ocv::contour_analyzer分析器和ocv::contour_analyzer的代码位于ocv_libs.
我试图找到下图中的所有多边形(包括填充的多边形)。目前,我正在尝试使用 Hough Transform 来完成此操作,但它没有检测到图像中的所有线条。此外,由于线条有多宽,它会将每条线条计算两次。有没有一种方法可以对图像应用一些过滤器以使霍夫变换性能更好,或者是否有一种完全不同的方法来找到我丢失的多边形?谢谢!
这是我正在处理的图片,
下面是我的代码。
import cv2
import numpy as np
img = cv2.imread('test.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
for num in range (0, len(lines)):
for x1,y1,x2,y2 in lines[num]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imwrite('houghlines.jpg',img)
我认为使用 findContours 更容易解决这个问题。
解决方法如下,我把意见写下来,有什么问题欢迎追问。
void detect_by_contour()
{
//Following comments are written for non c++ programmers
auto img = cv::imread("../forum_quest/data/yd8pA.png");
if(img.empty()){
throw std::runtime_error("cannot open image");
}
cv::Mat gray_img;
cv::cvtColor(img, gray_img, CV_BGR2GRAY);
cv::Mat thin_img;
//make your lines as thin as possible
morphology_skeleton(gray_img, thin_img);
std::vector<std::vector<cv::Point>> contours;
cv::findContours(thin_img, contours, cv::RETR_EXTERNAL,
cv::CHAIN_APPROX_SIMPLE);
//remove contour if the area less than 100
auto it = std::remove_if(std::begin(contours), std::end(contours),
[](std::vector<cv::Point> const &a)
{
return cv::boundingRect(a).area() < 100;
});
//remove_if move unwanted elements to the backyard of the containers
//you need to call the erase function of the containers to remove
//unwanted elements
contours.erase(it, std::end(contours));
//contour_analyzer is a class used to print out statistic info
//of the contour
ocv::contour_analyzer analyzer;
//print_contour_attribute_name print out the attribute names
//of the contours as following
//CArea | BArea | Perimeter | Aspect | Extent | Solidity | PolySize
ocv::print_contour_attribute_name(std::cout);
for(size_t i = 0; i != contours.size(); ++i){
cv::drawContours(img, contours, static_cast<int>(i), {0,255,0}, 2);
std::cout<<analyzer.analyze(contours[i], 0.1);
cv::imshow("img", img);
cv::waitKey();
}
cv::imwrite("polygon.jpg", img);
}
如果你运行程序(我正在使用从github克隆的opencv3)。你会发现有5个轮廓
他们的属性是
您可以尝试通过这些属性来判断多边形的类型。
detect_by_contour和morphology_skeleton的代码位于forum_quest。 ocv::contour_analyzer分析器和ocv::contour_analyzer的代码位于ocv_libs.