void imageCallback(const sensor_msgs::ImageConstPtr& msg) { //convert ROS image format to opencv image format cv_bridge::CvImageConstPtr cv_ptr; try { cv_ptr = cv_bridge::toCvShare(msg, sensor_msgs::image_encodings::BGR8); } catch (cv_bridge::Exception& e) { ROS_ERROR("cv_bridge exception: %s", e.what()); return; } cv::Mat img_hsv(cv_ptr->image.rows, cv_ptr->image.cols,CV_8UC3); cv::cvtColor(cv_ptr->image, img_hsv, CV_BGR2HSV); cv::Mat img_thresh = hsvThreshold(img_hsv, hsv); cv::imshow(window_name, img_thresh); int c = cv::waitKey(3); if( (c & 255) == 27 ) // ESC key pressed { ROS_INFO("Finished. HSV: %s .", hsv.toString().c_str()); ROS_INFO(" Copy this into the launch file:"); printf(" H: [ %i, %i]\n", hsv.H.min, hsv.H.max); printf(" S: [ %i, %i]\n", hsv.S.min, hsv.S.max); printf(" V: [ %i, %i]\n", hsv.V.min, hsv.V.max); ros::shutdown(); } }
void GripPipeline::Process(cv::Mat & source0) { cv::Mat cvResizeSrc = source0; cv::Size cvResizeDsize(0, 0); double cvResizeFx = 0.75; // default Double double cvResizeFy = 0.75; // default Double int cvResizeInterpolation = cv::INTER_LINEAR; cvResize(cvResizeSrc, cvResizeDsize, cvResizeFx, cvResizeFy, cvResizeInterpolation, this->cvResizeOutput); cv::Mat hsvThresholdInput = cvResizeOutput; double hsvThresholdHue[] = {69,180}; double hsvThresholdSaturation[] = {172,255}; double hsvThresholdValue[] = {112,255}; hsvThreshold(hsvThresholdInput, hsvThresholdHue, hsvThresholdSaturation, hsvThresholdValue, this->hsvThresholdOutput); cv::Mat findContoursInput = hsvThresholdOutput; source0= hsvThresholdOutput; std::vector<std::vector<cv::Point> > filterContoursContours = findContoursOutput; cv::Mat cvErodeSrc = hsvThresholdOutput; cv::Mat cvErodeKernel; cv::Point cvErodeAnchor(-1, -1); double cvErodeIterations = 1; int cvErodeBordertype = cv::BORDER_CONSTANT; cv::Scalar cvErodeBordervalue(-1); cvErode(cvErodeSrc, cvErodeKernel, cvErodeAnchor, cvErodeIterations, cvErodeBordertype, cvErodeBordervalue, this->cvErodeOutput); cv::Mat findLinesInput = cvErodeOutput; findLines(findLinesInput, this->findLinesOutput); // print the lines // GripPipeline::printLines(source0, findLinesOutput); // find center GripPipeline::findCenter(source0, findLinesOutput); // find distance GripPipeline::findDistance(source0, findLinesOutput, difference); }
/** * Runs an iteration of the Pipeline and updates outputs. * * Sources need to be set before calling this method. * */ void GripPipeline::process(cv::Mat source0){ //Step CV_resize0: //input cv::Mat cvResizeSrc = source0; cv::Size cvResizeDsize(0, 0); double cvResizeFx = 0.25; // default Double double cvResizeFy = 0.25; // default Double int cvResizeInterpolation = cv::INTER_LINEAR; cvResize(cvResizeSrc, cvResizeDsize, cvResizeFx, cvResizeFy, cvResizeInterpolation, this->cvResizeOutput); //Step HSV_Threshold0: //input cv::Mat hsvThresholdInput = cvResizeOutput; double hsvThresholdHue[] = {42.086330935251794, 86.7911714770798}; double hsvThresholdSaturation[] = {32.10431654676259, 207.37691001697794}; double hsvThresholdValue[] = {91.72661870503596, 255.0}; hsvThreshold(hsvThresholdInput, hsvThresholdHue, hsvThresholdSaturation, hsvThresholdValue, this->hsvThresholdOutput); //Step CV_erode0: //input cv::Mat cvErodeSrc = hsvThresholdOutput; cv::Mat cvErodeKernel; cv::Point cvErodeAnchor(-1, -1); double cvErodeIterations = 1; // default Double int cvErodeBordertype = cv::BORDER_CONSTANT; cv::Scalar cvErodeBordervalue(-1); cvErode(cvErodeSrc, cvErodeKernel, cvErodeAnchor, cvErodeIterations, cvErodeBordertype, cvErodeBordervalue, this->cvErodeOutput); //Step Mask0: //input cv::Mat maskInput = cvResizeOutput; cv::Mat maskMask = cvErodeOutput; mask(maskInput, maskMask, this->maskOutput); //Step Find_Blobs0: //input cv::Mat findBlobsInput = maskOutput; double findBlobsMinArea = 0.0; // default Double double findBlobsCircularity[] = {0.0, 1.0}; bool findBlobsDarkBlobs = true; // default Boolean findBlobs(findBlobsInput, findBlobsMinArea, findBlobsCircularity, findBlobsDarkBlobs, this->findBlobsOutput); }