I would like to recognize blue balls with Kinect using opencv.

Asked 2 years ago, Updated 2 years ago, 121 views

Error Message

Use int for the main return type. You cannot use void.
The int main(void) overload function differs only from the int main(void) return type.

These two won't disappear.
Thank you for your cooperation

From: http://kivantium.hateblo.jp/entry/20120822/p1
    Kinect Practical Program Sugiuraji

#include "stdafx.h"
# include <time.h>
# include <stdlib.h>
# include <iostream>
# include <sstream>
# include "opencv/Labeling.h"
# include <cstdio>

void main()
{
    try{
        KinectSample kinect;
        kinect.initialize();
        kinect.run();
        using namespace cv;
        using namespace std;
        int num;
        int main(); {
            try{
                VideoCapture capture(0);
                Mat RGBMap, Cloud, valid;
                IplImage ipl_RGB;
                IplImage*imgR,*imgG=0,*imgB;
                IplImage*imgThreshold_R, *imgThreshold_G, *imgThreshold_B, *imgResult, *imgTmp, *RGB_image;
                int x, y;
                short*dst;
                LabelingBS labeling;
                RegionInfoBS*ri;
                dst = new short [640*480];

                imgThreshold_R=cvCreateImage(cvSize(640,480), IPL_DEPTH_8U,1);
                imgThreshold_G = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U,1);
                imgThreshold_B = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U,1);
                imgResult=cvCreateImage(cvSize(640,480), IPL_DEPTH_8U,1);
                imgTmp = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U,1);
                imgR=cvCreateImage(cvSize(640,480), IPL_DEPTH_8U,1); // Red
                imgG = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U,1); // Green
                imgB = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U,1); // Blue
                while(1){
                    // wait for data to be updated
                    capture.grab();
                    // Obtain and view RGB
                    capture.retrieve(RGBMap, CV_CAP_OPENNI_BGR_IMAGE);
                    ipl_RGB = RGBMap;
                    RGB_image=&ipl_RGB;
                    cvSplit(RGB_image, imgB, imgG, imgR, NULL);
                    // Extract pixels with more than 100 red elements and 1.5 times more than green and blue
                    cvThreshold(imgB, imgThreshold_B, 100, 255, CV_THRESH_BINARY);
                    cvDiv(imgB, imgG, imgTmp, 10); // 10x
                    cvThreshold(imgTmp, imgThreshold_G, 18,255, CV_THRESH_BINARY);
                    cvDiv(imgB, imgR, imgTmp, 10);
                    cvThreshold(imgTmp, imgThreshold_R, 18,255, CV_THRESH_BINARY);
                    cvAnd(imgThreshold_G, imgThreshold_R, imgTmp, NULL);
                    cvAnd(imgTmp, imgThreshold_B, imgResult, NULL);
                    cvAnd(imgThreshold_G, imgThreshold_R, imgTmp, NULL);
                    cvAnd(imgTmp, imgThreshold_B, imgResult, NULL);

                    // Labeling
                    labeling.Exec(uchar*)imgResult->imageData,dst,imgResult->width,imgResult->height,true,30);
                    if(labeling.GetNumOfResultRegions()!=0){
                        r=labeling.GetResultRegionInfo(0);
                        // Draw a square
                        int ltop_x, ltop_y, rbottom_x, rbottom_y;
                        ri->GetMin(ltop_x,ltop_y);
                        r->GetMax(rbottom_x,rbottom_y);
                        cvLine(RGB_image, cvPoint(ltop_x, ltop_y), cvPoint(rbottom_x, ltop_y), CV_RGB(255,255,255));
                        cvLine(RGB_image, cvPoint(rbottom_x, ltop_y), cvPoint(rbottom_x, rbottom_y), CV_RGB(255,255,255));
                        cvLine(RGB_image, cvPoint(rbottom_x,rbottom_y), cvPoint(ltop_x,rbottom_y), CV_RGB(255,255,255));
                        cvLine(RGB_image, cvPoint(ltop_x,rbottom_y), cvPoint(ltop_x,ltop_y), CV_RGB(255,255,255));
                        float f_x, f_y;
                        ri->GetCenter(f_x,f_y);
                        x = (int)f_x;
                        y=(int)f_y;
                        capture.retrieve(Cloud, CV_CAP_OPENNI_POINT_CLOUD_MAP);
                        capture.retrieve(valid, CV_CAP_OPENNI_VALID_DEPTH_MASK);
                        if(valid.at<unsigned char>(y,x)==0xff){
                            Vec3fs=Cloud.at<Vec3f>(y, x);
                            printf("%f%f%f\n", s[0], s[1], s[2]); // Display coordinates
                        }
                        else{
                            printf("invalid!\n";
                        }
                    }

                    cvShowImage("Window", RGB_image);
                    if(waitKey(10)>=0){
                        break;
                    }
                }
                destroyAllWindows();
            }
            catch(...){
                cout<<"exception!!!"<<endl;
            }
        }
    }

    catch(std::exception&ex){
        std::cout<<ex.what()<<std::endl;
    }
}

opencv kinect

2022-09-30 17:18

1 Answers

The main function is written incorrectly.

First, use the main function

void main()
{
  try{
    KinectSample kinect;
    kinect.initialize();
    kinect.run();
  }
  catch(std::exception&ex){
    std::cout<<ex.what()<<std::endl;
  }
}

Rewrite as shown in .

Write the code that recognizes the blue ball in the run() function.Processing flow is

  • Convert input images to HSV format
  • Extract the blue part of the image
  • Take out the connecting components of the extracted image

That's how it feels.We recommend that you use OpenCV's connectedComponentsWithStats() function (document) to extract the concatenated components.This function can only be used with OpenCV 3.0 or later, so install a newer version of OpenCV if necessary.

Here is an example of a run() function that recognizes blue balls:

void run()
{
  cv::Match;

  // Main loop
  while(1){

    //Waiting for data
    DWORDret=::WaitForSingleObject (streamEvent, INFINITE);
    :: ResetEvent (streamEvent);

    drawRgbImage(image);

    cv::Math_image;
    cv::flip(image,h_image,1);

    // Convert RGB image to HSV image
    cv::Mathsv, extracted;
    cv::cvtColor(h_image, hsv, CV_RGB2HSV); // CV_BGR2HSV if camera image is BGR
    // Extract the area where the color ranges from 100<=H<=120,80<=S<=255,80<=V<=255
    // Adjust the color range to match the color of the object you want to extract.
    cv::Scalar hsv_min = cv::Scalar(100,80,80);
    cv::Scalar hsv_max = cv::Scalar(120,255,255);
    cv::inRange(hsv,hsv_min,hsv_max,extracted);

    // labeling execution
    cv::MatlabelImg,stats,centroids;
    intnLabels=cv::connectedComponentsWithStats(extracted, labelImg, stats, centroids);

    // rectangular drawing
    for(inti=1;i<nLabels;++i){
      int*param=stats.ptr<int>(i);

      int x = param [0];
      inty=param[1];
      int width=param[2];
      intheight=param[3];

      /* C++11 is easier to understand than this.
         intx = param [cv:: ConnectedComponentsTypes::CC_STAT_LEFT];
         inty=param [cv::ConnectedComponentsTypes::CC_STAT_TOP];
         int width = param [cv:: ConnectedComponentsTypes::CC_STAT_WIDTH];
         intheight=param [cv::ConnectedComponentsTypes::CC_STAT_HEIGHT];
       */

      cv::rectangle(h_image, cv::Rect(x,y,width,height), cv::Scalar(255,255,255),2);
    }

    // Display image
    cv::imshow("kinect Sample", h_image);

    // Key input check for termination and weight for display
    int key = cv::waitKey(10);
    if(key=='q'){
      break;
    }
  }
}

Try adjusting the blue range yourself.

See also


2022-09-30 17:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.