BOOL CDlg_AppInfo::OnInitDialog() 
{
	CDialog::OnInitDialog();
	Sizer.InitSizer(this,objSettings.iStickPix,HKEY_CURRENT_USER,PROG_REGKEY"\\Wnd_AppInfo");
	Sizer.SetOptions(STICKABLE|SIZEABLE);
	CRect minRect(0,0,260,100);
	Sizer.SetMinRect(minRect);
	Sizer.AddDialogElement(IDOK,widthSize|heightSize);
	Sizer.AddDialogElement(IDC_EDIT,0);
	Sizer.AddDialogElement(ID_OPEN,widthSize|heightSize);
	Sizer.AddDialogElement(ID_WNDS,widthSize|heightSize|hidable);
	Sizer.AddLeftTopAlign(IDC_EDIT,0,4);
	Sizer.AddRightTopAlign(IDC_EDIT,0,4);
	Sizer.AddRightBottomAlign(IDOK,0,-4);
	Sizer.AddBottomAlign(ID_OPEN,0,-4);
	Sizer.AddBottomAlign(ID_WNDS,0,-4);
	Sizer.AddRightAlign(ID_OPEN,IDOK,leftPos,-4);
	Sizer.AddRightAlign(ID_WNDS,ID_OPEN,leftPos,-4);
	Sizer.AddBottomAlign(IDC_EDIT,IDOK,topPos,-4);
	SetWindowText(_l("Application info"));
	GetDlgItem(ID_OPEN)->SetWindowText(_l("Application folder"));
	GetDlgItem(ID_WNDS)->SetWindowText(_l("Actions")+"...");
	SetIcon(theApp.MainImageList.ExtractIcon(13),TRUE);
	SetIcon(theApp.MainImageList.ExtractIcon(13),FALSE);
	SetTaskbarButton(this->GetSafeHwnd());	
	SwitchToWindow(this->GetSafeHwnd());
	return TRUE;
}
void BallDetector::contourDetection()
{
    Canny(thrs,cannyImg, 50, 150, 3);
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(cannyImg, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0,0));
    vector<RotatedRect> minRect(contours.size());
    vector<Point2f> center(contours.size());
    vector<float> radius(contours.size());
    for( size_t i = 0; i < contours.size(); i++)
    {
        if(contours[i].size() > 5)
            minRect[i] = fitEllipse(Mat(contours[i]));
        minEnclosingCircle(contours[i],center[i],radius[i]);
    }
    
    RNG rng(12345);
    Mat drawing = Mat::zeros(cannyImg.size(),CV_8UC3);
    for( size_t i = 0; i < contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255));
        Size2f s = minRect[i].size;
        Point2f c = minRect[i].center;
        if(contours[i].size() > 5 && s.width/s.height > 0.7 && s.width/s.height < 1.4 && s.width > 5 && s.height > 5)
            // && arcLength(contours[i],true)/(2*CV_PI*radius[i])>0.8 && arcLength(contours[i],true)/(2*CV_PI*radius[i]) < 1.2 )
        {
            drawContours(drawing, contours, (int)i, color, -1, 8, hierarchy, 0, Point());
            cout<<c.x<<" "<<c.y<<endl;
            cout<<s.width<<" "<<s.height<<endl<<endl;
        }
    }
    imshow("contours",drawing);
    imwrite("images/contours.png",drawing);
}
Beispiel #3
0
Mat IPS_simple::applyContourns(Mat src){
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    RNG rng(12345);
    Mat src2 = src.clone();
    Mat dst = src.clone();
    Mat src_gray = src.clone();
    Mat final = imread(Constants::IMG_RAW);

    cvtColor( src, src_gray, CV_BGR2GRAY );
    // Eliminar ruido
    src2 = applyBlur(src_gray);
    findContours( src2, contours, hierarchy,CV_CHAIN_APPROX_SIMPLE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    qDebug() << contours.data();
    // Rotar los rectangulos buscando el area minima
    vector<RotatedRect> minRect( contours.size() );
    for( int i = 0; i < contours.size(); i++ ){
         minRect[i] = minAreaRect( Mat(contours[i]) );
     }

    //  Dibujar rectangulos
     Scalar color = Scalar( rng.uniform(0,0), rng.uniform(250,250), rng.uniform(0,0) );
     for( int i = 0; i< contours.size(); i++ ){
         Point2f rect_points[4]; minRect[i].points( rect_points );

         if( pow((int)(rect_points[0].x-(rect_points[1]).x), 2.0) + pow((int)(rect_points[0].y-(rect_points[1]).y), 2.0) > pow(Constants::CONTOURS_MIN_CONTOURS, 2.0) &&
             pow((int)(rect_points[1].x-(rect_points[2]).x), 2.0) + pow((int)(rect_points[1].y-(rect_points[2]).y), 2.0) > pow(Constants::CONTOURS_MIN_CONTOURS, 2.0))
             {
            for(int j = 0; j < 4; j++ ){
                line( dst, rect_points[j], rect_points[(j+1)%4], color, 2, 8 );
                line( final, rect_points[j], rect_points[(j+1)%4], color, 2, 8 );
            }
         }
     }
     imwrite(Constants::IMG_FINAL,final);
    return dst;
}
/*****************************************************************************************
* Method Name: FindGate
* Description: The function search for a couple of white vertical rectangles, whose buttom points
* 				are on green && upper part is not on green.
* 				This rectangles are marked as gate posts.
* 				If only one post is present in the frame, the function tries to find the direction
* 				of the second vertical post, using the upper post direction.
* Arguments: BWImage, Output - DetectedGate
* Return Values: DetectedGate object
* Owner: Hen Shoob, Assaf Rabinowitz
*****************************************************************************************/
DetectedObject* GateDetector::FindGate(Mat& inputImage)
{
	Mat onlyWhiteImage;
	GetWhiteImage(inputImage, onlyWhiteImage);

	Mat whiteVertical = onlyWhiteImage.clone();
	GetVerticalObjects(whiteVertical);

	//creating Mat thr with contours, using canny
	Mat src;
	cvtColor(whiteVertical, src, CV_GRAY2BGR);
	Mat canny;
	Canny(whiteVertical, canny, 50, 190, 3, false);
	ImageShowOnDebug("canny", canny);

	//crating contours vector
	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;

	findContours(canny, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

	cvtColor(canny, canny, CV_GRAY2BGR);
	//finding the blocking rectangles of the contours
	vector<RotatedRect> minRect(contours.size());
	for (size_t i = 0; i < contours.size(); i++)
	{
		minRect[i] = minAreaRect(Mat(contours[i]));
		DrawRectangle(canny, minRect[i]);
	}
	ImageShowOnDebug("contours", canny);

	Mat field;
	FindField(inputImage, field);

	//Showing the contours + field in one image
	cvtColor(canny, canny, CV_BGR2GRAY);
//	Mat or;
//	bitwise_or(field, canny, or);
//	ImageShowOnDebug("convex", or);

	vector<RotatedRect> post1Candidates;
	FindPostCandidates(field, minRect, post1Candidates);

	RotatedRect post1 = FindLargestCandidate(post1Candidates);
	DrawRectangle(src, post1);
	ImageShowOnDebug("post1", src);

	vector<RotatedRect> post2Candidates;
	for (size_t i = 0; i < post1Candidates.size(); i++)
	{
		if (post1Candidates[i].center.x != post1.center.x)
		{
			post2Candidates.push_back(post1Candidates[i]);
		}
	}

	RotatedRect post2 = FindLargestCandidate(post2Candidates);
	DrawRectangle(src, post2);
	ImageShowOnDebug("post2", src);

	//////////////////////////////////////////////////////////////////////////////////////////

	if (post1.size.area() > 0 && post2.size.area() > 0)
	{
		return new DetectedGate(post1, post2);
	}

	if (post1.size.area() == 0 && post2.size.area() == 0)
	{
		return new DetectedGate();
	}

	// One post - detect which.

	float longSide = (post1.size.height > post1.size.width) ? post1.size.height : post1.size.width;
	float shortSide = (post1.size.height < post1.size.width) ? post1.size.height : post1.size.width;

	Mat whiteHorizontal = onlyWhiteImage.clone();
	GetHorizontalObjects(whiteHorizontal);
	ImageShowOnDebug("whiteHorizontal", whiteHorizontal);

	//counting white pixels near the top of the post on both sides
	// We set the range to look for white pixels in the upper post.
	float upperPostTop = MAX(post1.center.y - longSide / 2 - 10, 0);
	float upperPostButtom = upperPostTop + shortSide + 5;	

	//Counting near the left post
	float upperPostLeft = MAX(post1.center.x - shortSide * 3, 0);
	float upperPostRight = MAX(post1.center.x - shortSide, 0);
	unsigned int leftCounter = CountAndDrawWhitePixels(whiteHorizontal, src, upperPostTop, upperPostButtom, upperPostLeft, upperPostRight);

	//finding right check rectangle, left and right side
	upperPostRight = MIN(post1.center.x + shortSide * 3, FRAME_WIDTH - 1);
	upperPostLeft = MIN(post1.center.x + shortSide, FRAME_WIDTH - 1);
	unsigned int rightCounter = CountAndDrawWhitePixels(whiteHorizontal, src, upperPostTop, upperPostButtom, upperPostLeft, upperPostRight);
	
	if (rightCounter > leftCounter  &&  rightCounter - leftCounter > 5)
	{
		return new DetectedGate(post1, E_DetectedPost_LEFT);
	}
	else if (rightCounter < leftCounter  &&  leftCounter - rightCounter > 5)
	{
		return new DetectedGate(post1, E_DetectedPost_RIGHT);
	}
	else
	{
		PrintStringOnImage(src, "Move camera up");
		return new DetectedGate();
	}	
}
  void do_work(const sensor_msgs::ImageConstPtr& msg, const std::string input_frame_from_msg)
  {
    // Work on the image.
    try
    {
      // Convert the image into something opencv can handle.
      cv::Mat frame = cv_bridge::toCvShare(msg, msg->encoding)->image;

      // Messages
      opencv_apps::RotatedRectArrayStamped rects_msg, ellipses_msg;
      rects_msg.header = msg->header;
      ellipses_msg.header = msg->header;

      // Do the work
      cv::Mat src_gray;

      /// Convert image to gray and blur it
      if ( frame.channels() > 1 ) {
        cv::cvtColor( frame, src_gray, cv::COLOR_RGB2GRAY );
      } else {
        src_gray = frame;
      }
      cv::blur( src_gray, src_gray, cv::Size(3,3) );

      /// Create window
      if( debug_view_) {
        cv::namedWindow( window_name_, cv::WINDOW_AUTOSIZE );
      }

      cv::Mat threshold_output;
      int max_thresh = 255;
      std::vector<std::vector<cv::Point> > contours;
      std::vector<cv::Vec4i> hierarchy;
      cv::RNG rng(12345);

      /// Detect edges using Threshold
      cv::threshold( src_gray, threshold_output, threshold_, 255, cv::THRESH_BINARY );

      /// Find contours
      cv::findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );

      /// Find the rotated rectangles and ellipses for each contour
      std::vector<cv::RotatedRect> minRect( contours.size() );
      std::vector<cv::RotatedRect> minEllipse( contours.size() );

      for( size_t i = 0; i < contours.size(); i++ )
      { minRect[i] = cv::minAreaRect( cv::Mat(contours[i]) );
        if( contours[i].size() > 5 )
        { minEllipse[i] = cv::fitEllipse( cv::Mat(contours[i]) ); }
      }

      /// Draw contours + rotated rects + ellipses
      cv::Mat drawing = cv::Mat::zeros( threshold_output.size(), CV_8UC3 );
      for( size_t i = 0; i< contours.size(); i++ )
      {
        cv::Scalar color = cv::Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
        // contour
        cv::drawContours( drawing, contours, (int)i, color, 1, 8, std::vector<cv::Vec4i>(), 0, cv::Point() );
        // ellipse
        cv::ellipse( drawing, minEllipse[i], color, 2, 8 );
        // rotated rectangle
        cv::Point2f rect_points[4]; minRect[i].points( rect_points );
        for( int j = 0; j < 4; j++ )
          cv::line( drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );

        opencv_apps::RotatedRect rect_msg;
        opencv_apps::Point2D rect_center;
        opencv_apps::Size rect_size;
        rect_center.x = minRect[i].center.x;
        rect_center.y = minRect[i].center.y;
        rect_size.width = minRect[i].size.width;
        rect_size.height = minRect[i].size.height;
        rect_msg.center = rect_center;
        rect_msg.size = rect_size;
        rect_msg.angle = minRect[i].angle;
        opencv_apps::RotatedRect ellipse_msg;
        opencv_apps::Point2D ellipse_center;
        opencv_apps::Size ellipse_size;
        ellipse_center.x = minEllipse[i].center.x;
        ellipse_center.y = minEllipse[i].center.y;
        ellipse_size.width = minEllipse[i].size.width;
        ellipse_size.height = minEllipse[i].size.height;
        ellipse_msg.center = ellipse_center;
        ellipse_msg.size = ellipse_size;
        ellipse_msg.angle = minEllipse[i].angle;

        rects_msg.rects.push_back(rect_msg);
        ellipses_msg.rects.push_back(ellipse_msg);
      }

      /// Create a Trackbar for user to enter threshold
      if( debug_view_) {
        if (need_config_update_) {
          config_.threshold = threshold_;
          srv.updateConfig(config_);
          need_config_update_ = false;
        }
        cv::createTrackbar( "Threshold:", window_name_, &threshold_, max_thresh, trackbarCallback);

        cv::imshow( window_name_, drawing );
        int c = cv::waitKey(1);
      }

      // Publish the image.
      sensor_msgs::Image::Ptr out_img = cv_bridge::CvImage(msg->header, sensor_msgs::image_encodings::BGR8, drawing).toImageMsg();
      img_pub_.publish(out_img);
      rects_pub_.publish(rects_msg);
      ellipses_pub_.publish(ellipses_msg);
    }
    catch (cv::Exception &e)
    {
      NODELET_ERROR("Image processing error: %s %s %s %i", e.err.c_str(), e.func.c_str(), e.file.c_str(), e.line);
    }

    prev_stamp_ = msg->header.stamp;
  }
  void imageCb(const sensor_msgs::ImageConstPtr& msg)
  {
    cv_bridge::CvImagePtr cv_ptr;
    try
      {
	cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
      }
    catch (cv_bridge::Exception& e)
      {
	ROS_ERROR("cv_bridge exception: %s", e.what());
	return;
      }

    /* Add any OpenCV processing here */
    /* Gray scale image */
    cv::Mat filtered_image;
    cv::cvtColor(cv_ptr->image,filtered_image,CV_BGR2GRAY);
    
    // Image Filtering 
    cv::Mat element5(5,5,CV_8U,cv::Scalar(1));
    cv::morphologyEx(filtered_image,filtered_image,cv::MORPH_CLOSE,element5);
    cv::morphologyEx(filtered_image,filtered_image,cv::MORPH_OPEN,element5);

    cv::blur(filtered_image,filtered_image,cv::Size(3,3));    

    // Show final filtered image 
    cv::namedWindow("Filtered Image");
    cv::imshow("Filtered Image",filtered_image);

    // Finding Contours 
    cv::Mat contoured_image;
    cv::morphologyEx(filtered_image,contoured_image,cv::MORPH_GRADIENT,cv::Mat());
    cv::namedWindow("MORPH_GRADIENT contours");
    cv::imshow("MORPH_GRADIENT contours",contoured_image);

    // threshold image 
    cv::Mat threshold_image;
    cv::threshold(contoured_image,threshold_image,global_min_threshold,255,cv::THRESH_BINARY);
    cv::namedWindow("Threshold Image");
    cv::imshow("Threshold Image",threshold_image);
    cv::createTrackbar("Min Threshold:","Threshold Image",&global_min_threshold,255,update_global_min_threshold);
    update_global_min_threshold(global_min_threshold,0);
    
    // Make bounding boxes:
    //// detect edges using threshold image
    std::vector<std::vector<cv::Point> > contours;
    std::vector<cv::Vec4i> hierarchy;

    cv::findContours(threshold_image,contours,hierarchy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE);

    //// find the rotated rectangles
    std::vector<cv::RotatedRect> minRect(contours.size());
    cv::RotatedRect new_rectangle;
    
    double length_side_a, length_side_b;
    cv::Point2f new_rect_points[4];
    

    for (int i=0; i<contours.size(); i++) {
      // only keep track of rectangles that might be cubelets... needs work
      
      new_rectangle=cv::minAreaRect(cv::Mat(contours[i]));
      new_rectangle.points(new_rect_points);
      
      std::cout<<"INFO:\t point.x = "<<new_rect_points[0].x<<std::endl;
      length_side_a=pow(new_rect_points[0].x-new_rect_points[1].x,2)+
        pow(new_rect_points[0].y-new_rect_points[1].y,2);
      length_side_b=pow(new_rect_points[0].x-new_rect_points[3].x,2)+
        pow(new_rect_points[0].y-new_rect_points[3].y,2);
      
      std::cout<<"INFO:\tsize, l_a/l_b = "<<new_rectangle.size.area()<<" ,"<<length_side_a/length_side_b<<std::endl;
      if (new_rectangle.size.area()>=500 && // minimum size requirement
          length_side_a/length_side_b>(100.0-global_squareness_ratio)/100.0 && // minimum squareness
          length_side_a/length_side_b<(100.0+global_squareness_ratio)/100.0) {
        minRect[i]=new_rectangle;
      }
    }
    
    //// Draw contours & rectangles
    cv::Mat contour_output(threshold_image.size(),CV_8U,cv::Scalar(255));
    cv::Mat bounding_boxes=cv::Mat::zeros(contour_output.size(),CV_8UC3);;
    int i=0;
    for (; i >=0; i=hierarchy[i][0]) {
      cv::Point2f rect_points[4];
      minRect[i].points(rect_points);
      cv::drawContours(bounding_boxes,contours,i,cv::Scalar(255,255,255),1,8,hierarchy,0,cv::Point());
      for (int j=0; j<4; j++) {
        cv::line(bounding_boxes,rect_points[j],rect_points[(j+1)%4],cv::Scalar(255),2,8);
      }
    }
    cv::namedWindow("bounding boxes");
    cv::imshow("bounding boxes",bounding_boxes);
    cv::createTrackbar("Out-of-square acceptance: ","bounding boxes",&global_squareness_ratio,100,update_global_squareness_ratio);

   // end of processing
    cv::imshow(WINDOW, cv_ptr->image);
    cv::waitKey(3);
    
    image_pub_.publish(cv_ptr->toImageMsg());
  }