彩球碰撞
for (int i = 0; i < circles->total; i++)
{
// round the floats to an int
float* p = (float*)cvGetSeqElem(circles, i);
cv::Point center(cvRound(p[0]), cvRound(p[1]));
int radius = cvRound(p[2]);
//uchar* ptr;
//ptr = cvPtr2D(img, center.y, center.x, NULL);
//printf("B: %d G: %d R: %d\n", ptr[0],ptr[1],ptr[2]);
CvScalar s;
s = cvGet2D(img,center.y, center.x);//colour of circle
printf("B: %f G: %f R: %f\n",s.val[0],s.val[1],s.val[2]);
// draw the circle center
cvCircle(img, center, 3, CV_RGB(0,255,0), -1, 8, 0 );
// draw the circle outline
cvCircle(img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );
//display coordinates
printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
上面的代码检测 22 个彩色球并提取每个球的 RGB 值。我可以使用此 RGB 值来确定每个球的颜色。我正在尝试实现对白球首先击中哪个颜色球的检测。我的想法是等待白球中心改变(即移动),下一个中心改变的彩球就是它击中的球。但我在编码时遇到问题?
for (int i = 0; i < circles->total; i++)
{
// round the floats to an int
float* p = (float*)cvGetSeqElem(circles, i);
cv::Point center(cvRound(p[0]), cvRound(p[1]));
int radius = cvRound(p[2]);
//uchar* ptr;
//ptr = cvPtr2D(img, center.y, center.x, NULL);
//printf("B: %d G: %d R: %d\n", ptr[0],ptr[1],ptr[2]);
CvScalar s;
s = cvGet2D(img,center.y, center.x);//colour of circle
printf("B: %f G: %f R: %f\n",s.val[0],s.val[1],s.val[2]);
// draw the circle center
cvCircle(img, center, 3, CV_RGB(0,255,0), -1, 8, 0 );
// draw the circle outline
cvCircle(img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );
//display coordinates
printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
the code above detects 22 colored balls and extracts rgb value of each ball. ican use this rgb value to determine color of each ball. i am trying to implement detection of which color ball the white ball hits first. my idea was to wait for white balls centre to change(i.e. move) and the next color ball whose center changes is the ball it hits. but i am having trouble coding this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将先前的 x,y 保存在变量中,并在每次迭代时检查它们,以确定它们是否已更改。
我建议您检查白球中心与其他球中心之间的距离,而不是这样 - 当距离是半径之和时,就会发生碰撞。
顺便说一句,这不是 C。
You can save the previous x,y in variables, and check them every iteration, to determinate if they had changed.
I recommend you, instead of this, to check the distance between the center of the white ball to the center of the others - a collision is when the distance is the sum of the radiuses.
BTW, It's not C.