How to draw circles with random colors in OpenCV

我与影子孤独终老i 提交于 2020-08-22 05:26:20

问题


I am using SURF algorithm to match two images with OpenCV. And I have got the keypioints.

Now I want to draw these keypoints with random colors circles.

I know how to draw a circle in OpenCV with the function cvCircle, but the color is fixed with cvScalar(r,g,b).

I want the the color of the circle of a keypoint in a image is different to the circles near it.

The library function cv::drawMatches() in OpenCV have the effect I want. But I don't know how to realize it.

Does anyone who can tell me how to draw the circles.

Thanks!


回答1:


Suppose you want to draw circles in different colors on Mat image. Here is a way to generate random colors:

RNG rng(12345);
Mat image = Mat::zeros(500, 500, CV_8UC3);     // change to the size of your image
for (int i = 0; i < circleNum; i++) {
    Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0, 255), rng.uniform(0, 255));
    circle(image, center[i], radius[i], color, -1, 8, 0);
}



回答2:


cv::drawKeypoints(matOriginal, keyPoints, matOriginal);

with this method it should be posible to draw your matches.




回答3:


Just generate a list of random numbers and then convert them to int otherwise it will give an error: TypeError: Scalar value for argument 'color' is not numeric. so convert it to int and then function for circle.

color1 = (list(np.random.choice(range(256), size=3)))  
color =[int(color1[0]), int(color1[1]), int(color1[2])]  
cv.circle(img3, (x1,y1), 2,color,2)  

put your image where you want to draw circles.



来源:https://stackoverflow.com/questions/19400376/how-to-draw-circles-with-random-colors-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!