Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can correct a problem color

Hello fellows, I made a program who was responsible for detect the orange ball position inside a transparent tube , witch are limited by 2 green color lines who represent the superior and inferior tube limit. In this moment my biggest problem is put the correct color in the ball and the limits. because they vary with luminosity, can anyone help me , I share my code. thank's in advance!

<code>image description</code>

How can correct a problem color

Hello fellows, I made a program who was responsible for detect the orange ball position inside a transparent tube , witch are limited by 2 green color lines who represent the superior and inferior tube limit. In this moment my biggest problem is put the correct color in the ball and the limits. because they vary with luminosity, can anyone help me , I share my code. thank's in advance!

<code>image description</code>

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <string>
#include <fstream>
#include <sstream>

/*
 ball tracker
 HSV
 MIN[0 208 186]
 MAX[47 255 255]



 int retorno = 0;

 RECTANGLE
 HSV
 MIN[25 189 118]
 MAX[95 255 255]
 */

/*#NAMESPACE*/
using namespace cv;
using namespace std;

int cntr = 0;                                                        /*VAR INCREMENT COUNT*/
int yUserMarker, yBallPosition;                                      /*INTEREST "VAR" POSITIONS*/
char str1[20];
char str2[20];

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

/*FUNCTION*/
void mouseMarker(int event, int x, int y, int flags, void *param);   /*USER BALL POSITION DEFENITION*/
void ballTracker(Mat& Matrix);            /*ORANGE BALL TRACKING*/
void imageMorphological(Mat& thresh);                                /*IMAGE MORPHOLOGICAL OPERATION's*/
void drawObject(int x, int y, Mat& Matrix);                          /*DRAW OUTLINE COUNTOUR's*/
string intToString(int number);                                      /*CONVERT INTEGER TO STRING*/
void regionOfInterest(Mat& frame);                                   /*DEFINE A REGION OF INTREST*/
int mousePoint(int yPoint);                                          /*MOUSE POINT*/
/*END FUNCTION*/

Point pointMarker(-10,10);                                           /*START POINT POSITION MARKER*/

/*VALUES TO SET MAX AND MIN HSV TRACK BALL*/
int H_MIN = 0;                                                      /*SET LOW BALL HUE*/
int H_MAX = 256;                                                    /*SET HIGH BALL HUE*/
int S_MIN = 0 ;                                                     /*SET LOW BALL SATURATION*/
int S_MAX = 256;                                                    /*SET HIGH BALL SATURATION*/
int V_MIN = 0;                                                      /*SET LOW BALL VALUE*/
int V_MAX = 256;                                                    /*SET HIGH BALL VALUE*/

int retorno = 0;


/*VALUES FOR THE ROI*/
int hueValue = 120;
int hueRange = 10;
int minSaturation = 90;
int minValue = 90;


int main(int argc, char** argv) {
    /*SERIAL TO ARDUINO GLOBAL DECLARATIONS*/
    fstream MSP430;
    MSP430.open(USB_PORT_SERIAL);
    /*VERIFY IF THE SERIAL PORT IS CONNECTED OR NOT*/
    if (MSP430.fail()) {
        /*FILE COULD NOT BE OPENED*/
        cout << "SERIAL OPEN FAILED" << endl;
    }else{
        cout << "SERIAL OPEN SUCESSEFUL" << endl;
    }
    /*CAMERA CAPTURE*/
    VideoCapture cap(0);
    /*MATRIX LINE LIMITS*/
    Mat limits;
    /*MATRIX STORAGE IMAGE CAMERA */
    Mat cameraFrame;
    /*VERIFY IF EXISTS ANY CAMERA*/

    if(!cap.isOpened()){
        cout << "CAMERA CAPTURE FAILED" << endl;
        retorno = 1;
    }
    cout << "CAMERA OPENED SUCCESSFULLY" << endl;

    /*DEFINE IMAGE SIZE AND NAME*/
    namedWindow(OUTPUT_WINDOW_NAME, WINDOW_NORMAL);   /*CREATE A NEW WINDOW*/




    while(true){

        cap >> cameraFrame;
        Mat Matrix = cameraFrame;
        Mat mask;

        /*SHOW MOUSE EVENT SET POINT*/
        if(pointMarker.x!=-1){
            drawMarker(Matrix, pointMarker, Scalar(0,0,255),MARKER_CROSS, 10, 1);
        }
        /*CALL MOUSE EVENT*/
        setMouseCallback(OUTPUT_WINDOW_NAME,  mouseMarker, (void*)(&pointMarker));
        /*LIMITS*/
        regionOfInterest(Matrix);
        /*TRACKING BALL*/
        ballTracker(Matrix);
        /*VALUES STREAM TO ARDUINO*/
        stringstream  yMarker, yPosition;

        yMarker << yUserMarker;
        yPosition << yBallPosition;

        rectangle(Matrix,Point(0, 670), Point(220,720), Scalar(0, 0, 0), -1);

        sprintf(str1,"SET  POINT: %d",yUserMarker);
        sprintf(str2,"BALL POINT: %d",yBallPosition);

        putText(Matrix, str1, Point2f(20,690), FONT_HERSHEY_PLAIN, 1,  Scalar(0,249,158,51),1);
        putText(Matrix, str2, Point2f(20,710), FONT_HERSHEY_PLAIN, 1,  Scalar(0,249,158,51),1);


       // cout << Matrix.rows << endl;
        fstream MSP430;
        MSP430.open(USB_PORT_SERIAL);
        MSP430 <<yMarker.str()<<"p"<<endl;                          /*ySetPoint*/
        MSP430 <<yPosition.str()<<"y"<<endl;                        /*yBallCenter*/
        //MSP430;
        MSP430.close();


        if(waitKey(60)!= -1){
            yUserMarker = 0;
            cout << "CAMERA DISABLE SUCCESSFULLY" <<endl;
            /*END SHUT DOWN SERIAL CONNECTION*/
            if (!MSP430.fail()) {
                MSP430.close();
                cout << "SERIAL CLOSE SUCESSEFUL" << endl;
            }
            break;
        }

        /*OPEN IMAGE AND SHOW WINDOW*/
        imshow(OUTPUT_WINDOW_NAME, Matrix);

    }

    /*SHUT DOWN SERIAL CONNECTION*/
    destroyWindow(OUTPUT_WINDOW_NAME);
    return retorno;
}


/*MOUSE EVENT CALLBACK*/
void mouseMarker(int event, int x, int y, int flags, void *param){
    Point* pointMarker = (Point*)param;                 /*START POINTER OF THE SETPOINT*/
    if  ( event == EVENT_LBUTTONDOWN ){
        if(cntr == 0){
            pointMarker->x = x;
            pointMarker->y = y;
            yUserMarker = y;
            cout << "DEFINED POINT:"<<pointMarker->x<<","<<pointMarker->y<<"\n"<<endl;
        }else{
            pointMarker->x = x;
            pointMarker->y = y;
            yUserMarker = y;
            cout << "DEFINED POINT: "<<pointMarker->x<<","<<pointMarker->y<<"\n"<<endl;
        }
        cntr = cntr + 1;
    }
}

/*IDENTIFY LIMITS TRACE A ROI ZONE*/
void regionOfInterest(Mat& Matrix){

    /*CONVERT TO HSV COLOR SPACE*/
    Mat hsvImage;

    cvtColor(Matrix,hsvImage,COLOR_BGR2HSV);


    vector<Mat>HSV_CHANNELS;
    split(hsvImage, HSV_CHANNELS);


    Mat hueImage = HSV_CHANNELS[0];
    imshow("test", hueImage);
    Mat hueMask;
    inRange(hueImage, hueValue - hueRange, hueValue + hueRange, hueMask);

    if (hueValue - hueRange < 0 || hueValue + hueRange > 180){
        Mat hueMaskUpper;

        int upperHueValue = hueValue + 180;
        inRange(hueImage, upperHueValue - hueRange, upperHueValue + hueRange, hueMaskUpper);


        hueMask = hueMask | hueMaskUpper;
    }

    Mat saturationMask = HSV_CHANNELS[1] > minSaturation;
    Mat valueMask = HSV_CHANNELS[2] > minValue;
    hueMask = (hueMask & saturationMask) & valueMask;



    vector<Vec4i> lines;
    HoughLinesP(hueMask, lines, 1, CV_PI/90, 50, 50, 10);

    for (unsigned int i = 0; i < lines.size(); ++i){
        Point(lines[i][0], lines[i][1]);
        Point(lines[i][2], lines[i][3]);
    }
    vector<Point>pts;
    for(unsigned int i = 0; i < lines.size(); i++){
        pts.push_back(Point(lines[i][0],lines[i][1]));
        pts.push_back(Point(lines[i][2],lines[i][3]));

    }
    /*GET THE PREVIOUS POINTS DETECTED IN THE IMAGE*/
    Rect box = boundingRect(pts);
    /*DRAW RECTANGLE REGION OF INTEREST*/
    rectangle(Matrix, box.tl(), box.br(), Scalar(0, 255, 0), 2);

}
/*END IDENTIFY LIMITS TRACE A ROI ZONE*/

/*BALL TRACKER FUNCTION*/
void ballTracker(Mat& Matrix){

    Mat imgHSV;                                                                 /*IMAGE HSV SPACE COLOR*/
    Mat imgThreshLow;                                                           /*IMAGE LOW THRESHOLD RESULT*/
    Mat imgThreshHigh;                                                          /*IMAGE HIGH THRESHOLD RESULT*/
    Mat imageThreshold;                                                         /*IMAGE THRESHOLD*/
    /*CONVERT ORIGINAL IMAGE TO HSV SPACE COLOR*/
    cvtColor(Matrix,imgHSV,COLOR_RGB2HSV);

    inRange(imgHSV, Scalar(16,155,155), Scalar(40, 255, 255), imgThreshLow);
    inRange(imgHSV, Scalar(95, 155, 155),Scalar(120, 255, 255), imgThreshHigh);

    add(imgThreshLow,imgThreshHigh,imageThreshold);

    GaussianBlur(imageThreshold, imageThreshold, Size(3,3), 0);

    Mat structuringElement = getStructuringElement(MORPH_RECT, Size(3, 3));

    dilate(imageThreshold, imageThreshold, structuringElement);
    erode(imageThreshold, imageThreshold, structuringElement);


    vector<Vec3f>v3fCircles;

    HoughCircles(imageThreshold, v3fCircles, HOUGH_GRADIENT, 2, imageThreshold.rows/4,100,50,10,400);
    for(int i = 0; i < v3fCircles.size();i++){
        cout << "Ball position X = "<< v3fCircles[i][0]   // x position of center point of circle
        <<",\tY = "<< v3fCircles[i][1]        // y position of center point of circle
        <<",\tRadius = "<< v3fCircles[i][2]<< "\n"<< endl;     // radius of circle

        yBallPosition = v3fCircles[i][1] ;
        circle(Matrix,Point((int)v3fCircles[i][0], (int)v3fCircles[i][1]),3,Scalar(0, 255, 0),FILLED);
        circle(Matrix,Point((int)v3fCircles[i][0], (int)v3fCircles[i][1]),(int)v3fCircles[i][2],Scalar(0, 0, 255),2);


    }
    /*END BALL TRACKER FUNCTION*/

}


/*PASSING MOUSE POINT VALUE*/
int mousePoint(int yPoint){
    return yPoint;
}
/*END PASSING MOUSE POINT VALUE*/

string intToString(int number){
    std::stringstream ss;
    ss << number;
    return ss.str();
}

How can correct a problem color

Hello fellows, I made a program who was responsible for detect the orange ball position inside a transparent tube , witch are limited by 2 green color lines who represent the superior and inferior tube limit. In this moment my biggest problem is put the correct color in the ball and the limits. because they vary with luminosity, can anyone help me , I share my code. thank's in advance!

<code>image description</code>

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <string>
#include <fstream>
#include <sstream>

/*
 ball tracker
 HSV
 MIN[0 208 186]
 MAX[47 255 255]



 int retorno = 0;

 RECTANGLE
 HSV
 MIN[25 189 118]
 MAX[95 255 255]
 */

/*#NAMESPACE*/
using namespace cv;
using namespace std;

int cntr = 0;                                                        /*VAR INCREMENT COUNT*/
int yUserMarker, yBallPosition;                                      /*INTEREST "VAR" POSITIONS*/
char str1[20];
char str2[20];

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

/*FUNCTION*/
void mouseMarker(int event, int x, int y, int flags, void *param);   /*USER BALL POSITION DEFENITION*/
void ballTracker(Mat& Matrix);            /*ORANGE BALL TRACKING*/
void imageMorphological(Mat& thresh);                                /*IMAGE MORPHOLOGICAL OPERATION's*/
void drawObject(int x, int y, Mat& Matrix);                          /*DRAW OUTLINE COUNTOUR's*/
string intToString(int number);                                      /*CONVERT INTEGER TO STRING*/
void regionOfInterest(Mat& frame);                                   /*DEFINE A REGION OF INTREST*/
int mousePoint(int yPoint);                                          /*MOUSE POINT*/
/*END FUNCTION*/

Point pointMarker(-10,10);                                           /*START POINT POSITION MARKER*/

/*VALUES TO SET MAX AND MIN HSV TRACK BALL*/
int H_MIN = 0;                                                      /*SET LOW BALL HUE*/
int H_MAX = 256;                                                    /*SET HIGH BALL HUE*/
int S_MIN = 0 ;                                                     /*SET LOW BALL SATURATION*/
int S_MAX = 256;                                                    /*SET HIGH BALL SATURATION*/
int V_MIN = 0;                                                      /*SET LOW BALL VALUE*/
int V_MAX = 256;                                                    /*SET HIGH BALL VALUE*/

int retorno = 0;


/*VALUES FOR THE ROI*/
int hueValue = 120;
int hueRange = 10;
int minSaturation = 90;
int minValue = 90;


int main(int argc, char** argv) {
    /*SERIAL TO ARDUINO GLOBAL DECLARATIONS*/
    fstream MSP430;
    MSP430.open(USB_PORT_SERIAL);
    /*VERIFY IF THE SERIAL PORT IS CONNECTED OR NOT*/
    if (MSP430.fail()) {
        /*FILE COULD NOT BE OPENED*/
        cout << "SERIAL OPEN FAILED" << endl;
    }else{
        cout << "SERIAL OPEN SUCESSEFUL" << endl;
    }
    /*CAMERA CAPTURE*/
    VideoCapture cap(0);
    /*MATRIX LINE LIMITS*/
    Mat limits;
    /*MATRIX STORAGE IMAGE CAMERA */
    Mat cameraFrame;
    /*VERIFY IF EXISTS ANY CAMERA*/

    if(!cap.isOpened()){
        cout << "CAMERA CAPTURE FAILED" << endl;
        retorno = 1;
    }
    cout << "CAMERA OPENED SUCCESSFULLY" << endl;

    /*DEFINE IMAGE SIZE AND NAME*/
    namedWindow(OUTPUT_WINDOW_NAME, WINDOW_NORMAL);   /*CREATE A NEW WINDOW*/




    while(true){

        cap >> cameraFrame;
        Mat Matrix = cameraFrame;
        Mat mask;

        /*SHOW MOUSE EVENT SET POINT*/
        if(pointMarker.x!=-1){
            drawMarker(Matrix, pointMarker, Scalar(0,0,255),MARKER_CROSS, 10, 1);
        }
        /*CALL MOUSE EVENT*/
        setMouseCallback(OUTPUT_WINDOW_NAME,  mouseMarker, (void*)(&pointMarker));
        /*LIMITS*/
        regionOfInterest(Matrix);
        /*TRACKING BALL*/
        ballTracker(Matrix);
        /*VALUES STREAM TO ARDUINO*/
        stringstream  yMarker, yPosition;

        yMarker << yUserMarker;
        yPosition << yBallPosition;

        rectangle(Matrix,Point(0, 670), Point(220,720), Scalar(0, 0, 0), -1);

        sprintf(str1,"SET  POINT: %d",yUserMarker);
        sprintf(str2,"BALL POINT: %d",yBallPosition);

        putText(Matrix, str1, Point2f(20,690), FONT_HERSHEY_PLAIN, 1,  Scalar(0,249,158,51),1);
        putText(Matrix, str2, Point2f(20,710), FONT_HERSHEY_PLAIN, 1,  Scalar(0,249,158,51),1);


       // cout << Matrix.rows << endl;
        fstream MSP430;
        MSP430.open(USB_PORT_SERIAL);
        MSP430 <<yMarker.str()<<"p"<<endl;                          /*ySetPoint*/
        MSP430 <<yPosition.str()<<"y"<<endl;                        /*yBallCenter*/
        //MSP430;
        MSP430.close();


        if(waitKey(60)!= -1){
            yUserMarker = 0;
            cout << "CAMERA DISABLE SUCCESSFULLY" <<endl;
            /*END SHUT DOWN SERIAL CONNECTION*/
            if (!MSP430.fail()) {
                MSP430.close();
                cout << "SERIAL CLOSE SUCESSEFUL" << endl;
            }
            break;
        }

        /*OPEN IMAGE AND SHOW WINDOW*/
        imshow(OUTPUT_WINDOW_NAME, Matrix);

    }

    /*SHUT DOWN SERIAL CONNECTION*/
    destroyWindow(OUTPUT_WINDOW_NAME);
    return retorno;
}


/*MOUSE EVENT CALLBACK*/
void mouseMarker(int event, int x, int y, int flags, void *param){
    Point* pointMarker = (Point*)param;                 /*START POINTER OF THE SETPOINT*/
    if  ( event == EVENT_LBUTTONDOWN ){
        if(cntr == 0){
            pointMarker->x = x;
            pointMarker->y = y;
            yUserMarker = y;
            cout << "DEFINED POINT:"<<pointMarker->x<<","<<pointMarker->y<<"\n"<<endl;
        }else{
            pointMarker->x = x;
            pointMarker->y = y;
            yUserMarker = y;
            cout << "DEFINED POINT: "<<pointMarker->x<<","<<pointMarker->y<<"\n"<<endl;
        }
        cntr = cntr + 1;
    }
}

/*IDENTIFY LIMITS TRACE A ROI ZONE*/
void regionOfInterest(Mat& Matrix){

    /*CONVERT TO HSV COLOR SPACE*/
    Mat hsvImage;

    cvtColor(Matrix,hsvImage,COLOR_BGR2HSV);


    vector<Mat>HSV_CHANNELS;
    split(hsvImage, HSV_CHANNELS);


    Mat hueImage = HSV_CHANNELS[0];
    imshow("test", hueImage);
    Mat hueMask;
    inRange(hueImage, hueValue - hueRange, hueValue + hueRange, hueMask);

    if (hueValue - hueRange < 0 || hueValue + hueRange > 180){
        Mat hueMaskUpper;

        int upperHueValue = hueValue + 180;
        inRange(hueImage, upperHueValue - hueRange, upperHueValue + hueRange, hueMaskUpper);


        hueMask = hueMask | hueMaskUpper;
    }

    Mat saturationMask = HSV_CHANNELS[1] > minSaturation;
    Mat valueMask = HSV_CHANNELS[2] > minValue;
    hueMask = (hueMask & saturationMask) & valueMask;



    vector<Vec4i> lines;
    HoughLinesP(hueMask, lines, 1, CV_PI/90, 50, 50, 10);

    for (unsigned int i = 0; i < lines.size(); ++i){
        Point(lines[i][0], lines[i][1]);
        Point(lines[i][2], lines[i][3]);
    }
    vector<Point>pts;
    for(unsigned int i = 0; i < lines.size(); i++){
        pts.push_back(Point(lines[i][0],lines[i][1]));
        pts.push_back(Point(lines[i][2],lines[i][3]));

    }
    /*GET THE PREVIOUS POINTS DETECTED IN THE IMAGE*/
    Rect box = boundingRect(pts);
    /*DRAW RECTANGLE REGION OF INTEREST*/
    rectangle(Matrix, box.tl(), box.br(), Scalar(0, 255, 0), 2);

}
/*END IDENTIFY LIMITS TRACE A ROI ZONE*/

/*BALL TRACKER FUNCTION*/
void ballTracker(Mat& Matrix){

    Mat imgHSV;                                                                 /*IMAGE HSV SPACE COLOR*/
    Mat imgThreshLow;                                                           /*IMAGE LOW THRESHOLD RESULT*/
    Mat imgThreshHigh;                                                          /*IMAGE HIGH THRESHOLD RESULT*/
    Mat imageThreshold;                                                         /*IMAGE THRESHOLD*/
    /*CONVERT ORIGINAL IMAGE TO HSV SPACE COLOR*/
    cvtColor(Matrix,imgHSV,COLOR_RGB2HSV);

    inRange(imgHSV, Scalar(16,155,155), Scalar(40, 255, 255), imgThreshLow);
    inRange(imgHSV, Scalar(95, 155, 155),Scalar(120, 255, 255), imgThreshHigh);

    add(imgThreshLow,imgThreshHigh,imageThreshold);

    GaussianBlur(imageThreshold, imageThreshold, Size(3,3), 0);

    Mat structuringElement = getStructuringElement(MORPH_RECT, Size(3, 3));

    dilate(imageThreshold, imageThreshold, structuringElement);
    erode(imageThreshold, imageThreshold, structuringElement);


    vector<Vec3f>v3fCircles;

    HoughCircles(imageThreshold, v3fCircles, HOUGH_GRADIENT, 2, imageThreshold.rows/4,100,50,10,400);
    for(int i = 0; i < v3fCircles.size();i++){
        cout << "Ball position X = "<< v3fCircles[i][0]   // x position of center point of circle
        <<",\tY = "<< v3fCircles[i][1]        // y position of center point of circle
        <<",\tRadius = "<< v3fCircles[i][2]<< "\n"<< endl;     // radius of circle

        yBallPosition = v3fCircles[i][1] ;
        circle(Matrix,Point((int)v3fCircles[i][0], (int)v3fCircles[i][1]),3,Scalar(0, 255, 0),FILLED);
        circle(Matrix,Point((int)v3fCircles[i][0], (int)v3fCircles[i][1]),(int)v3fCircles[i][2],Scalar(0, 0, 255),2);


    }
    /*END BALL TRACKER FUNCTION*/

}


/*PASSING MOUSE POINT VALUE*/
int mousePoint(int yPoint){
    return yPoint;
}
/*END PASSING MOUSE POINT VALUE*/

string intToString(int number){
    std::stringstream ss;
    ss << number;
    return ss.str();
}