int main(int argc, char* argv[]) { //if we would like to calibrate our filter values, set to true. bool calibrationMode = true; //Matrix to store each frame of the webcam feed Mat cameraFeed; Mat threshold; Mat HSV; if(calibrationMode) { //create slider bars for HSV filtering createTrackbars(); } //while(1) { src = imread("/home/yf/Pictures/tack2.png", 1); cameraFeed = src; if( !src.data ) { return -1; } //convert frame from BGR to HSV colorspace cvtColor(src,HSV,CV_BGR2HSV); //need to find the appropriate color range values // calibrationMode must be false //if in calibration mode, we track objects based on the HSV slider values. cvtColor(cameraFeed,HSV,CV_BGR2HSV); inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold); morphOps(threshold); imshow(windowName2,threshold); //the folowing for canny edge detec /// Create a matrix of the same type and size as src (for dst) dst.create( src.size(), src.type() ); /// Convert the image to grayscale cvtColor( src, src_gray, CV_BGR2GRAY ); /// Create a window namedWindow( window_name, CV_WINDOW_AUTOSIZE ); /// Create a Trackbar for user to enter threshold //createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold); /// Show the image trackFilteredObject(threshold,HSV,cameraFeed); //show frames //imshow(windowName2,threshold); imshow(windowName,cameraFeed); //imshow(windowName1,HSV); //delay 30ms so that screen can refresh. //image will not appear without this waitKey() command waitKey(); } return 0; }
void visual_servoing::processImage(const sensor_msgs::ImageConstPtr& msg) { // Convert the ROS image to an OpenCV Image cv_bridge::CvImagePtr cv_ptr; // try // { cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); // } // catch(cv_bridge::Exception& e) // { // ROS_ERROR("cv_bridge exception: %s", e.what()); // return; // } Mat camerafeed = cv_ptr->image; Mat threshold; Mat HSV; // visual_servoing the threshed image createTrackbars(); cvtColor(cv_ptr->image,HSV,COLOR_BGR2HSV); cvtColor(cv_ptr->image,HSV,COLOR_BGR2HSV); inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX), threshold); morphOps(threshold); imshow(windowName2,threshold); trackRobot(this->robot, threshold,HSV,cv_ptr->image); cv::setMouseCallback("Original Image", mouseEventCallback, &(this->clickedPoint)); //Robot's position over time int robotX = this->robot.getXPos(); int robotY = this->robot.getYPos(); int clickedX = (int)this->clickedPoint.x; int clickedY = (int)this->clickedPoint.y; // Draw target point if(clickedX != -1 && clickedY != -1) { cv::circle(cv_ptr->image, cv::Point(clickedX, clickedY), 5, cv::Scalar(255, 0, 0)); cv::putText(cv_ptr->image, intToString(clickedX)+ " , " + intToString(clickedY), cv::Point(clickedX, clickedY + 20), 1, 1,Scalar(255, 0, 0)); } // Calculate deltas int delta_x = robotX - clickedX; int delta_y = robotY - clickedY; imshow(windowName,cv_ptr->image); waitKey(3); }
int main(int argc, char* argv[]) { cuerpo humano; bool grabarmeneada = false; bool calibrationMode = false; Mat cameraFeed; Mat threshold; Mat HSV; cv::Size frameSize(FRAME_WIDTH, FRAME_HEIGHT); Mat figura= Mat::zeros(frameSize, CV_8UC1); vector<Mat> spl; if(calibrationMode){ createTrackbars(); } VideoCapture captura; captura.open(1); // cv::VideoWriter grabar; //figura = Mat::zeros(frameSize, CV_8UC1); if(grabarmeneada){ string filename = "meneate.avi"; //fcc int fcc = CV_FOURCC('D','I','V','3'); int fps = 10; grabar = VideoWriter(filename, fcc, fps, frameSize); if(!grabar.isOpened()){ cout<<"ERROR al grabar archivo"<<endl; getchar(); return -1; } } captura.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH); captura.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT); while(1){ captura.read(cameraFeed); cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); if(calibrationMode==true){ cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold); morphOps(threshold); imshow(windowName2,threshold); trackFilteredObject(threshold,HSV,cameraFeed,figura, humano); }else{ nodo marca; marca.setHSVmin(Scalar(24,2,240)); marca.setHSVmax(Scalar(85,115,256)); cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,marca.getHSVmin(),marca.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(threshold,HSV,cameraFeed,figura, humano); if(grabarmeneada){ split(cameraFeed, spl); for (int i =0; i < 3; ++i) spl[i] = figura; merge(spl, figura); grabar.write(figura); } } imshow(windowName2,threshold); imshow(windowName,cameraFeed); grabar.write(figura); imshow(meneate,figura); //imshow(windowName1,HSV); switch(waitKey(1)){ case 27://ESC return 0; } waitKey(10); } return 0; }
int main(int argc, char* argv[]) { //if we would like to calibrate our filter values, set to true. bool calibrationMode = true; //Matrix to store each frame of the webcam feed Mat cameraFeed; Mat threshold; Mat HSV; if(calibrationMode){ //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(1); //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 //all of our operations will be performed within this loop while(1){ //store image to matrix capture.read(cameraFeed); //convert frame from BGR to HSV colorspace cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); //cvtColor(cameraFeed,HSV,COLOR_BGR2YCrCb); if(calibrationMode==true){ //if in calibration mode, we track objects based on the HSV slider values. cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); //cvtColor(cameraFeed,HSV,COLOR_BGR2YCrCb); inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold); morphOps(threshold); imshow(windowName2,threshold); trackFilteredObject(threshold,HSV,cameraFeed); }else{ Sample BlueRock("BlueRock"), RedPuck("RedPuck"), OrangePipe("OrangePipe"), PinkBall("PinkBall"); cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); //cvtColor(cameraFeed,HSV,COLOR_BGR2YCrCb); inRange(HSV,BlueRock.getHSVmin(),BlueRock.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(BlueRock,threshold,HSV,cameraFeed); cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); //cvtColor(cameraFeed,HSV,COLOR_BGR2YCrCb); inRange(HSV,RedPuck.getHSVmin(),RedPuck.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(RedPuck,threshold,HSV,cameraFeed); cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); //cvtColor(cameraFeed,HSV,COLOR_BGR2YCrCb); inRange(HSV,OrangePipe.getHSVmin(),OrangePipe.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(OrangePipe,threshold,HSV,cameraFeed); cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); //cvtColor(cameraFeed,HSV,COLOR_BGR2YCrCb); inRange(HSV,PinkBall.getHSVmin(),PinkBall.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(PinkBall,threshold,HSV,cameraFeed); } //show frames //imshow(windowName2,threshold); imshow(windowName,cameraFeed); //imshow(windowName1,HSV); //delay 30ms so that screen can refresh. //image will not appear without this waitKey() command waitKey(30); } return 0; }
int main() { double time; CvScalar hsv_min; CvScalar hsv_max; #ifdef VIDEO CvCapture* capture = cvCreateFileCapture("E:\\Evan Lin\\CLUB\\PingPong\\OPENCV\\Table Tennis.flv"); //CvCapture *capture = cvCreateFileCapture("E:\\Evan Lin\\CLUB\\PingPong\\OPENCV\\Test\\motionTracking(ZC)\\bouncingBall.avi"); //CvCapture *capture = cvCreateFileCapture("E:\\Evan Lin\\CLUB\\PingPong\\OPENCV\\PingPongTest.avi"); //CvCapture *capture = cvCreateFileCapture("E:\\Evan Lin\\CLUB\\PingPong\\OPENCV\\WIN_20150803_103818.MP4");//WIN_20150803_102356.MP4 #else CvCapture* capture = cvCreateCameraCapture(0); #endif if (!capture) { printf("ERROR ACQUIRING VIDEO FEED\n"); getchar(); return -1; } CvSize size = cvSize(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT)); IplImage* frame = cvCreateImage(size, IPL_DEPTH_8U, 3); IplImage* hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); IplImage* thresholded = cvCreateImage(size, IPL_DEPTH_8U, 1); cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX, 0.5, 0.5, 0, 1, 1); while (1) { #ifdef VIDEO capture = cvCreateFileCapture("E:\\Evan Lin\\CLUB\\PingPong\\OPENCV\\Table Tennis.flv"); if (!capture) { printf("ERROR ACQUIRING VIDEO FEED\n"); getchar(); return -1; } while (cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES) < cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT) - 1) { time = (double)cvGetTickCount(); frame = cvQueryFrame(capture); if (!frame) { fprintf(stderr, "ERROR: frame is null...\n"); getchar(); exit(1); } cvCvtColor(frame, hsv_frame, CV_BGR2HSV); hsv_min = cvScalar(H_MIN, S_MIN, S_MIN, 0); hsv_max = cvScalar(H_MAX, S_MAX, V_MAX, 0); cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded); cvErode(thresholded, thresholded, NULL, 1); cvDilate(thresholded, thresholded, NULL, 1); //cvSmooth(thresholded, thresholded, CV_GAUSSIAN, 9, 9); if (debugMode == true) { createTrackbars(); cvNamedWindow("HSV", CV_WINDOW_AUTOSIZE); cvShowImage("HSV", thresholded); } else { cvDestroyWindow("trackbarControl"); cvDestroyWindow("HSV"); } if (trackingEnabled) { searchForMovement(thresholded, frame); } if (showEnabled) { time = ((double)cvGetTickCount() - time) / cvGetTickFrequency(); double fps = 100000.0 / time; char str[20]; sprintf(str, "FPS:%.2f", fps); cvPutText(frame, str, cvPoint(0, 30), &font, cvScalar(255, 0, 0)); } cvShowImage("Frame1", frame); switch (cvWaitKey(33)) { case 27: //'esc' key has been pressed, exit program. //cvReleaseMemStorage(); cvReleaseCapture(&capture); cvDestroyAllWindows(); return 0; case 116: //'t' has been pressed. this will toggle tracking trackingEnabled = !trackingEnabled; if (trackingEnabled == false) printf("Tracking disabled.\n"); else printf("Tracking enabled.\n"); break; case 100: //'d' has been pressed. this will debug mode debugMode = !debugMode; if (debugMode == false) printf("Debug mode disabled.\n"); else printf("Debug mode enabled.\n"); break; case 115: //'s' has been pressed. this will debug mode showEnabled = !showEnabled; if (showEnabled == false) printf("Show mode disabled.\n"); else printf("Show mode enabled.\n"); break; case 112: //'p' has been pressed. this will pause/resume the code. pause = !pause; if (pause == true) { printf("Code paused, press 'p' again to resume\n"); while (pause == true) { //stay in this loop until switch (cvWaitKey()) { case 112: //change pause back to false pause = false; printf("Code resumed.\n"); break; } } } } } } #else time = (double)GetTickCount(); //time = (double)cvGetTickCount(); frame = cvQueryFrame(capture); if (!frame) { fprintf(stderr, "ERROR: frame is null...\n"); getchar(); exit(1); } cvCvtColor(frame, hsv_frame, CV_BGR2HSV); hsv_min = cvScalar(H_MIN, S_MIN, S_MIN, 0); hsv_max = cvScalar(H_MAX, S_MAX, V_MAX, 0); cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded); cvErode(thresholded, thresholded, NULL, 1); cvErode(thresholded, thresholded, NULL, 1); cvErode(thresholded, thresholded, NULL, 1); cvDilate(thresholded, thresholded, NULL, 1); //cvSmooth(thresholded, thresholded, CV_GAUSSIAN, 9, 9); if (debugMode == true) { createTrackbars(); cvNamedWindow("HSV", CV_WINDOW_AUTOSIZE); cvShowImage("HSV", thresholded); } else { cvDestroyWindow("trackbarControl"); cvDestroyWindow("HSV"); } if (trackingEnabled) { searchForMovement(thresholded, frame); } if (showEnabled) { //time = ((double)cvGetTickCount() - time) / cvGetTickFrequency(); //double fps = 100000.0 / time; time = (double)GetTickCount() - time; double fps = 1000.0 / time; char str[20]; sprintf(str, "FPS:%.2f", fps); cvPutText(frame, str, cvPoint(0, 30), &font, cvScalar(255, 0, 0)); } cvShowImage("Frame1", frame); }
int main(int argc, char* argv[]) { //if we would like to calibrate our filter values, set to true. bool calibrationMode = true; //Matrix to store each frame of the webcam feed Mat cameraFeed; Mat threshold; Mat HSV; if(calibrationMode){ //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); //start an infinite loop where webcam feed is copied to cameraFeed matrix //all of our operations will be performed within this loop while(1){ //store image to matrix capture.read(cameraFeed); //convert frame from BGR to HSV colorspace cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); if(calibrationMode==true){ //if in calibration mode, we track objects based on the HSV slider values. cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold); morphOps(threshold); imshow(windowName2,threshold); trackFilteredObject(threshold,HSV,cameraFeed); }else{ //create some temp fruit objects so that //we can use their member functions/information Fruit apple("apple"), banana("banana"), cherry("cherry"); //first find apples cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,apple.getHSVmin(),apple.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(apple,threshold,HSV,cameraFeed); //then bananas cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,banana.getHSVmin(),banana.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(banana,threshold,HSV,cameraFeed); //then cherries cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,cherry.getHSVmin(),cherry.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(cherry,threshold,HSV,cameraFeed); } //show frames //imshow(windowName2,threshold); imshow(windowName,cameraFeed); //imshow(windowName1,HSV); //delay 30ms so that screen can refresh. //image will not appear without this waitKey() command waitKey(30); } return 0; }
int main(int argc, char* argv[]) { //if we would like to calibrate our filter values, set to true. bool calibrationMode = true; //Matrix to store each frame of the webcam feed Mat cameraFeed; Mat threshold; Mat HSV; if(calibrationMode){ //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); //start an infinite loop where webcam feed is copied to cameraFeed matrix //all of our operations will be performed within this loop waitKey(1000); while(1){ //store image to matrix capture.read(cameraFeed); src = cameraFeed; if( !src.data ) { return -1; } //convert frame from BGR to HSV colorspace cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); if(calibrationMode==true){ //need to find the appropriate color range values // calibrationMode must be false //if in calibration mode, we track objects based on the HSV slider values. cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold); morphOps(threshold); imshow(windowName2,threshold); //the folowing for canny edge detec /// Create a matrix of the same type and size as src (for dst) dst.create( src.size(), src.type() ); /// Convert the image to grayscale cvtColor( src, src_gray, CV_BGR2GRAY ); /// Create a window namedWindow( window_name, CV_WINDOW_AUTOSIZE ); /// Create a Trackbar for user to enter threshold createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold); /// Show the image trackFilteredObject(threshold,HSV,cameraFeed); } else{ //create some temp fruit objects so that //we can use their member functions/information Object blue("blue"), yellow("yellow"), red("red"), green("green"); //first find blue objects cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,blue.getHSVmin(),blue.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(blue,threshold,HSV,cameraFeed); //then yellows cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,yellow.getHSVmin(),yellow.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(yellow,threshold,HSV,cameraFeed); //then reds cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,red.getHSVmin(),red.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(red,threshold,HSV,cameraFeed); //then greens cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); inRange(HSV,green.getHSVmin(),green.getHSVmax(),threshold); morphOps(threshold); trackFilteredObject(green,threshold,HSV,cameraFeed); } //show frames //imshow(windowName2,threshold); imshow(windowName,cameraFeed); //imshow(windowName1,HSV); //delay 30ms so that screen can refresh. //image will not appear without this waitKey() command waitKey(30); } return 0; }