gpt4 book ai didi

colors - 识别opencv中检测到的圆圈的颜色

转载 作者:行者123 更新时间:2023-12-02 17:15:49 26 4
gpt4 key购买 nike

您将如何使用opencv中的cvHoughcircles识别每个检测到的圆圈的颜色?
我添加了显示我当前正在尝试的代码,但出现错误:imageTest1.exe中0x758d9617的未处理异常:Microsoft C++异常:内存位置0x0019eed4的cv::Exception

#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;
}

最佳答案

您试图获取颜色的当前圆的坐标看起来超出范围。在获取之前,应检查每个点是否在图像范围内(类似于center.x> 0 && center.x 0 && center.y

关于colors - 识别opencv中检测到的圆圈的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7801203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com