自编HoughLine函数

尝试着自己编写houghline代码,也参考了别人的代码,试着实现了一个,如下所示。
但存在一些问题,和opencv自带的效果并不一样,opencv自带(同样参数)的检测出来的直线更多,而我这个少了一些,虽然检测到的都是正确的。那为什么会漏检呢?暂时不知道原因。
#include<opencv2/opencv.hpp> #include<opencv2/imgproc/imgproc.hpp> using namespace cv; using namespace std; void HoughM(const Mat &img, vector<Vec2f>&Ve, int theta0,int MinNum); int main() { Mat srcImage = imread("data2.jpg"); if (!srcImage.data) { cout << "no srcImage" << endl; return 0; } imshow("orinageImage", srcImage); Mat midImage, dstImage; cvtColor(srcImage, midImage, CV_BGR2GRAY); GaussianBlur(midImage, midImage, Size(3, 3), 0, 0); Canny(midImage, dstImage, 100, 200,3); //cvtColor(midImage, dstImage, CV_GRAY2BGR); imshow("edgeDetect", dstImage); vector<Vec2f> lines; //HoughLines(dstImage, lines, 1, CV_PI / 360, 170, 0, 0); HoughM(dstImage, lines, 1, 170); float fRate = (float)(CV_PI / 180); vector<Vec2f>::const_iterator it = lines.begin(); cout << lines.size() << endl; //while (it != lines.end()) //{ // float rho = (*it)[0]; // float theta = (*it)[1]; // if (theta < CV_PI / 4. || theta>3. * CV_PI / 4) // { // Point pt1(rho / cos(theta), 0); // Point pt2((rho - dstImage.rows*sin(theta)) / cos(theta), dstImage.rows); // line(dstImage, pt1, pt2, Scalar(100,0,255), 2); // } // it++; // cout << "."; //} for (size_t i = 0; i < lines.size(); i++) { float rho = lines[i][0], theta = lines[i][1]; Point pt1, pt2; double a = cos(theta*fRate), b = sin(theta*fRate); 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)); //if (fabs(a)<0.001) //{ // pt1.x = pt2.x = cvRound(rho); // pt1.y = 0; // pt2.y = dstImage.rows; //} //else if (fabs(b)<0.001) //{ // pt1.y = pt2.y = cvRound(rho); // pt1.x = 0; // pt2.x = dstImage.cols; //} //else //{ // pt1.x = 0; // pt1.y = cvRound(rho / b); // pt2.x = cvRound(rho / a); // pt2.y = 0; //} line(dstImage, pt1, pt2, Scalar(255, 100, 5), 2, CV_AA); //line(dstImage,pt1,pt2,Scalar(55,100,195),1,LINE_AA); } imshow("HoughLine", dstImage); waitKey(0); return 0; } void HoughM(const Mat &img, vector<Vec2f>&Ve,int theta0,int MinNum) { int width = img.cols; int height = img.rows; int iRMax = (int)sqrt(width*width + height*height) + 1; int iThMax = 360;// theta0; int *pArray; pArray = new int[2*iRMax*iThMax]; memset(pArray, 0, sizeof(int)*2*iRMax*iThMax); float fRate = (float)(CV_PI / 180); for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) { if (img.at<unsigned char>(y,x) == 255) { for (int iTh = 0; iTh < iThMax; iTh += 1) { int iR = (int)(x*cos(iTh*fRate) + y*sin(iTh*fRate)); //if (iR < 0) // continue; iR = iR + iRMax; pArray[2*iRMax*iTh + iR]++; } } } for (int theta = 0; theta < 360; theta++) { for (int r = 0; r < iRMax*2; r++) { int thetaL = max(0, theta - 1); int thetaR = min(359, theta + 1); int rL = max(0, r - 1); int rR = min(iRMax*2 - 1, r + 1); int tmp = pArray[theta*iRMax*2 + r]; //if ((tmp>MinNum) // && tmp>pArray[thetaL*iRMax * 2 + rL] && tmp>pArray[thetaL*iRMax * 2 + r] // && (tmp>pArray[thetaL*iRMax * 2 + rR])&& (tmp > pArray[theta*iRMax * 2 + rL]) // && (tmp > pArray[theta*iRMax * 2 + rR])&& (tmp > pArray[thetaR*iRMax * 2 + rL]) // && (tmp > pArray[thetaR*iRMax * 2 + r]) && (tmp > pArray[thetaR*iRMax * 2 + rR]) if ((tmp>MinNum) &&tmp>pArray[thetaL*iRMax * 2 + r] && (tmp >= pArray[theta*iRMax * 2 + rR]) && (tmp > pArray[theta*iRMax * 2 + rL]) && (tmp >= pArray[thetaR*iRMax * 2 + r])) { Vec2f t = Vec2f(float(r-iRMax), float(theta)); Ve.push_back(t); } } } }

 

转载于:https://www.cnblogs.com/NEVER-OR-EVER/p/6083725.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Matlab的hough函数是一个用于霍夫变换的函数。霍夫变换是一种图像处理技术,用于检测图像中的直线和曲线。通过hough函数,可以将图像转换为霍夫空间,并在霍夫空间中找到直线或曲线的参数。具体而言,在Matlab中,可以使用hough函数生成霍夫变换矩阵,该矩阵表示了在图像中出现的直线或曲线的可能性。通过调用houghpeaks函数,可以找到霍夫变换矩阵中的峰值点,这些峰值点对应着图像中的直线或曲线。最后,使用houghlines函数可以提取出基于霍夫变换的直线或曲线的具体信息,例如其在图像中的位置和方向等。总的来说,通过Matlab的hough函数及其相关函数,可以实现对图像中直线或曲线的检测和提取。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [matlab编写hough函数](https://download.csdn.net/download/yilvpiaoxiang/3241254)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [小白学习图像处理8——使用matlab的hough变换函数](https://blog.csdn.net/qq_41140138/article/details/105886655)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值