您的位置首页快问快答

opencv代码求助(探测摄像头中红色圆形 并标注)

opencv代码求助(探测摄像头中红色圆形 并标注)

的有关信息介绍如下:

opencv代码求助(探测摄像头中红色圆形 并标注)

static const int R=2;

static const int G=1;

static const int B=0;

IplImage *src = cvLoadImage("E:/test.png");

cvShowImage("s",src);

IplImage *dst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);

cvSet(dst,cvScalar(0,0,0));

for (int h=0;hheight;h++) {

unsigned char* prgb=(unsigned char*)src->imageData+h*src->widthStep;

unsigned char* pdst=(unsigned char*)dst->imageData+h*dst->widthStep;

for (int w=0;wwidth;w++) {

if (prgb[R]>=200&&prgb[G]>0&&prgb[G]<50&&prgb[B]>0&&prgb[B]<50)

{

memcpy(pdst,prgb,3);

}

prgb+=3;

pdst+=3;

}

}

IplImage *gray = cvCreateImage(cvGetSize(src),src->depth,1);

cvCvtColor(dst,gray,CV_BGR2GRAY);

cvThreshold(gray,gray,50,255,CV_THRESH_BINARY);

cvSmooth(gray,gray,CV_GAUSSIAN,5,5);

cvShowImage("red",gray);

CvMemStorage *pcvMStorage = cvCreateMemStorage();

double fMinCircleGap = gray->height / 10;

CvSeq *pcvSeqCircles = cvHoughCircles(gray, pcvMStorage, CV_HOUGH_GRADIENT, 1, fMinCircleGap);

int i;

for (i = 0; i < pcvSeqCircles->total; i++)

{

float* p = (float*)cvGetSeqElem(pcvSeqCircles, i);

cvCircle(src, cvPoint(cvRound(p), cvRound(p)), cvRound(p), CV_RGB(0, 255, 0), 3);

}

cvShowImage("result",src);