问题描述
是否有解决方案来访问沿曲线/路径的像素?我们可以使用LineIterator做它
Is there a solution to access to pixels along the curve /path ? can we use LineIterator to do it
推荐答案
好的,这里是一种访问像素的连接曲线可以参数化的方法。可能有更有效的方法,但这一个很简单:只是在参数步骤中的曲线采样,以便您不访问像素两次,不跳过一个像素:
Ok, here is a way to access pixel along a connected curve that can be parametrized. There might be more efficient ways, but this one is quite simple: just sample the curve in parametersteps so that you don't access a pixel twice and don't skip a pixel:
我使用wikipedia的参数函数作为示例:
I've taken a parametric function from wikipedia as a sample: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;
}
提供以下结果:
像素已访问一次:
像素被多次访问:
终端输出:
nIterters:1240 for activePixel:1065
这篇关于使用opencv访问沿曲线/路径的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!