利用霍夫变换提取矩形的角点坐标
背景:一张图包含矩形,要提取其中矩形的角点。
思路:对图片进行概率霍夫变换线变换,再筛选出特定矩形的边,求两个边的直线角点
流程:
代码:
主函数文件
//----------------------------------------------------------------------------------------------
#include
#include
#include "opencv2/imgproc/imgproc_c.h"
#include
#include
#include
//-----------------------------------【命名空间声明部分】---------------------------------------
// 描述:包含程序所使用的命名空间
//-----------------------------------------------------------------------------------------------
using namespace cv;
using namespace std;
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-----------------------------------------------------------------------------------------------
Point2f getCrossPoint(Vec4i LineA, Vec4i LineB)
{
double ka, kb;
ka = (double)(LineA[3] - LineA[1]) / (double)(LineA[2] - LineA[0]); //求出LineA斜率
kb = (double)(LineB[3] - LineB[1]) / (double)(LineB[2] - LineB[0]); //求出LineB斜率
Point2f crossPoint;
crossPoint.x = (ka*LineA[0] - LineA[1] - kb*LineB[0] + LineB[1]) / (ka - kb);
crossPoint.y = (ka*kb*(LineA[0] - LineB[0]) + ka*LineB[1] - kb*LineA[1]) / (ka - kb);
return crossPoint;
}
int main( )
{
//【1】载入原始图和Mat变量定义
Mat srcImage = imread("/home/xiao/projects/jiqishijue/3.1M.bmp",CV_LOAD_IMAGE_COLOR); //工程目录下应该有一张名为1.jpg的素材图
Mat midImage,dstImage;//临时变量和目标图的定义
//【2】进行边缘检测和转化为灰度图
//模糊
cv::blur(srcImage, srcImage, cv::Size(3, 3));
Canny(srcImage, midImage, 100, 200, 3);//进行一此canny边缘检测
cvtColor(midImage,dstImage, cv::COLOR_GRAY2BGR);//转化边缘检测后的图为灰度图
//【3】进行霍夫线变换
vector lines;//定义一个矢量结构lines用于存放得到的线段矢量集合
HoughLinesP(midImage, lines, 1, CV_PI/360, 30, 10, 10 );
//【4】依次在图中绘制出每条线段
int x1 ;
int x2 ;
int y1 ;
int y2;
x1=50 ; //横坐标的范围
x2=595 ;
y1=47 ; //纵坐标的范围
y2=600;
for( size_t i = 0; i < lines.size(); i++ )
{
//如果线段的端点在矩形范围内,则画出这条线
if(lines[i][0] >x1 && lines[i][0] < x2 && lines[i][1] > y1 && lines[i][1] < y2){
Vec4i l = lines[i];
line( dstImage, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(186,88,255), 1, CV_AA);
}
/*
//画出线段延长得到的直线
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
line( dstImage, pt1, pt2, Scalar(186,88,255), 3, CV_AA);
*/
}
vector corners;//线的交点存储
for (unsigned int i = 0; ix1 && lines[i][0] < x2 && lines[i][1] > y1 && lines[i][1] < y2){
for (unsigned int j = i + 1; jx1 && lines[j][0] < x2 && lines[j][1] > y1 && lines[j][1] < y2){
cv::Point2f pt = getCrossPoint(lines[i], lines[j]);
if (pt.x >= 0 && pt.y >= 0)
{
//输出交点坐标
corners.push_back(pt);
std::cout << " pt.x: " << pt.x << " y: " << pt.y << std::endl;
}
}
}
}
}
//【4】依次在图中绘制出角点
for (unsigned int i = 0; i < corners.size(); i++)
{
circle(dstImage, corners[i], 3, CV_RGB(0, 255, 0), 2);
std::cout << " x: " << corners[i].x << " y: " << corners[i].y << std::endl;
}
//【5】显示原始图
imshow("【原始图】", srcImage);
//【6】边缘检测后的图
imshow("【边缘检测后的图】", midImage);
//【7】显示效果图
imshow("【效果图】", dstImage);
waitKey(0);
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(vo1)
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_FLAGS "-std=c++14 ${SSE_FLAGS} -msse4")
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
set(OpenCV_DIR /usr/lib/x86_64-linux-gnu/cmake/opencv4)
find_package(OpenCV REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
)
add_executable(hfchange hfchange1.cpp)
target_link_libraries(hfchange
${OpenCV_LIBS}
fmt)
参考
【OpenCV入门教程之十四】OpenCV霍夫变换:霍夫线变换,霍夫圆变换合辑_浅墨_毛星云的博客-CSDN博客_opencv 霍夫变换本篇文章中,我们一起探讨了OpenCV中霍夫变换相关的知识点,以及了解了OpenCV中实现霍夫线变换的HoughLines、HoughLinesP函数的使用方法,实现霍夫圆变换的HoughCircles函数的使用方法。此博文一共有四个配套的简短的示例程序,其详细注释过的代码都在文中贴出,且文章最后提供了综合示例程序的下载。先尝鲜一下其中一个示例程序的运行截图:https://blog.csdn.net/poem_qianmo/article/details/26977557