Как создать поток для этой программы отслеживания объектов?
У меня есть программа отслеживания объектов, как показано ниже.Проблема в том, что я хочу сделать его в основную программу в виде потока, чтобы запустить как основную программу, так и эту программу параллельно:
#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; }
Что я уже пробовал:
Я уже вставил эту роль int main(int argc, char* argv[]) в основную программу и создать поток по лямбда-выражению вроде этого, но программа вообще не запустилась(может быть, она была тяжелой и не могла загрузиться)
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); }
Спасибо!
Stefan_Lang
Я могу многое понять из вашего кода, и то, что вы говорите, что вы сделали или хотите, не кажется мне более разумным. Существует множество статей и советов на тему параллельного выполнения кода. Если это не помогает, вы должны потратить немного больше усилий, объясняя, чего вы на самом деле хотите достичь, и, что более важно, что происходит не так. Мы не собираемся тратить часы на анализ вашего кода и гадать, чего вы, возможно, намеревались достичь.
Member 14629414
Мне очень жаль, что я так поздно вам ответил. Но в любом случае это был плохой вопрос, Что касается моей мысли. Спасибо за комментарий!
Stefan_Lang
Без проблем.
Дело не в том, что это плохой вопрос, просто такие вопросы требуют гораздо больше времени и объяснений с вашей стороны. Если ваш код уже не работает по большей части, то нет никакого смысла публиковать его - наоборот, это просто отвлечет от реальной проблемы.
В большинстве случаев в таких случаях требуется гораздо меньше времени, чтобы найти в google ответ, который вы ищете, потому что у вас есть знания, необходимые, чтобы решить, какие ответы полезны, - в то время как мы понятия не имеем, поможет ли вам предложение, которое мы имеем в виду, если вы не потратите много времени на объяснение того, что именно вам нужно.
Member 14629414
Stefan_Lang уверен, что я приложу гораздо больше усилий, чтобы объяснить свою ситуацию, чем просто разместить код. В любом случае спасибо вам за ваш совет!