Click here to Skip to main content
14,575,022 members
Rate this:
Please Sign up or sign in to vote.
See more:
I've an object tracking program as below.The problem is I want to make it into The Main program as a thread to run both the Main program and this program parallel:
  #include"sstream"
  #include"ostream"
  #include"string"
  #include"opencv2/highgui.hpp"
  #include"opencv2/opencv.hpp"
  #include"stdio.h"

  using namespace cv;
  using namespace std;

  int H_MIN = 0;
  int H_MAX = 256;
  int S_MIN = 0;
  int S_MAX = 256;
  int V_MIN = 0;
  int V_MAX = 256;

  const string windowName1 = "Original image";
  const string windowName2 = "HSV image";
  const string windowName3 = "Thresholded image";
  const string windowName4 = "After Morphological Operations";
  const string trackbarWindowName = "Trackbars";
  const int FRAME_HEIGHT = 480;
  const int FRAME_WIDTH = 640;
  //max number of objects to be detected in frame
  const int MAX_NUM_OBJECTS = 2;
  //minimum and maximum object area
  const int MIN_OBJECT_AREA = 20 * 20;
  const int MAX_OBJECT_AREA = 64 * 48 / 1.5;
  double robotposx, robotposy ;
  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 memory to store trackbar name on window
  char TrackbarName[50];
  sprintf_s(TrackbarName, "H_MIN", H_MIN);
  sprintf_s(TrackbarName, "H_MAX", H_MAX);
  sprintf_s(TrackbarName, "S_MIN", S_MIN);
  sprintf_s(TrackbarName, "S_MAX", S_MAX);
  sprintf_s(TrackbarName, "V_MIN", V_MIN);
  sprintf_s(TrackbarName, "V_MAX", V_MAX);
  //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!

  //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, 255, 0), 2);

  }
  void morphOps(Mat& thresh) {

  //create structuring element that will be used to "dilate" and "erode" image.
  //the element chosen here is a 3px by 3px rectangle

  Mat erodeElement = getStructuringElement(MORPH_RECT, Size(8, 8));
  //dilate with larger element so make sure object is nicely visible
  Mat dilateElement = getStructuringElement(MORPH_RECT, Size(8, 8));
  erode(thresh, thresh, erodeElement);
  erode(thresh, thresh, erodeElement);
  dilate(thresh, thresh, dilateElement);
  dilate(thresh, thresh, dilateElement);



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

  Mat temp;
  threshold.copyTo(temp);
  //these two vectors needed for output of findContours
  vector< vector<Point> > contours;
  vector<Vec4i> hierarchy;
  //find contours of filtered image using openCV findContours function
  findContours(temp, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);
  //use moments method to find our filtered object
  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
//we only want the object with the largest area so we safe a reference area each
 //iteration and compare it to the area in the next iteration.
  if (area > MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea) {
  x = moment.m10 / area;
  y = moment.m01 / area;
  objectFound = true;
  refArea = area;
      }
  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 = false;
  int iLastX1 = -1;
  int iLastY1 = -1;
  int iLastX2 = -1;
  int iLastY2 = -1;
  //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
  VideoCapture capture;
  //open capture object at location zero (default location for webcam)
  capture.open(0);
  //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);
  Mat imgTmp1;//,imgTmp2;
  capture.read(imgTmp1);
  Mat imgLines1 = Mat::zeros(imgTmp1.size(), CV_8UC3);;
  //Mat imgLines = Mat::zeros(imgTmp1.size(), CV_8UC3);;
  //start an infinite loop where webcam feed is copied to cameraFeed matrix
  //all of our operations will be performed within this loop
  Moments oMoments1 = moments(threshold);
  double d1M01 = oMoments1.m01;
  double d1M10 = oMoments1.m10;
  double d1Area = oMoments1.m00;
  double Xa = 22957,Ya = 17037, xa = 526, ya = 246;
  double Xb = 22957, Yb = 2437, xb = 123, yb = 246;
  double Xd = 12356, Yd = 17037, xd = 590, yd = 0;
  double Xe = 22957, Ye = 9437, xe = 315, ye = 0;

  double aw = -0.260163;
  double bw = 275.0;
  double c = 1057.03;
  double a = -2.70684e-8;
  double b = -6.11588e-4;

  while (1) {
      //store image to matrix
      capture.read(cameraFeed);
      //convert frame from BGR to HSV colorspace
      cvtColor(cameraFeed, HSV, COLOR_BGR2HSV);
      //filter HSV image between values and store filtered image to
      //threshold matrix
  inRange(HSV, Scalar(H_MIN, S_MIN, V_MIN), Scalar(H_MAX, S_MAX, V_MAX), threshold);
  //perform morphological operations on thresholded image to eliminate noise
      //and emphasize the filtered object(s)
      if (useMorphOps)
          morphOps(threshold);
      //pass in thresholded frame to our object tracking function
      //this function will return the x and y coordinates of the
      //filtered object
      if (trackObjects)
          trackFilteredObject(x, y, threshold, cameraFeed);
      Moments oMoments1 = moments(threshold);
      double d1M01 = oMoments1.m01;
      double d1M10 = oMoments1.m10;
      double d1Area = oMoments1.m00;

      if (d1Area > 10000)
      {
          //calculate the position of the ball
          int posX1 = d1M10 / d1Area;
          int posY1 = d1M01 / d1Area;

      if ( posX1 >= 0 && posY1 >= 0)
          {

      /*Draw a red line from the previous point to the current point*/

          robotposx = (1.0 / (posY1 - c) - b) / a;
          robotposy = (posX1 - xe) * (Yd - Ye) / (aw * posY1 + bw) + Ye;
    line(threshold, Point(posX1-10, posY1), Point(posX1+10, posY1), Scalar(255, 0,
    0), 2);
    line(threshold, Point(posX1 , posY1-10), Point(posX1, posY1+10), Scalar(255, 0,
    0), 2);
   }

          iLastX1 = posX1;
          iLastY1 = posY1;
       //Find the Contour of the Object
          vector<vector<cv::Point>> contours;
  findContours(threshold, contours, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);
          for (int i = 0; i < contours.size(); i++)
      {
          drawContours(threshold, contours, i, Scalar(255, 0, 0), 2);

          }



          //show frames
          imshow(windowName3, threshold);
          imshow(windowName1, cameraFeed);
          imshow(windowName2, HSV);


          //delay 30ms so that screen can refresh.
          //image will not appear without this waitKey() command
          waitKey(1);
      }
  }





  return 0;
    }


What I have tried:

I've put the part in int main(int argc, char* argv[]) into The Main program and create the thread by lambda expression like this but the program didn't run at all(maybe it was heavy and can not load)

int main(int argc, char* argv[])
		{
			bool trackObjects = true;
			bool useMorphOps = false;
			int iLastX1 = -1;
			int iLastY1 = -1;
			int iLastX2 = -1;
			int iLastY2 = -1;
			Mat cameraFeed;
			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
			VideoCapture capture;
		//open capture object at location zero (default location for webcam)
			capture.open(0);
			//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);
			Mat imgTmp1;//,imgTmp2;
			capture.read(imgTmp1);
			Mat imgLines1 = Mat::zeros(imgTmp1.size(), CV_8UC3);;
			//Mat imgLines = Mat::zeros(imgTmp1.size(), CV_8UC3);;
	//start an infinite loop where webcam feed is copied to cameraFeed matrix
	//all of our operations will be performed within this loop
			Moments oMoments1 = moments(threshold);
			double d1M01 = oMoments1.m01;
			double d1M10 = oMoments1.m10;
			double d1Area = oMoments1.m00;
			double Xa = 22957, Ya = 17037, xa = 526, ya = 246;
			double Xb = 22957, Yb = 2437, xb = 123, yb = 246;
			double Xd = 12356, Yd = 17037, xd = 590, yd = 0;
			double Xe = 22957, Ye = 9437, xe = 315, ye = 0;

			double aw = -0.260163;
			double bw = 275.0;
			double c = 1057.03;
			double a = -2.70684e-8;
			double b = -6.11588e-4;


			//store image to matrix
			capture.read(cameraFeed);
			//convert frame from BGR to HSV colorspace
			cvtColor(cameraFeed, HSV, COLOR_BGR2HSV);
			//filter HSV image between values and store filtered image to
			//threshold matrix
    inRange(HSV, Scalar(H_MIN, S_MIN, V_MIN), Scalar(H_MAX, S_MAX, V_MAX), threshold);
	//perform morphological operations on thresholded image to eliminate noise
			//and emphasize the filtered object(s)
			if (useMorphOps)
				morphOps(threshold);
			//pass in thresholded frame to our object tracking function
			//this function will return the x and y coordinates of the
			//filtered object
			if (trackObjects)
				trackFilteredObject(x, y, threshold, cameraFeed);

			if (d1Area > 10000)
			{
				//calculate the position of the ball
				int posX1 = d1M10 / d1Area;
				int posY1 = d1M01 / d1Area;
				//int posX2 = d2M10 / d2Area;
				//int posY2 = d2M01 / d2Area;

				if ( posX1 >= 0 && posY1 >= 0)
				{

		/*Draw a red line from the previous point to the current point*/
		robotposx = (1.0 / (posY1 - c) - b) / a;
		robotposy = (posX1 - xe) * (Yd - Ye) / (aw * posY1 + bw) + Ye;

    line(threshold, Point(posX1 - 10, posY1), Point(posX1 + 10, posY1), Scalar(255, 0, 
    0), 2);
    line(threshold, Point(posX1, posY1 - 10), Point(posX1, posY1 + 10), Scalar(255, 0, 
    0), 2);
					}
      iLastX1 = posX1;
      iLastY1 = posY1;
				//Find the Contour of the Object
				vector<vector<cv::Point> > balls;
				vector<cv::Rect> ballsBox;
				vector<vector<cv::Point>> contours;
	findContours(threshold, contours, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);
	std::thread t1([&]() {
		for (int i = 0; i < contours.size(); i++)
			{
		drawContours(threshold, contours, i, Scalar(255, 0, 0), 2);
	        cv::Rect bBox;
		bBox = cv::boundingRect(contours[i]);
                float ratio = (float)bBox.width / (float)bBox.height;
		if (ratio > 1.0f)
		ratio = 1.0f / ratio;
                // Searching for a bBox almost square
		if (ratio > 0.75 && bBox.area() >= 400)
		    {
			balls.push_back(contours[i]);
			ballsBox.push_back(bBox);
		    }
                    cv::Point center;
		    center.x = ballsBox[i].x + ballsBox[i].width / 2;
		    center.y = ballsBox[i].y + ballsBox[i].height / 2;
		    cv::circle(threshold, center, 2, CV_RGB(20, 150, 20), -1);
                    stringstream sstr;
		    sstr << "(" << center.x << "," << center.y << ")";
		    cv::putText(threshold, sstr.str(),
		    cv::Point(center.x + 3, center.y - 3),
		    cv::FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(20, 150, 20), 2);
					}
					});
		    t1.join();
				//show frames 
				imshow(windowName3, threshold);
				imshow(windowName1, cameraFeed);
				imshow(windowName2, HSV);
                    //delay 30ms so that screen can refresh.
		    //image will not appear without this waitKey() command
		    waitKey(10);
			}

Thank you!
Posted
Updated 8-Jan-20 21:19pm
v2
Comments
Stefan_Lang 10-Jan-20 3:37am
   
I can' make much sense of your code, and what you say you did or want doesn't sound any more sensible to me. There are plenty of articles and tips on the topic of running code in parallel. If that doesn't help, you should spend a little more effort explaining what you actually want to achieve, and, more importantly, what is going wrong. We're not going to spend hours on analyzing your code and guessing what you might have intended to achieve.
Member 14629414 4-Mar-20 9:25am
   
I'm very sorry for replying late to you. But anyways it was a bad question as for my thought. Thank you for comment!
Stefan_Lang 4-Mar-20 10:10am
   
No problem.

It's not that this is a bad question, just that these kind of questions require much more time and explanations from your side. If your code doesn't already work for the most part, then there's no point posting any - to the contrary, it will just distract from the actual problem.

Most of the time in such cases it takes a lot less time to google for the answer you're seeking, because you have the knowledge needed to decide which answers are useful - whereas we have no idea if the suggestion we have in mind is of any help to you, unless you spend a lot of time explaning what you need, exactly.
Member 14629414 4-Mar-20 17:43pm
   
Stefan_Lang sure i'll take much more effort to explain my situation than just posting code. Anyways thank you for your advice!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100