How to identify specific object using JavaCV

孤人 提交于 2019-12-24 19:25:06

问题


I am trying to detect doctor's tools from endoscopy video using the following code

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

The input of the above code is a color image with 2 doctor's object and the output is a grayscale image that detect the tools. Sorry, but the system doesn't allow to upload the images.

The problem is I don't know how to find the position of 2 tools and draw a rectangle. Please can some one explain how to archive my objective using javacv/opencv?


回答1:


It can be achieve using Haar Casacde files .Some of the bulit-In Haar files provided by google can found in opencv\data\haarcascades directory .These files are XML files generated by heavy training sections.Some of the sample haar files are used for face detection,ear detection etc.A sample project can be fount at here.You can generate your own haar cascade file for any purpose.one of the method for generate Haar cascade file can be found at here



来源:https://stackoverflow.com/questions/19447885/how-to-identify-specific-object-using-javacv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!