是否有解决方案可以访问曲线/路径上的像素?我们可以使用LineIterator来实现吗
答案 0 :(得分:6)
是的,您可以使用CvLineIterator
方法访问像素。
请参阅以下链接,
http://opencv.jp/opencv-2.2_org/c/core_drawing_functions.html
答案 1 :(得分:4)
我认为没有任何内置功能。您需要先在cv::Mat
结构中定义直线/曲线,然后从那里继续。让我用一个例子来解释。
cv::Mat input_image
,并使用cv::HoughLinesDetector
检测图片中存储在cv::Mat hough_lines
中的行。 hough_lines
并填充cv::Mat hough_Mat(cv::Size(input_image.size()))
(如果您希望根据原始数据明亮地显示您的行,则应将其转换为BGR图像。hough_Mat
,其中像素大于零,然后只访问input_image
中的相同位置。 虽然这个例子是一个使用霍夫变换的简单例子,但你可以将它与任何其他曲线一起使用,只要你有原始图像的曲线数据。
HTH
答案 2 :(得分:4)
好的,这是一种沿着可以参数化的连接曲线访问像素的方法。可能有更有效的方法,但这个方法非常简单:只需在参数步骤中对曲线进行采样,这样就不会访问像素两次而不会跳过像素:
我从维基百科中采用了参数函数作为样本: http://en.wikipedia.org/wiki/Parametric_equation#Some_sophisticated_functions
int main()
{
cv::Mat blank = cv::Mat::zeros(512,512,CV_8U);
// parametric function:
// http://en.wikipedia.org/wiki/Parametric_equation#Some_sophisticated_functions
// k = a/b
// x = (a-b)*cos(t) + b*cos(t((a/b)-1))
// y = (a-b)*sin(t) - b*sin(t((a/b)-1))
float k = 0.5f;
float a = 70.0f;
float b = a/k;
// translate the curve somewhere
float centerX = 256;
float centerY = 256;
// you will check whether the pixel position has moved since the last active pixel, so you have to remember the last one:
int oldpX,oldpY;
// compute the parametric function's value for param t = 0
oldpX = (a-b)*cos(0) + b*cos(0*((a/b)-1.0f)) + centerX -1;
oldpY = (a-b)*sin(0) - b*sin(0*((a/b)-1.0f)) + centerY -1;
// initial stepsize to parametrize the curve
float stepsize = 0.01f;
//counting variables for analyzation
unsigned int nIterations = 0;
unsigned int activePixel = 0;
// iterate over whole parameter region
for(float t = 0; t<4*3.14159265359f; t+= stepsize)
{
nIterations++;
// compute the pixel position for that parameter
int pX = (a-b)*cos(t) + b*cos(t*((a/b)-1.0f)) + centerX;
int pY = (a-b)*sin(t) - b*sin(t*((a/b)-1.0f)) + centerY;
// only access pixel if we moved to a new pixel:
if((pX != oldpX)||(pY != oldpY))
{
// if distance to old pixel is too big: stepsize was too big
if((abs(oldpX-pX)<=1) && (abs(oldpY-pY)<=1))
{
//---------------------------------------------------------------
// here you can access the pixel, it will be accessed only once for that curve position!
blank.at<unsigned char>((pY),(pX)) = blank.at<unsigned char>((pY),(pX))+1;
//---------------------------------------------------------------
// update last position
oldpX = pX;
oldpY = pY;
activePixel++; // count number of pixel on the contour
}
else
{
// adjust/decrease stepsize here
t -= stepsize;
stepsize /= 2.0f;
//TODO: choose smarter stepsize updates
}
}
else
{
// you could adjust/increase the stepsize here
stepsize += stepsize/2.0f;
//TODO: prevent stepsize from becoming 0.0f !!
//TODO: choose smarter stepsize updates
}
}
std::cout << "nIterations: " << nIterations << " for activePixel: " << activePixel << std::endl;
cv::imwrite("accessedOnce.png", blank>0);
cv::imwrite("accessedMulti.png", blank>1);
cv::waitKey(-1);
return 0;
}
给出这些结果:
像素访问一次:
多次访问像素:
终端输出:
nIterations: 1240 for activePixel: 1065