gpt4 book ai didi

image - 估计图像线梯度(不是像素梯度)

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:01 25 4
gpt4 key购买 nike

我有一个问题,我想估计轮廓上线的梯度。请注意,我不需要像素梯度,而是线的变化率。

如果您看到附加图像,您将看到带有绿色轮廓的二值图像。我想根据轮廓上像素的梯度来标记每个像素。

为什么我需要渐变是因为我想计算渐变方向从 + 变为 - 或从 - 变为 + 的点。

我想不出一个好的方法来估计图像上的这个点。有人可以帮我建议我如何估计这一点。

enter image description here

最佳答案

这是一个小程序,它以非常简单的方式计算每个轮廓像素位置的切线(还有其他可能更好的方法!简单的方法是:http://en.wikipedia.org/wiki/Finite_difference#Forward.2C_backward.2C_and_central_differences):

  1. 对于轮廓像素 c_{i} 获取邻居 c_{i-1} 和 c_{i+1}
  2. c_i 处的切线方向为 (c_{i-1} - c_{i+1}

所以这一切都在 CONTOUR PIXELS 上,但如果你计算与完整图像像素梯度的正交,也许你可以做类似的事情......对此不确定;)

代码如下:

int main()
{
cv::Mat input = cv::imread("../inputData/ContourTangentBin.png");

cv::Mat gray;
cv::cvtColor(input,gray,CV_BGR2GRAY);

// binarize
cv::Mat binary = gray > 100;

// find contours
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
findContours( binary.clone(), contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE ); // CV_CHAIN_APPROX_NONE to get each single pixel of the contour!!


for( int i = 0; i< contours.size(); i++ )
{
std::vector<cv::Point> & cCont = contours[i];
std::vector<cv::Point2f> tangents;

if(cCont.size() < 3) continue;

// 1. compute tangent for first point
cv::Point2f cPoint = cCont.front();
cv::Point2f tangent = cCont.back() - cCont.at(1); // central tangent => you could use another method if you like to
tangents.push_back(tangent);

// display first tangent
cv::Mat tmpOut = input.clone();
cv::line(tmpOut, cPoint + 10*tangent, cPoint-10*tangent, cv::Scalar(0,0,255),1);
cv::imshow("tangent",tmpOut);
cv::waitKey(0);

for(unsigned int j=1; j<cCont.size(); ++j)
{
cPoint = cCont[j];
tangent = cCont[j-1] - cCont[(j+1)%cCont.size()]; // central tangent => you could use another method if you like to
tangents.push_back(tangent);

//display current tangent:
tmpOut = input.clone();
cv::line(tmpOut, cPoint + 10*tangent, cPoint-10*tangent, cv::Scalar(0,0,255),1);
cv::imshow("tangent",tmpOut);
cv::waitKey(0);
//if(cv::waitKey(0) == 's') cv::imwrite("../outputData/ContourTangentTangent.png", tmpOut);
}

// now there are all the tangent directions in "tangents", do whatever you like with them
}

for( int i = 0; i< contours.size(); i++ )
{
drawContours( input, contours, i, cv::Scalar(0,255,0), 1, 8, hierarchy, 0 );
}

cv::imshow("input", input);
cv::imshow("binary", binary);
cv::waitKey(0);
return 0;
}

我使用了这张图片:

enter image description here

得到如下输出:

enter image description here

enter image description here

enter image description here

在结果中,您会得到一个向量,其中包含该轮廓的每个像素的 2D 切线信息(线方向)。

关于image - 估计图像线梯度(不是像素梯度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28104828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com