Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw circles with random colors in OpenCV

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!

like image 626
Nick Timebreak Avatar asked Oct 16 '13 09:10

Nick Timebreak


People also ask

How do you code random colors in python?

Using random() function to generate random colors To begin, import the random function in Python to obtain a random color. The variable r stands for red, g stands for green, and b stands for blue. We already know that the RGB format contains an integer value ranging from 0 to 255.

How do I generate a random color in Matplotlib?

random() to generate a random color for a Matplotlib plot. Create three variables r , g , and b , each assigned a random float value in the range [0, 1] by calling random. random() . Use the syntax (r, g, b) to generate an RGB tuple color .


1 Answers

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);
}
like image 167
WangYudong Avatar answered Oct 05 '22 03:10

WangYudong