我需要根据图像中的函数绘制一条曲线。 x ^ 2 + y ^ 2 = 1之类的东西,无需将其转换为显式形式,因为并非我使用的所有函数都可以通过这种方式轻松转换。对于常规的隐式函数,我将cv2.polylines与x和y点一起使用以在图像中绘制曲线,并且我想向大家了解对于显式函数是否有类似的解决方案。到目前为止,我发现的所有内容都使用matplotlib通过plt.contour绘制图形。但这对我似乎并没有真正的用处。请帮忙
提前致谢
最佳答案
您可以像这样渲染它,对不起,我是C++爱好者)希望这个想法对python程序员是清楚的。
// Your function
float func(float x, float y)
{
// x * x * sin(x) + y * y = 1
return (x * x * sin(x) + y * y - 1);
}
// render it
void plotXY()
{
// canvas size
int height = 800;
int width = 800;
// plot scale
float scale = 0.1;
// cavas
cv::Mat result=cv::Mat::zeros(height, width, CV_8UC3);
// move origin to canvas center
float cx = (float)width / 2.0f;
float cy = (float)height / 2.0f;
// precompute scale
float sx = (scale * (float)width);
float sy = (scale * (float)height);
// scan for zero crossings
for (uint32_t i = 1; i < height-1; ++i)
{
for (uint32_t j = 1; j < width-1; ++j)
{
// transform screen space to real space
float x = ((float)j - cx) / sx;
float x1 = ((float)(j-1) - cx) / sx;
float x2 = ((float)(j+1) - cx) / sx;
float y = ((float)i - cy) / sy;
float y1 = ((float)(i-1) - cy) / sy;
float y2 = ((float)(i+1) - cy) / sy;
// if zero crossimg along x put pixel
if ( func(x1,y)* func(x2, y) < 0)
{
result.at<cv::Vec3b>(i, j) = cv::Vec3b(255, 255, 255);
}
// if zero crossimg along y put pixel
if (func(x, y1) * func(x, y2) < 0)
{
result.at<cv::Vec3b>(i, j) = cv::Vec3b(255, 255, 255);
}
}
}
// show and save result
cv::imshow("plot", result);
cv::imwrite("plot.jpg", result);
cv::waitKey();
}
函数x * x * sin(x)+ y * y = 1的结果为:关于python - 在图像中绘制隐式函数(Python,[OpenCV]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64359929/