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; }
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; }
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; }
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(); }