2013-10-18 41 views
0

我試圖從內窺鏡視頻檢測醫生的工具,使用下面的代碼如何使用JavaCV

static void video_tracking(){ 
     //read image 
     IplImage orgImg = cvLoadImage("C:/Users/Ioanna/Desktop/pic.png"); 

     IplImage thresholdImage = hsvThreshold(orgImg); 
     cvSaveImage("hsvthreshold.jpg", thresholdImage); 
     Dimension position = getCoordinates(thresholdImage); 
     System.out.println("Dimension of original Image : " + thresholdImage.width() + " , " + thresholdImage.height()); 
     System.out.println("Position of red spot : x : " + position.width + " , y : " + position.height); 

    } 

    static Dimension getCoordinates(IplImage thresholdImage) { 
     int posX = 0; 
     int posY = 0; 
     CvMoments moments = new CvMoments(); 
     cvMoments(thresholdImage, moments, 1); 
     double momX10 = cvGetSpatialMoment(moments, 1, 0); // (x,y) 
     double momY01 = cvGetSpatialMoment(moments, 0, 1);// (x,y) 
     double area = cvGetCentralMoment(moments, 0, 0); 
     posX = (int) (momX10/area); 
     posY = (int) (momY01/area); 
     return new Dimension(posX, posY); 
    } 

    static IplImage hsvThreshold(IplImage orgImg) { 

     // Convert the image into an HSV image 
     IplImage imgHSV = cvCreateImage(cvGetSize(orgImg), 8, 3); 

     cvCvtColor(orgImg, imgHSV, CV_BGR2HSV); 

     //create a new image that will hold the threholded image 
     // 1- color = monochrome 
     IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), orgImg.depth(), 1); 

     //do the actual thresholding 
     cvInRangeS(imgHSV, cvScalar(13, 0, 0, 0), cvScalar(40, 117, 124, 88), imgThreshold); 

     cvReleaseImage(imgHSV); 

     cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 13); 
     // save 
     return imgThreshold; 
    } 

上述代碼的輸入,以確定具體的目標是用2醫生的對象的彩色圖像,輸出是檢測工具的灰度圖像。抱歉,系統不允許上傳圖片。

問題是我不知道如何找到2個工具的位置並繪製一個矩形。請解釋一下如何使用javacv/opencv存檔我的目標?

回答

0

它可以使用Haar Casacde文件實現。谷歌提供的一些bulit-in Haar文件可以在opencv \ data \ haarcascades目錄中找到。這些文件是由重型訓練部分生成的XML文件。一些示例haar文件用於人臉檢測,人耳檢測等。一個示例項目可以從here獲得。您可以爲任何目的生成自己的哈爾級聯文件。其中一種生成哈爾級聯文件的方法可以在here