Colors 识别opencv中检测到的圆的颜色
您将如何使用opencv中的cvHoughcircles来识别每个检测到的圆的颜色? 我已经添加了代码,显示了我当前正在尝试的但是我得到了一个错误:IVIETest1.xe中的0x75 8D9617未处理的异常:微软C++异常:CV::内存位置0x00 19ED4< /P>异常Colors 识别opencv中检测到的圆的颜色,colors,opencv,geometry,Colors,Opencv,Geometry,您将如何使用opencv中的cvHoughcircles来识别每个检测到的圆的颜色? 我已经添加了代码,显示了我当前正在尝试的但是我得到了一个错误:IVIETest1.xe中的0x75 8D9617未处理的异常:微软C++异常:CV::内存位置0x00 19ED4< /P>异常 #include <stdio.h> #include <cv.h> #include <highgui.h> #include <math.h> int main(in
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
int main(int argc, char** argv)
{
//load image from directory
IplImage* img = cvLoadImage("C:\\Users\\Nathan\\Desktop\\SnookerPic.png");
IplImage* gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
CvMemStorage* storage = cvCreateMemStorage(0);
//covert to grayscale
cvCvtColor(img, gray, CV_BGR2GRAY);
// This is done so as to prevent a lot of false circles from being detected
cvSmooth(gray, gray, CV_GAUSSIAN, 7, 7);
IplImage* canny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage* rgbcanny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
cvCanny(gray, canny, 50, 100, 3);
//detect circles
CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, 35.0, 75, 60,0,0);
cvCvtColor(canny, rgbcanny, CV_GRAY2BGR);
//draw all detected circles
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]);
cvScalar c;
c = cvGet2D(img, center.x, center.y);//colour of circle
// 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);
}
//create window
cvNamedWindow("circles", 1);
cvNamedWindow("SnookerImage", 1);
//show image in window
cvShowImage("circles", rgbcanny);
cvShowImage("SnookerImage", img);
cvSaveImage("out.png", rgbcanny);
cvWaitKey(0);
return 0;
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
//从目录中加载图像
IplImage*img=cvLoadImage(“C:\\Users\\Nathan\\Desktop\\SnookerPic.png”);
IplImage*gray=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
CvMemStorage*storage=cvCreateMemStorage(0);
//隐到灰度
CVT颜色(img、灰色、CV_bgr2灰色);
//这样做是为了防止检测到大量假圆
cvSmooth(灰色、灰色、CV_高斯、7、7);
IplImage*canny=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage*rgbcanny=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
cvCanny(灰色,canny,50,100,3);
//检测圆
CvSeq*圆=cvHoughCircles(灰色,存储,CV_-HOUGH_梯度,1,35.0,75,60,0,0);
CVTColor(canny、rgbcanny、CV_GRAY2BGR);
//绘制所有检测到的圆
对于(int i=0;itotal;i++)
{
//将浮点数四舍五入为整数
float*p=(float*)cvGetSeqElem(圆,i);
cv::点中心(cvRound(p[0]),cvRound(p[1]);
int radius=cvRound(p[2]);
cvc;
c=cvGet2D(img,center.x,center.y);//圆的颜色
//画圆心
CV圆(img,中心,3,CV_RGB(0255,0),-1,8,0);
//画圆的轮廓
CV圆(img,中心,半径+1,CV_RGB(0,0255),2,8,0);
//显示坐标
printf(“x:%d y:%d r:%d\n”,中心x,中心y,半径);
}
//创建窗口
cvNamedWindow(“圆圈”,1);
cvNamedWindow(“斯诺克时代”,1);
//在窗口中显示图像
cvShowImage(“圆”,rgbcanny);
cvShowImage(“斯诺克时代”,img);
cvSaveImage(“out.png”,rgbcanny);
cvWaitKey(0);
返回0;
}
您试图获取颜色的当前圆的坐标似乎超出范围。在尝试获取颜色之前,您应该检查每个点是否在图像范围内(例如center.x>0&¢er.x