已定义值的未定义值错误

发布于 2024-12-13 13:55:22 字数 1371 浏览 1 评论 0原文

我有一个像这样的 for 循环,

 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]);
         int num_red = 0;
         //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]);

        if (s.val[2]<=255 && s.val[2]>=230 && s.val[1]<=40 && s.val[1]>=0 && s.val[0] <=40 && s.val[0]>=0)
        {
            printf("Red Ball\n");
            num_red++;
        }

它正在工作。但后来在我的代码中,我尝试使用 s.val[]num_red 就像这样,

int count_red = 0;
int red_pot = 0;
if(s.val[2]<=255 && s.val[2]>=230 && s.val[1]<=40 && s.val[1]>=0 && s.val[0] <=40 && s.val[0]>=0)
    count_red ++;//count the reds detected
num_red - count_red = red_pot;//originally detected - whats left = whats potted

我收到了 's' 的未声明标识符错误。 .val 的左侧必须具有类/结构和 'num_red' :未声明的标识符。我不明白为什么程序不能从上面向下读取这些值。有人能帮忙吗?

I have a for loop like so

 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]);
         int num_red = 0;
         //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]);

        if (s.val[2]<=255 && s.val[2]>=230 && s.val[1]<=40 && s.val[1]>=0 && s.val[0] <=40 && s.val[0]>=0)
        {
            printf("Red Ball\n");
            num_red++;
        }

which is working. but later on in my code i tried to use the s.val[] and num_red like this

int count_red = 0;
int red_pot = 0;
if(s.val[2]<=255 && s.val[2]>=230 && s.val[1]<=40 && s.val[1]>=0 && s.val[0] <=40 && s.val[0]>=0)
    count_red ++;//count the reds detected
num_red - count_red = red_pot;//originally detected - whats left = whats potted

im getting undeclared identifier error for 's'. Left of .val must have class/struct and 'num_red' : undeclared identifier. I dont understand why the program doesnt can't read these values from above further down. anyone able to help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧故 2024-12-20 13:55:22

您可以在 for 循环内创建 s。因此,一旦 for 循环终止,s 就会超出范围。您需要在一个范围内创建一个变量,该范围包括您打算访问它的每个范围。

You create s inside the for loop. So as soon as the for loop terminates, s goes out of scope. You need to create a variable at a scope that includes every scope in which you intend to access it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文