c++ - 霍夫圆形变换
问题描述
我试图使用梯度方向实现霍夫变换。我知道 OpenCv 中有一个实现,但我想自己做。
我正在使用 Sobel 来获取 X 和 Y 渐变。那么对于每个像素
- 大--->
sqrt(sobelX^2 + sobelY^2)
- 方向 -->
atan2(sobelY,sobelX) * 180/PI
如果幅度高于 220(几乎是黑色),这就是边缘。然后在圆方程上使用方向。
但结果是不能接受的。有什么帮助吗?
我知道有cv::polar
and cv::cartToPolar
,但我想优化代码,以便所有方程都可以即时计算,没有空循环。
cv::Mat sobelX,sobelY;
Sobel(mat, sobelX, CV_32F, 1, 0, kernelSize, 1, 0, cv::BORDER_REPLICATE);
Sobel(mat, sobelY, CV_32F, 0, 1, kernelSize, 1, 0, cv::BORDER_REPLICATE);
//cv::Canny(mat,mat,100,200,kernelSize,false);
debug::showImage("sobelX",sobelX);
debug::showImage("SobelY",sobelY);
debug::showImage("MAT",mat);
cv::Mat magnitudeMap,angleMap;
magnitudeMap = cv::Mat::zeros(mat.rows,mat.cols,mat.type());
angleMap = cv::Mat::zeros(mat.rows,mat.cols,mat.type());
std::vector<cv::Mat> hough_spaces(max);
for(int i=0; i<max; ++i)
{
hough_spaces[i] = cv::Mat::zeros(mat.rows,mat.cols,mat.type());
}
for(int x=0; x<mat.rows; ++x)
{
for(int y=0; y<mat.cols; ++y)
{
const float magnitude = sqrt(sobelX.at<uchar>(x,y)*sobelX.at<uchar>(x,y)+sobelY.at<uchar>(x,y)*sobelY.at<uchar>(x,y));
const float theta= atan2(sobelY.at<uchar>(x,y),sobelX.at<uchar>(x,y)) * 180/CV_PI;
magnitudeMap.at<uchar>(x,y) = magnitude;
if(magnitude > 225)//mat.at<const uchar>(x,y) == 255)
{
for(int radius=min; radius<max; ++radius)
{
const int a = x - radius * cos(theta);//lookup::cosArray[static_cast<int>(theta)];//+ 0.5f;
const int b = y - radius * sin(theta);//lookup::sinArray[static_cast<int>(theta)]; //+ 0.5f;
if(a >= 0 && a <hough_spaces[radius].rows && b >= 0 && b<hough_spaces[radius].cols) {
hough_spaces[radius].at<uchar>(a,b)+=10;
}
}
}
}
}
debug::showImage("magnitude",magnitudeMap);
for(int radius=min; radius<max; ++radius)
{
double min_f,max_f;
cv::Point min_loc,max_loc;
cv::minMaxLoc(hough_spaces[radius],&min_f,&max_f,&min_loc,&max_loc);
if(max_f>=treshold)
{
circles.emplace_back(cv::Point3f(max_loc.x,max_loc.y,radius));
// debug::showImage(std::to_string(radius).c_str(),hough_spaces[radius]);
}
}
circles.shrink_to_fit();
解决方案
推荐阅读
- react-native - 在 ubuntu 20.04 中运行 npm install -g create-react-app 时出错
- node.js - 对于模型“Project”的路径“_id”处的值“req.params.id”,转换为 ObjectId 失败
- c# - 使用 Webclient 一次下载多个文件
- bash - 使用“set -o errtrace”时,单语句子shell 似乎没有继承 ERR 陷阱
- node.js - 当我告诉节点运行一个文件时,它运行两个文件
- javascript - 如何使用javascript将参数传递给目标url
- reactjs - 如何在 Ag-grid 中按 ObjectID (_id) 过滤记录
- arrays - 如何从具有多个维度的numpy数组中删除重复项
- node.js - 如何在 docker CMD 命令中运行多个 npm 脚本
- c# - 模型属性更改时的 WPF 调用方法