Ask Your Question

grisi's profile - activity

2016-01-14 05:57:21 -0500 asked a question opencv videocapture lag

hi everybody,

we have implemented a colour detection, as can be seen in the attached code. the problem is, that when moving the object, that shall be detected, reveils a delay of about 3 seconds. we assume the problem is the buffer size of the processed frames is too big.

a solution might be to process only every n-th frame, instead of each. unfortunately I can't access the lab these days, so if someone has further ideas I'm very thankful for every input.

#include <cv.h>
#include <highgui.h>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include <math.h>

using namespace cv;

//initial min and max HSV filter values.
//these will be changed using trackbars
int H_MIN = 167;
int H_MAX = 195;
int S_MIN = 121;
int S_MAX = 256;
int V_MIN = 0;
int V_MAX = 256;


//default capture width and height
const int FRAME_WIDTH = 640;
const int FRAME_HEIGHT = 480;
//max number of objects to be detected in frame
const int MAX_NUM_OBJECTS=50;
//minimum and maximum object area
const int MIN_OBJECT_AREA = 5*5;
const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH/1.5;
//names that will appear at the top of each window
const string windowName = "Original Image";
const string windowName1 = "HSV Image";
const string windowName2 = "Thresholded Image";
const string windowName3 = "After Morphological Operations";
const string trackbarWindowName = "Trackbars";



// Marker Jump
#define  ArraySize 5
int CoordinatesHistory[ArraySize][2];
int CurrentCoordinates[1][2];

int distance = 0;
int radius = 0;
int history = 0;


void on_trackbar( int, void* )
{//This function gets called whenever a
    // trackbar position is changed

}

string intToString(int number){
    std::stringstream ss;
    ss << number;
    return ss.str();
}
void createTrackbars(){
    //create window for trackbars
     createTrackbar( "H_MIN", trackbarWindowName, &H_MIN, H_MAX, on_trackbar );
    createTrackbar( "H_MAX", trackbarWindowName, &H_MAX, H_MAX, on_trackbar );
    createTrackbar( "S_MIN", trackbarWindowName, &S_MIN, S_MAX, on_trackbar );
    createTrackbar( "S_MAX", trackbarWindowName, &S_MAX, S_MAX, on_trackbar );
    createTrackbar( "V_MIN", trackbarWindowName, &V_MIN, V_MAX, on_trackbar );
    createTrackbar( "V_MAX", trackbarWindowName, &V_MAX, V_MAX, on_trackbar );
 }
void drawObject(int x, int y,Mat &frame){

    //use some of the openCV drawing functions to draw crosshairs
    //on your tracked image!

    //UPDATE:JUNE 18TH, 2013
    //added 'if' and 'else' statements to prevent
    //memory errors from writing off the screen (ie. (-25,-25) is not within the window!)

    circle(frame,Point(x,y),20,Scalar(0,255,0),2);
    if(y-25>0)
    line(frame,Point(x,y),Point(x,y-25),Scalar(0,255,0),2);
    else line(frame,Point(x,y),Point(x,0),Scalar(0,255,0),2);
    if(y+25<FRAME_HEIGHT)
    line(frame,Point(x,y),Point(x,y+25),Scalar(0,255,0),2);
    else line(frame,Point(x,y),Point(x,FRAME_HEIGHT),Scalar(0,255,0),2);
    if(x-25>0)
    line(frame,Point(x,y),Point(x-25,y),Scalar(0,255,0),2);
    else line(frame,Point(x,y),Point(0,y),Scalar(0,255,0),2);
    if(x+25<FRAME_WIDTH)
    line(frame,Point(x,y),Point(x+25,y),Scalar(0,255,0),2);
    else line(frame,Point(x,y),Point(FRAME_WIDTH,y),Scalar(0,255,0),2);

    putText(frame,intToString(x)+","+intToString(y),Point(x,y+30),1,1,Scalar(0 ...
(more)
2016-01-11 05:10:05 -0500 asked a question automatic detection of HSV values

hello everybody,

I got an object tracking which is based on findContours. The HSV values are set now by trackbars and it is working well. nevertheless I would like to do the calibration of the HSV values dynamically. Assuming a red ball must be tracked. I thought about setting up an area of interest and reading out the HSV values in this area. maybe does anyone have experience with that, or there is a way better method, to implement such a feature. I'm thankful for every input.

best regards, grisi

2015-12-05 07:04:44 -0500 commented answer Colour + Contour Detection

hello,

ok I see. In my case the ball isn't moving.

may you post an image of your threshold image? I posted mine recently and it looked weird. Is your's looking differently?

best regrads, chris

2015-12-02 07:55:48 -0500 received badge  Scholar (source)
2015-12-02 07:55:40 -0500 received badge  Scholar (source)
2015-12-02 07:20:29 -0500 commented answer Colour + Contour Detection

"BTW I suggest to track the motion or the position between frames instead of circles"

May you tell me why you would suggest that?

2015-12-02 06:42:28 -0500 commented answer Colour + Contour Detection

hello,

I implemented your corrections, but there is no change so far. i will try to fix it and post the status afterwards

greetings, bibof

2015-12-01 14:09:55 -0500 commented answer Colour + Contour Detection

thx, sorry I'm just really new to opencv and c++. everything takes a bit longer.

I will continue with working by tomorrow and then I can give you a feedback. thank you

2015-11-30 10:29:15 -0500 received badge  Editor (source)
2015-11-30 03:06:53 -0500 commented answer Colour + Contour Detection

hey pklab,

"ok do it your comparison, try your HoughCircles+countour with countour filtered by circularity and let us know ;)"

I'm not sure how to implement this.

maybe here? :

if(area>MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea &&  CircularityStandard > 0.7   )

may you also tell me what you mean with: "you are looking for largest area. Maybe it isn't the ball. Sure your control var objectFound id bad managed because it can become false after it has been true."

thank you!!

best, chris

2015-11-30 02:42:07 -0500 received badge  Enthusiast
2015-11-29 09:31:10 -0500 commented answer Colour + Contour Detection

hello :)

yes I will let you know by tomorrow!! Thanks in advance, have a nice sunday

best chris

2015-11-28 12:17:26 -0500 commented answer Colour + Contour Detection

hey pklab,

I'm have troubling with setting up openCV in eclipse on my windows machine. (usually I'm working on eclipse in linux at university) so I can't run that code now, but on monday when I'm back at uni.

i got a short question:

"You are mixing contours and circles from HoughCircles in a obscure way. Detected circles OR contours and select your favourite."

I'm searching for a condition that "combines" both cases, just to filter out everything that is red and not a ball.

there must always be one tracked object, because the coordinates of that will be sent to a pid controller which adjusts the flight direction of the drone.

thanks :) chris

2015-11-28 09:07:48 -0500 commented answer Colour + Contour Detection

Hello,

thank you very much. I deleted my answer as whished. I will have a look at your code now and answer later on!! :-)

Best, chris

2015-11-27 08:04:53 -0500 answered a question Colour + Contour Detection

Hello Guys,

thank you very much for your help!

First of all, here are the pictures when there is just one red ball and when there is a red ball + another red object. In that case the tracking won't work anymore, so I'd like to combine colour + shape detection.

C:\fakepath\1_object.jpg

C:\fakepath\2_objects.jpg

So far I'm using an existing code which looks like that:

 void trackFilteredObject(int &x, int &y, Mat threshold, Mat &cameraFeed){

        vector<Vec3f> circles;
        Mat temp;
        threshold.copyTo(temp);
        vector< vector<Point> > contours;
        vector<Vec4i> hierarchy;
        GaussianBlur( threshold, threshold, Size(9, 9), 2, 2 );
        findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );
        HoughCircles( threshold, circles, CV_HOUGH_GRADIENT, 1, threshold.rows/8, P1_MIN, P2_MIN, RADIUS_MIN, 0 );




        double refArea = 0;
        bool objectFound = false;
        if (hierarchy.size() > 0) {
            int numObjects = hierarchy.size();
            //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
            if(numObjects<MAX_NUM_OBJECTS){
                for (int index = 0; index >= 0; index = hierarchy[index][0]) {

                    Moments moment = moments((cv::Mat)contours[index]);
                    double area = moment.m00;

                    //if the area is less than 20 px by 20px then it is probably just noise
                    //if the area is the same as the 3/2 of the image size, probably just a bad filter
                    //object wanted with the largest area so a reference area is stored each
                    //iteration and compare it to the area in the next iteration.


                    if(area>MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea &&  not circles.empty()   ){
                        x = moment.m10/area;
                        y = moment.m01/area;
                        objectFound = true;
                        refArea = area;

                        for( size_t i = 0; i < circles.size(); i++ )
                            {      int x = circles.size();
                                    std::cout << x << std::endl;

                                    Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
                                    int radius = cvRound(circles[i][2]);
                                    // circle center
                                    circle(cameraFeed , center, 3, Scalar(0,255,0), -1, 8, 0 );
                                    // circle outline
                                    circle( cameraFeed, center, radius, Scalar(0,0,255), 3, 8, 0 );

                            }




                    }else objectFound = false;


                }
                //let user know you found an object
                if(objectFound ==true){
                    putText(cameraFeed,"Tracking Object",Point(0,50),2,1,Scalar(0,255,0),2);
                    //draw object location on screen
                    drawObject(x,y,cameraFeed);}

            }else putText(cameraFeed,"TOO MUCH NOISE! ADJUST FILTER",Point(0,50),1,2,Scalar(0,0,255),2);
        }
    }

    int main(int argc, char* argv[])
    {
        //some boolean variables for different functionality within this
        //program
        bool trackObjects = true;
        bool useMorphOps = true;
        //Matrix to store each frame of the webcam feed
        Mat cameraFeed;
        //matrix storage for HSV image
        Mat HSV;
        //matrix storage for binary threshold image
        Mat threshold;
        //x and y values for the location of the object
        int x=0, y=0;
        //create slider bars for HSV filtering
        createTrackbars();
        //video capture object to acquire webcam feed





        //open capture object at location zero (default location for webcam)
        VideoCapture capture(0);
        //capture.open(0);// open the video file for reading
        //set height and width of capture frame
        capture.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);
        capture.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);
        //start an infinite loop where webcam feed is copied to cameraFeed matrix
        //operations will be performed within this loop
        while(1 ...
(more)
2015-11-26 09:17:43 -0500 received badge  Student (source)
2015-11-26 05:48:19 -0500 commented question Colour + Contour Detection

Hello sturkmen,

thanks for ur help and sorry for the late reply

unfortunately I can't post it in the comment section since the amount of characters is restricted. I could post it in the answer-section but I need to wait 2 days before I can answer my own question

best, bibof

2015-11-25 07:44:34 -0500 asked a question Colour + Contour Detection

hey pklab,

sorry for late reply, eclipse stated some really weird errors, now it's finally building: The actual code is this one ( I replaced my TrackingObject function with yours):

the picture looks as following. Would you suggest to improve the code or to go back to the old one? Can you explain me, why the picture looks like that?

contours.jpg

#include <cv.h>
#include <highgui.h>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;


//initial min and max HSV filter values.
//these will be changed using trackbars
int H_MIN = 0;
int H_MAX = 256;
int S_MIN = 0;
int S_MAX = 256;
int V_MIN = 0;
int V_MAX = 256;

//default capture width and height
const int FRAME_WIDTH = 640;
const int FRAME_HEIGHT = 480;
//max number of objects to be detected in frame
const int MAX_NUM_OBJECTS=50;
//minimum and maximum object area
const int MIN_OBJECT_AREA = 5*5;
const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH/1.5;
//names that will appear at the top of each window
const string windowName = "Original Image";
const string windowName1 = "HSV Image";
const string windowName2 = "Thresholded Image";
const string windowName3 = "After Morphological Operations";
const string trackbarWindowName = "Trackbars";
void on_trackbar( int, void* )
{//This function gets called whenever a
    // trackbar position is changed
 }
string intToString(int number){


    std::stringstream ss;
    ss << number;
    return ss.str();
}
void createTrackbars(){
    //create window for trackbars


    namedWindow(trackbarWindowName,0);


    //create trackbars and insert them into window
    //3 parameters are: the address of the variable that is changing when the trackbar is moved(eg.H_LOW),
    //the max value the trackbar can move (eg. H_HIGH),
    //and the function that is called whenever the trackbar is moved(eg. on_trackbar)
    //                                  ---->    ---->     ---->
    createTrackbar( "H_MIN", trackbarWindowName, &H_MIN, H_MAX, on_trackbar );
    createTrackbar( "H_MAX", trackbarWindowName, &H_MAX, H_MAX, on_trackbar );
    createTrackbar( "S_MIN", trackbarWindowName, &S_MIN, S_MAX, on_trackbar );
    createTrackbar( "S_MAX", trackbarWindowName, &S_MAX, S_MAX, on_trackbar );
    createTrackbar( "V_MIN", trackbarWindowName, &V_MIN, V_MAX, on_trackbar );
    createTrackbar( "V_MAX", trackbarWindowName, &V_MAX, V_MAX, on_trackbar );




}
void drawObject(int x, int y,Mat &frame){

    //use some of the openCV drawing functions to draw crosshairs
    //on your tracked image!

    //UPDATE:JUNE 18TH, 2013
    //added 'if' and 'else' statements to prevent
    //memory errors from writing off the screen (ie. (-25,-25) is not within the window!)

    circle(frame,Point(x,y),20,Scalar(0,255,0),2);
    if(y-25>0)
    line(frame,Point(x,y),Point(x,y-25),Scalar(0,255,0),2);
    else line(frame,Point(x,y),Point(x,0),Scalar(0,255,0),2);
    if(y+25<FRAME_HEIGHT)
    line(frame,Point(x,y),Point(x,y+25),Scalar(0,255,0),2);
    else line(frame,Point(x,y),Point(x,FRAME_HEIGHT),Scalar(0,255,0),2);
    if(x-25>0)
    line(frame,Point(x,y),Point(x-25,y),Scalar(0,255,0),2);
    else line(frame,Point(x,y),Point(0,y),Scalar(0,255,0),2);
    if(x+25<FRAME_WIDTH)
    line(frame,Point(x,y),Point(x+25,y),Scalar(0,255,0),2);
    else line(frame,Point(x,y),Point(FRAME_WIDTH,y),Scalar(0,255,0),2);

    putText(frame,intToString(x)+","+intToString(y),Point(x,y+30),1,1 ...
(more)