Arduino + opencv project tracker ball

asked 2018-06-06 04:16:44 -0600

biza gravatar image

updated 2018-06-09 10:47:03 -0600

hello friends, I am carrying out a project that is based on tracking a ball inside an acrylic tube. At the bottom of the tube is placed a motor that moves a ping pong ball, the goal is after the user sets the setpoint through the camera control the displacement of the ball in the tube until the set point. The code that I will share with you is fully functional, however I face some problems the roi is drawn based on the max and min, and the color of the ball based on the HoughCircles, but sometimes the inner and outer contour of the ball disappears, which it causes me enough problems. If I can solve these problems I can calculate the distance between the ball and the point defined by the user. By getting the distance without problems it is possible to regulate the motor speed to reach a value close to the setpoint value.

image description

code:

    #include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>

#include <iostream>
#include "Tserial.h"

/*#NAMESPACE*/
using namespace cv;
using namespace std;
int cntr = 0;   /*VAR INCREMENT COUNT*/

/*DEFINE*/
#define OUTPUT_WINDOW_NAME "PID CONTROLLER"                         /*DEFINE WINDOW NAME*/
#define USB_PORT_SERIAL "/dev/cu.usbmodemM4321001"                  /*DEFINE SERIAL PORT*/

/*FUNCTION*/
void onMouse(int event, int x, int y, int flags, void *param);      /*BUTTON COORDINATE EVENT*/
void ball_tracker(const cv::Mat& frame);                            /*ORANGE BALL TRACKING*/
void regionOfInterest(const cv::Mat& frame);

/*VALUES TO SET MAX AND MIN HSV TRACK BALL*/
int lowH = 10;                                                      /*SET LOW BALL HUE*/
int highH = 25;                                                     /*SET HIGH BALL HUE*/

int lowS = 100;                                                     /*SET LOW BALL SATURATION*/
int highS = 255;                                                    /*SET HIGH BALL SATURATION*/

int lowV = 20;                                                      /*SET LOW BALL VALUE*/
int highV = 255;                                                    /*SET HIGH BALL VALUE*/

/*VALUES TO CREATE AUTOMATIC ROI*/
int hueValue = 65;
int hueRange =  1;

int minSaturation = 20;
int minValue = 0;
/*SERIAL TO ARDUINO GLOBAL DECLARATIONS*/
int arduino_command;
Tserial *LAUNCHPAD_PORT;
short MSBLSB = 0;
unsigned char MSB = 0;
unsigned char LSB = 0;
/*END SERIAL TO ARDUINO GLOBAL DECLARATIONS*/



int main(int argc, char** argv) {
    //CvCapture *CAM_CAP;
    VideoCapture cap(0);
    Mat cameraFrame;

    LAUNCHPAD_PORT = new Tserial();
    if (LAUNCHPAD_PORT!=0) {
        LAUNCHPAD_PORT->connect(USB_PORT_SERIAL, 57600, spNONE);
    }

    Point point_marker(-10,-10);                                    /*START POINTER OF THE SETPOINT*/
    int retorno = 0;



    /*VERIFY IF EXISTS ANY CAMERA*/
    if(!cap.isOpened()){
        cout << "Failed to capture from camera" << endl;
        retorno = 1;
    }
    cout << "Camera opened successfully" << endl;


    /*DEFINE IMAGE SIZE AND NAME*/
    namedWindow(OUTPUT_WINDOW_NAME, CV_WINDOW_AUTOSIZE);

    while(true){
       // if((cameraFrame = cap(0))){

            //IplImage * ipl = cameraFrame;
            //cv::Mat matrix = cv::cvarrToMat(ipl);
            cap >> cameraFrame;
            Mat matrix = cameraFrame;
            /*CALL BALL TRACKING FUNCTION*/
            ball_tracker(matrix);
            /*CALL REGION OF INTEREST FUNCTION*/
            regionOfInterest(matrix);
            /*SHOW MOUSE EVENT SETPOINT*/
            if (point_marker.x != -1){
                drawMarker(matrix, point_marker,  Scalar(0, 0, 255), MARKER_CROSS, 10, 1);
            }
            /*MOUSE CALLBACKFUNCTION*/
            cvSetMouseCallback(OUTPUT_WINDOW_NAME, onMouse, (void*) (&point_marker));
            /*OPEN IMAGE AND SHOW WINDOW*/
            imshow(OUTPUT_WINDOW_NAME, cameraFrame);


            /**/


        //}
        if(cvWaitKey(60)!= -1){
            cout << "Camera disable successfully" <<endl;
            break;
        }
    }
    cout << "INPUT" <<endl;
    //cvReleaseCapture(&CAM_CAP);

    /*SERIAL LAUNCHPAD SHUTDOWN*/
    LAUNCHPAD_PORT->disconnect();
    delete LAUNCHPAD_PORT;
    LAUNCHPAD_PORT = 0;
    /*END SERIAL LAUNCHPAD SHUTDOWN*/
    cvDestroyWindow(OUTPUT_WINDOW_NAME);
    return retorno;
}


/*MOUSE EVENT ...
(more)
edit retag flag offensive close merge delete

Comments

please do NOT use opencv's deprecated C-api for anything. it is no more maintaned since 2010, and might be simply gone in the next version.

berak gravatar imageberak ( 2018-06-06 04:23:54 -0600 )edit

also, throwing a wall of unformatted code at us has zero chance at help.

berak gravatar imageberak ( 2018-06-06 04:25:18 -0600 )edit

thank you for your advices, but i am a beginner, and I have basic knowledge in opencv, if you can help me solve this problems i promised i will try to learn and update all my code.

biza gravatar imagebiza ( 2018-06-06 10:31:36 -0600 )edit

if you want ppl here to try your code (and reproduce problems)

  • you should change your c code to c++ (cv::VideoCapture and such)
  • cut it down to the minimum (all arduino related stuff is irrelevant to your "track a ball" problem)

i know, that's hard, but it will also help you identifying the src of your problem.

berak gravatar imageberak ( 2018-06-06 10:40:49 -0600 )edit
1

I change it

biza gravatar imagebiza ( 2018-06-06 14:21:30 -0600 )edit

in your ball tracking:

inRange(HSV_IMAGE, cv::Scalar(lowH, lowS, lowV), cv::Scalar(highH, highS, highV), TRESH_IMAGE);

your HSV_IMAGE is actually grayscale, single channel (why ?) so only lowH, highH are used, and that's a very narrow band [10..25] . while your values would have made sense for hsv (track orange), not so much for grayscale intensity.

berak gravatar imageberak ( 2018-06-07 01:15:11 -0600 )edit

Yes I know the image is grayscale. The problem is that if I put the code as follows "cvtColor( frame, HSV_IMAGE, CV_RGB2HSV);" my whole program breaks. I liked to get a clean tracking, so the rest of the project had no errors.

biza gravatar imagebiza ( 2018-06-09 10:44:40 -0600 )edit