int main( int argc, char** argv )
{
  const string filename = argc > 1 ? argv[1] : "../image/white_flower.png";
  Mat image = imread(filename, 1);
  if(image.empty())
    return 1;

  cout << "First, select the rectangular area\n" <<
    "Hot keys: \n"
    "\tESC - quit the program\n"
    "\tr - restore the original image\n"
    "\tn - next iteration\n"
    "\n"
    "\tleft mouse button - set rectangle\n"
    "\n"
    "\tCTRL+left mouse button - set GC_BGD pixels\n"
    "\tSHIFT+left mouse button - set CG_FGD pixels\n"
    "\n"
    "\tCTRL+right mouse button - set GC_PR_BGD pixels\n"
    "\tSHIFT+right mouse button - set CG_PR_FGD pixels\n";
  
  const string winName = "image";
  namedWindow(winName.c_str(), CV_WINDOW_AUTOSIZE);
  cvSetMouseCallback(winName.c_str(), on_mouse, 0);

  gcapp.setImageAndWinName(image, winName);
  gcapp.showImage();

  while(1) {
    int c = waitKey(0);
    switch((char)c) {

    // () exit
    case '\x1b':
      return 0;

    // () reset
    case 'r':
      cout << endl;
      gcapp.reset();
      gcapp.showImage();
      break;

     // () iteration
    case 'n':
      int iterCount = gcapp.getIterCount();
      cout << "<" << iterCount << "... ";
      int newIterCount = gcapp.nextIter();
      if( newIterCount > iterCount ) {
        gcapp.showImage();
        cout << iterCount << ">" << endl;
      }
      else
        cout << "rect must be determined>" << endl;
      break;
    }
  }

  return 0;
}
Esempio n. 2
0
int main()
{

	string filename = "C:\\workspace\\test.jpg";
	Mat image = imread( filename, 1 );
	if( image.empty() )
	{
		cout << "\n , couldn't read image filename " << filename << endl;
		return 1;
	}

	help();

	const string winName = "image";
	cvNamedWindow( winName.c_str(), CV_WINDOW_AUTOSIZE );
	cvSetMouseCallback( winName.c_str(), on_mouse, 0 );

	gcapp.setImageAndWinName( image, winName );
	gcapp.showImage();

	for(;;)
	{
		int c = cvWaitKey(0);
		switch( (char) c )
		{
		case '\x1b':
			cout << "Exiting ..." << endl;
			goto exit_main;
		case 'r':
			cout << endl;
			gcapp.reset();
			gcapp.showImage();
			break;
		case 'n':
			int iterCount = gcapp.getIterCount();
			cout << "<" << iterCount << "... ";
			int newIterCount = gcapp.nextIter();
			if( newIterCount > iterCount )
			{
				gcapp.showImage();
				cout << iterCount << ">" << endl;
			}
			else
				cout << "rect must be determined>" << endl;
			break;
		}
	}

exit_main:
	cvDestroyWindow( winName.c_str() );
	return 0;

	return 0;
}
void on_mouse( int event, int x, int y, int flags, void* param )
{
  gcapp.mouseClick( event, x, y, flags, param );
}
Esempio n. 4
0
int main (int argc, char **argv)
{
   	if (argc != 2)
    {
        printf ("Wrong parameters\n");
		return -1;
	}

    VideoCapture capture (argv [1]);
    // Class for video capturing from video files or cameras

	if (!capture.isOpened ())
    {
		printf ("Can't load %s image file\n", argv [1]);
		return -1;
	}

	double rate = capture.get( CV_CAP_PROP_FPS );
    printf("rate is %d\n", rate);
    // get the frame rate

	bool stop (false);
	Mat frame, gray, grayPrev, prevFrame;
	namedWindow("Extracted Frame");
    // the name of the window
	int delay = 1000 / 200;

    setMouseCallback ("Extracted Frame", VideoOnMouse);
    // callback when click the mouse

    capture.read (prevFrame);
    // Grabs, decodes and returns the next video frame.

    cvtColor (prevFrame, grayPrev, CV_RGB2GRAY);
    // 这里是从RGB转到灰度图
    // The function converts an input image from one color space to another
    
    // 一直循环
    while (!isStartSet)
    {
        imshow ("Extracted Frame", prevFrame);
        waitKey (30);
        //  waitKey waits for a key event infinitely (when \texttt{delay}\leq 0 ) or for delay milliseconds
        // 等到按了键才开始下一个循环(while(1){...})
    }

    while (1)
    {
        // reinitialize
        keypoints.clear();
        pointsStart.clear();
        pointsFinish.clear();
        vectorSize = 0;
        
        capture.read (prevFrame);

        namedWindow( "grabcut", WINDOW_AUTOSIZE );
        setMouseCallback ("grabcut", on_mouse);
        // 就这段时间是on_mouse
        gcapp.setImageAndWinName( prevFrame, "grabcut" );
        gcapp.showImage();
        
        Mat result;
        // used to store cutted result
        
        for(;;)
        {
            int c = waitKey(0);
            switch( (char) c )
            {
            case 'q':
                destroyWindow("grabcut");
                destroyWindow("Extracted Frame");
                return 0;
            case 'r':
                cout << endl;
                gcapp.reset();
                gcapp.showImage();
                break;
            case 'n':
                int iterCount = gcapp.getIterCount();
                cout << "<" << iterCount << "... ";
                int newIterCount = gcapp.nextIter();
                if( newIterCount > iterCount )
                {
                    printf("newIterCount > iterCount\n");
                    result = gcapp.showImage2();
                    cout << iterCount << ">" << endl;
                    // 等5秒,然后继续
                    waitKey(50);
                    destroyWindow("grabcut");
                    printf("after destroying grabcut window\n");
                    break;
                }
                else
                    cout << "rect must be determined>" << endl;
                break;
            }
            break;
        }
        // 1、把prevFrame用另外一张窗口显示出来
        // 2、在imshow上面画图(mask)
        
        keypoints = SURFDectect(result);
        // 3、处理得到另外一个cv::Mat
        // 4、将新得到的cv::Mat输入下面的prevFrame
        int totalKeypoints = keypoints.size();
        int originalTotalKeypoints = totalKeypoints;
        initializePoints(keypoints, pointsStart, pointsFinish);
        isStartSet=1;
        printf("after initializing\n");


    	while ( !stop )
        {   
            vector <Mat> output1;
            vector <Mat> output2;

    		if (!capture.read (frame )) break;

            cvtColor (frame, gray, CV_RGB2GRAY);
            // Mat --> frame, gray, grayPrev, prevFrame;

            BuildPyramid (grayPrev, output1, 4);
            BuildPyramid (gray, output2, 4);

            int minX=100000;int minY=100000;
            int maxX=-100000;int maxY=-100000;
            int tempX = 0;
            int tempY = 0;
            for (int i = 0; i < vectorSize; ++i)
            {
                // Mat::Mat(int rows, int cols, int type, const Scalar& s)
                Mat start  (2, 1, CV_32F, Scalar (0));
                Mat finish (2, 1, CV_32F, Scalar (0));

                start = pointsStart[i];
                finish = pointsFinish[i];

                LucasKanade (output1, output2, start, finish, totalKeypoints);
                
                tempX = (int) finish.at <float> (1, 0);
                tempY = (int) finish.at <float> (0, 0);
                if (tempX < minX) minX = tempX;
                if (tempX > maxX) maxX = tempX;
                if (tempY < minY) minY = tempY;
                if (tempY > maxY) maxY = tempY;
                // circle (frame, Point ((int) finish.at <float> (1, 0), (int) finish.at <float> (0, 0)), 7, Scalar (0, 255, 0));
                // void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
                // void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

                pointsFinish[i].copyTo(pointsStart[i]);
                // finish换到start
            }
            rectangle(frame, Point(minX, minY), Point(maxX, maxY), Scalar (0, 255, 0));

            // 画一个圈圈记录之后的位置
            imshow ("Extracted Frame", frame);

            gray.copyTo (grayPrev);
            // gray换到grayPrev
            
            if (totalKeypoints <= originalTotalKeypoints/2)
            {
                printf("re-recognising keypoints\n");
                isStartSet=0;
                break;
            }

    		if (waitKey (delay) >= 0) stop = true;
    	}//end of while ( !stop )

    }
	
    capture.release();

    waitKey ();

    return 0;
}
Esempio n. 5
0
static void on_mouse( int event, int x, int y, int flags, void* param )
{
    printf("on_mouse responding\n");
    gcapp.mouseClick( event, x, y, flags, param );
}
Esempio n. 6
0
void CDialog_init::OnBnClickedButton3()  //X-ray dicom image pre-processing (dicom to contour)
{
	typedef itk::Image<unsigned short,2> DICOMImageType; //here is unsigned short based on read X-Ray DICOM. 
	typedef itk::Image<unsigned char,2>OpenCVType;
	typedef itk::ImageFileReader<DICOMImageType> DICOMReaderType;
    typedef itk::GDCMImageIO  ImageIOType;
	typedef itk::CastImageFilter<DICOMImageType,OpenCVType> castFilterType;
	typedef itk::RescaleIntensityImageFilter<DICOMImageType,DICOMImageType> RescaleType;

	DICOMReaderType::Pointer DCMreader = DICOMReaderType::New();
    ImageIOType::Pointer dicomIO = ImageIOType::New();
	castFilterType::Pointer castfilter=castFilterType::New();
	DICOMImageType::Pointer DicomImage=DICOMImageType::New();
	OpenCVType::Pointer OpenCVImage=OpenCVType::New();
	RescaleType::Pointer rescaler=RescaleType::New();

	TCHAR dialog_filt[]=_T("Dicom File(*.dcm)|*.dcm||");
	CFileDialog dialog(TRUE,_T("Dicom"), _T("*.dcm"),OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, dialog_filt, this);
	CString pathname;
	CString filename;

	DCMreader->SetImageIO( dicomIO );

			if(dialog.DoModal()==IDOK)
			{
				pathname=dialog.GetPathName();
				CT2CA ConvertedString(pathname);
				DICOMfileName=ConvertedString;
				DICOMfileName2=ConvertedString;
			}
	
			if(!g_prox_or_dist)
					DCMreader->SetFileName(DICOMfileName.c_str()); // user define a X-Ray file name
			else
				DCMreader->SetFileName(DICOMfileName2.c_str());			

			//AllocConsole();
			//SetConsoleTitle(_T("debug output"));
			//freopen("CONOUT$","w",stdout);
			
			rescaler->SetInput(DCMreader->GetOutput());
			rescaler->SetOutputMinimum(0);
			rescaler->SetOutputMaximum(255);
			
			rescaler->Update();
	
			castfilter->SetInput(rescaler->GetOutput());
			castfilter->Update();
			OpenCVImage=castfilter->GetOutput();

			Mat femure=cv::Mat::zeros(cv::Size(XRAY_WIDTH,XRAY_HEIGHT),CV_8UC3);
			try
			{
				Mat data=itk::OpenCVImageBridge::ITKImageToCVMat<OpenCVType>(OpenCVImage);
				femure=data;
			}
			catch(cv::Exception exc)
			{
				cout<<exc.err<<endl;
			}

			cvtColor(femure,femure,CV_GRAY2RGB);
			cout<<femure.channels()<<endl;
			cv::resize(femure,femure,Size(XRAY_WIDTH, XRAY_HEIGHT));
			const string winName = "image";
			namedWindow( winName, WINDOW_AUTOSIZE );
			setMouseCallback( winName, on_mouse, 0 );
			gcapp.setImageAndWinName( femure, winName );
			gcapp.showImage();

					for(;;)
					{
						int c = waitKey(0);
						switch( (char) c )
						{
						case '\x1b':
							cout << "Exiting ..." << endl;
							if(gcapp.isInitialized)
							{
								XRayContour();
							}
							goto exit_main;
						case 'r':
							cout << endl;
							gcapp.reset();
							gcapp.showImage();
							break;
						case 'n':
							int iterCount = gcapp.getIterCount();
							cout << "<" << iterCount << "... ";
							int newIterCount = gcapp.nextIter();
							if( newIterCount > iterCount )
							{
								gcapp.showImage();
								cout << iterCount << ">" << endl;
							}
							else
								cout << "rect must be determined>" << endl;
							break;
						}
					}

	exit_main:
	waitKey(0);
	cv::destroyAllWindows();
		
}
Esempio n. 7
0
bool fRequestShutdown = false;
bool fShutdown = false;
bool fDaemon = false;
bool fServer = false;
Esempio n. 8
0
    static const int decode64_table[256] =
    {
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
        -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
        29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
        49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
    };

    if (pfInvalid)
        *pfInvalid = false;

    vector<unsigned char> vchRet;
    vchRet.reserve(strlen(p)*3/4);

    int mode = 0;
    int left = 0;

    while (1)
    {
         int dec = decode64_table[(unsigned char)*p];
         if (dec == -1) break;
         p++;
         switch (mode)
         {
             case 0: // we have no bits and get 6
                 left = dec;
                 mode = 1;
                 break;

              case 1: // we have 6 bits and keep 4
                  vchRet.push_back((left<<2) | (dec>>4));
                  left = dec & 15;
                  mode = 2;
                  break;

             case 2: // we have 4 bits and get 6, we keep 2
                 vchRet.push_back((left<<4) | (dec>>2));
                 left = dec & 3;
                 mode = 3;
                 break;

             case 3: // we have 2 bits and get 6
                 vchRet.push_back((left<<6) | dec);
                 mode = 0;
                 break;
         }
    }

    if (pfInvalid)
        switch (mode)
        {
            case 0: // 4n base64 characters processed: ok
                break;

            case 1: // 4n+1 base64 character processed: impossible
                *pfInvalid = true;
                break;

            case 2: // 4n+2 base64 characters processed: require '=='
                if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
                    *pfInvalid = true;
                break;

            case 3: // 4n+3 base64 characters processed: require '='
                if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
                    *pfInvalid = true;
                break;
        }

    return vchRet;
}

string DecodeBase64(const string& str)
{
    vector<unsigned char> vchRet = DecodeBase64(str.c_str());
    return string((const char*)&vchRet[0], vchRet.size());
}

string EncodeBase32(const unsigned char* pch, size_t len)
{
    static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";

    string strRet="";
    strRet.reserve((len+4)/5*8);

    int mode=0, left=0;
    const unsigned char *pchEnd = pch+len;

    while (pch<pchEnd)
    {
        int enc = *(pch++);
        switch (mode)
        {
            case 0: // we have no bits
                strRet += pbase32[enc >> 3];
                left = (enc & 7) << 2;
                mode = 1;
                break;

            case 1: // we have three bits
                strRet += pbase32[left | (enc >> 6)];
                strRet += pbase32[(enc >> 1) & 31];
                left = (enc & 1) << 4;
                mode = 2;
                break;

            case 2: // we have one bit
                strRet += pbase32[left | (enc >> 4)];
                left = (enc & 15) << 1;
                mode = 3;
                break;

            case 3: // we have four bits
                strRet += pbase32[left | (enc >> 7)];
                strRet += pbase32[(enc >> 2) & 31];
                left = (enc & 3) << 3;
                mode = 4;
                break;

            case 4: // we have two bits
                strRet += pbase32[left | (enc >> 5)];
                strRet += pbase32[enc & 31];
                mode = 0;
        }
    }

    static const int nPadding[5] = {0, 6, 4, 3, 1};
    if (mode)
    {
        strRet += pbase32[left];
        for (int n=0; n<nPadding[mode]; n++)
             strRet += '=';
    }

    return strRet;
}

string EncodeBase32(const string& str)
{
    return EncodeBase32((const unsigned char*)str.c_str(), str.size());
}

vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
{
    static const int decode32_table[256] =
    {
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
        -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1,  0,  1,  2,
         3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
        23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
    };

    if (pfInvalid)
        *pfInvalid = false;

    vector<unsigned char> vchRet;
    vchRet.reserve((strlen(p))*5/8);

    int mode = 0;
    int left = 0;

    while (1)
    {
         int dec = decode32_table[(unsigned char)*p];
         if (dec == -1) break;
         p++;
         switch (mode)
         {
             case 0: // we have no bits and get 5
                 left = dec;
                 mode = 1;
                 break;

              case 1: // we have 5 bits and keep 2
                  vchRet.push_back((left<<3) | (dec>>2));
                  left = dec & 3;
                  mode = 2;
                  break;

             case 2: // we have 2 bits and keep 7
                 left = left << 5 | dec;
                 mode = 3;
                 break;

             case 3: // we have 7 bits and keep 4