Esempio n. 1
0
  // The processing loop where images are retrieved, tags detected,
  // and information about detections generated
  void loop() {

    cv::Mat image;
    cv::Mat image_gray;

    int frame = 0;
    double last_t = tic();
    while (true) {

      // capture frame
      Camera.grab();
      Camera.retrieve ( image);

      processImage(image, image_gray);

      // print out the frame rate at which image frames are being processed
      frame++;
      if (frame % 10 == 0) {
        double t = tic();
        cout << "  " << 10./(t-last_t) << " fps" << endl;
        last_t = t;
      }

      // exit if any key is pressed
      if (cv::waitKey(1) >= 0) break;
    }
  }
Esempio n. 2
0
int32_t FindLineCenter(raspicam::RaspiCam_Cv &Camera, int32_t nTick, int32_t &nCenterX, int32_t &nCenterY)
{
	cv::Mat image;

	char szFilename[100] = "";
	char szOriFilename[100];
#if PRESERVEROI
	char szOriROIFilename[100];
	cv::Mat roiImgPreserve;
#endif

    cv::Rect roi(0, 190, CAMERA_IMG_WIDTH, 100);
    cv::Mat roiImg, erodeElmt, dilateElmt;
    int thVal = IMGTHRESHOLD;
    vector<vector<cv::Point> > contours;
    vector<cv::Vec4i> hierarchy;
 
	int nRet = -1;
	nCenterX = CAMERA_IMG_WIDTH / 2;
	nCenterY = CAMERA_IMG_HEIGHT / 2;
	
    {
		try {
			Camera.grab();
			Camera.retrieve(image);
			
			image(roi).copyTo(roiImg);
			#if PRESERVEROI
				roiImg.copyTo(roiImgPreserve);
			#endif
		} catch (cv::Exception& e)
		{
			const char* err_msg = e.what();
			std::cout << "exception caught: " << err_msg << std::endl;
		}
		
#if THRESHOLDIMAGE
		//printf("threshold image\n");
        cv::threshold(roiImg, roiImg, thVal, 255, cv::THRESH_BINARY);
        //printf("bitwise_not image\n");
        cv::bitwise_not(roiImg, roiImg); // negative image
        
		erodeElmt = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
		dilateElmt = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
		//printf("erode image\n");
		cv::erode(roiImg, roiImg, erodeElmt);
		//printf("dilate image\n");
		cv::dilate(roiImg, roiImg, dilateElmt); 
#endif 		
#if TESTROI		
		{
			sprintf(szFilename, "roi_image%d.png", nTick);
			sprintf(szOriFilename, "image%d.png", nTick);
			try {
				//cv::drawContours(roiImg, contours, s, cv::Scalar(255, 255, 255), 2, 8, hierarchy, 0, cv::Point() );
				cv::imwrite(szFilename, roiImg);
				cv::imwrite(szOriFilename, image);
			} catch (cv::Exception& e)
			{
				const char* err_msg = e.what();
				std::cout << "exception caught: " << err_msg << std::endl;
			}
		}	
#endif	
#if !TESTROI
		//printf("findContours image\n");
		cv::findContours(roiImg, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0,0));
		int nMaxAreaContourIndex = -1;
		float fMaxArea = 0.f;
		
		for (size_t s = 0; s < contours.size(); s++) 
		{
			float fArea = cv::contourArea(contours[s]);
			if (fArea > fMaxArea)
			{
				nMaxAreaContourIndex = s;
				fMaxArea = fArea;
			}
		}
		{
			if (fMaxArea > MAXAREATHRESHOLD && nMaxAreaContourIndex >= 0) 
			{
				cv::Moments mu;
				mu = cv::moments(contours[nMaxAreaContourIndex], false);
				cv::Point2f center(mu.m10 / mu.m00, mu.m01 / mu.m00); // point in center (x only)   
				printf("Find a region: Area=%f, center(%f, %f)\n", fMaxArea, center.x, center.y);
				
				nCenterX = (int32_t)center.x;
				nCenterY = (int32_t)center.y;
				nRet = 1;
				// here is the center pixel
#if 0
				{
					try {
						cv::circle(roiImg, center, 5, cv::Scalar(255, 255, 255), -1, 8, 0);
					} catch (cv::Exception& e)
					{
						const char* err_msg = e.what();
						std::cout << "exception caught: " << err_msg << std::endl;
					}
					sprintf(szFilename, "roi_image%d.png", nTick);
					sprintf(szOriFilename, "image%d.png", nTick);
					try {
						cv::drawContours(roiImg, contours, s, cv::Scalar(255, 255, 255), 2, 8, hierarchy, 0, cv::Point());
						cv::imwrite(szFilename, roiImg);
						//cv::imwrite(szOriFilename, image);
#if PRESERVEROI
						sprintf(szOriROIFilename, "ori_roi_image%d.png", nTick);
						cv::imwrite(szOriROIFilename, roiImgPreserve);
#endif						
					} catch (cv::Exception& e)
					{
						const char* err_msg = e.what();
						std::cout << "exception caught: " << err_msg << std::endl;
					}
				}
#endif				
			}
		}
#endif
    }
    
    return nRet;
}