这是我对您的问题的解决方案:在此示例中,我从来自网络摄像头的 rgb 图像开始,然后将其转换为灰度,然后在应用阈值后转换为二进制。
下一步,当您有一个二进制图像时,将其再次转换为 RGB(或 BGR 作为 OpenCV 约定)并在其上绘制您想要的任何内容。箭头的代码是您所链接内容的复制。
希望对你有帮助
cv::VideoCapture cam(n_source);
cam >> frame;
cv::Mat grey_image;
cv::Mat binary_image; // Your binary image
cv::cvtColor(frame, grey_image, CV_RGB2GRAY);
cv::threshold(grey_image, binary_image, 100, 255, 0);
// Convert the binary to RGB
cv::Mat dst_rgb;
cv::cvtColor(binary_image, dst_rgb, CV_GRAY2BGR);
// Draw the arrow on the RGB image
int x = 200;
int y = 200;
int u = 100;
int v = 100;
cv::Point pt1,pt2;
double Theta;
double PI = 3.1416;
cv::Scalar Color(255,0,0);
int size = 5;
int Thickness = 5;
if(u==0)
Theta=PI/2;
else
Theta=atan2(double(v),(double)(u));
pt1.x=x;
pt1.y=y;
pt2.x=x+u;
pt2.y=y+v;
cv::line(dst_rgb,pt1,pt2,Color,Thickness,8); //Draw Line
size=(int)(size*0.707);
if(Theta==PI/2 && pt1.y > pt2.y)
{
pt1.x=(int)(size*cos(Theta)-size*sin(Theta)+pt2.x);
pt1.y=(int)(size*sin(Theta)+size*cos(Theta)+pt2.y);
cv::line(dst_rgb,pt1,pt2,Color,Thickness,8); //Draw Line
pt1.x=(int)(size*cos(Theta)+size*sin(Theta)+pt2.x);
pt1.y=(int)(size*sin(Theta)-size*cos(Theta)+pt2.y);
cv::line(dst_rgb,pt1,pt2,Color,Thickness,8); //Draw Line
}
else{
pt1.x=(int)(-size*cos(Theta)-size*sin(Theta)+pt2.x);
pt1.y=(int)(-size*sin(Theta)+size*cos(Theta)+pt2.y);
cv::line(dst_rgb,pt1,pt2,Color,Thickness,8); //Draw Line
pt1.x=(int)(-size*cos(Theta)+size*sin(Theta)+pt2.x);
pt1.y=(int)(-size*sin(Theta)-size*cos(Theta)+pt2.y);
cv::line(dst_rgb,pt1,pt2,Color,Thickness,8); //Draw Line
}
// Plot
cv::namedWindow("test rgb");
cv::imshow("test rgb", dst_rgb);
cv::waitKey(0);
一个例子: