2012-11-13 6 views
2

JavaCV를 사용하여 동작을 감지하는 프로그램을 개발했습니다. 지금까지 나는 처리 된 이미지의 cvFindContours를 완료했습니다. 소스 코드는이 코드 ROIFrame에서 opencv 및 Javacv를 사용하여 윤곽선의 흰색 영역 픽셀을 계산하십시오.

public class MotionDetect { 

public static void main(String args[]) throws Exception, InterruptedException { 

    //FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(new File("D:/pool.avi")); 
    OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("D:/2.avi"); 
    final CanvasFrame canvas = new CanvasFrame("My Image"); 
    final CanvasFrame canvas2 = new CanvasFrame("ROI"); 
    canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); 
    grabber.start(); 
    IplImage frame = grabber.grab(); 
    CvSize imgsize = cvGetSize(frame); 
    IplImage grayImage = cvCreateImage(imgsize, IPL_DEPTH_8U, 1); 
    IplImage ROIFrame = cvCreateImage(cvSize((265 - 72), (214 - 148)), IPL_DEPTH_8U, 1); 
    IplImage colorImage; 
    IplImage movingAvg = cvCreateImage(imgsize, IPL_DEPTH_32F, 3); 
    IplImage difference = null; 
    IplImage temp = null; 
    IplImage motionHistory = cvCreateImage(imgsize, IPL_DEPTH_8U, 3); 


    CvRect bndRect = cvRect(0, 0, 0, 0); 
    CvPoint pt1 = new CvPoint(), pt2 = new CvPoint(); 
    CvFont font = null; 

    //Capture the movie frame by frame. 
    int prevX = 0; 
    int numPeople = 0; 
    char[] wow = new char[65]; 

    int avgX = 0; 

    //Indicates whether this is the first time in the loop of frames. 
    boolean first = true; 

    //Indicates the contour which was closest to the left boundary before the object 
    //entered the region between the buildings. 
    int closestToLeft = 0; 
    //Same as above, but for the right. 
    int closestToRight = 320; 


    while (true) { 
     colorImage = grabber.grab(); 
     if (colorImage != null) { 
      if (first) { 
       difference = cvCloneImage(colorImage); 
       temp = cvCloneImage(colorImage); 
       cvConvertScale(colorImage, movingAvg, 1.0, 0.0); 
       first = false; 
       //cvShowImage("My Window1", difference); 
      } //else, make a running average of the motion. 
      else { 
       cvRunningAvg(colorImage, movingAvg, 0.020, null); 
      } 

      //Convert the scale of the moving average. 
      cvConvertScale(movingAvg, temp, 1.0, 0.0); 

      //Minus the current frame from the moving average. 
      cvAbsDiff(colorImage, temp, difference); 

      //Convert the image to grayscale. 
      cvCvtColor(difference, grayImage, CV_RGB2GRAY); 
      //canvas.showImage(grayImage); 
      //Convert the image to black and white. 
      cvThreshold(grayImage, grayImage, 70, 255, CV_THRESH_BINARY); 

      //Dilate and erode to get people blobs 
      cvDilate(grayImage, grayImage, null, 18); 
      cvErode(grayImage, grayImage, null, 10); 
      canvas.showImage(grayImage); 


      ROIFrame = cvCloneImage(grayImage); 
      cvSetImageROI(ROIFrame, cvRect(72, 148, (265 - 72), (214 - 148))); 
      //cvOr(outFrame, tempFrame, outFrame); 
      cvShowImage("ROI Frame", ROIFrame); 



      cvRectangle(colorImage, /* the dest image */ 
        cvPoint(72, 148), /* top left point */ 
        cvPoint(265, 214), /* bottom right point */ 
        cvScalar(255, 0, 0, 0), /* the color; blue */ 
        1, 8, 0); 

      CvMemStorage storage = cvCreateMemStorage(0); 
      CvSeq contour = new CvSeq(null); 
      cvFindContours(grayImage, storage, contour, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); 

     } 




      //Show the frame. 
      cvShowImage("My Window", colorImage); 

      //Wait for the user to see it. 
      cvWaitKey(10); 

     } 

     //If this is the first time, initialize the images. 

     //Thread.sleep(50); 
    } 

    } 
} 

, 난 흰색 윤곽선 영역 또는 픽셀 수를 계산해야, 아래와 같습니다 ?? .. 내가

답변

2

사용하는 기능 cvContourArea() 문서를 진행할 수있는 방법이있다 here. 당신의 cvFindContours 후 코드에서

는 같은처럼 윤곽의 모두와 함께 루프를 수행

CvSeq* curr_contour = contour; 

while (curr_contour != NULL) { 
    area = fabs(cvContourArea(curr_contour,CV_WHOLE_SEQ, 0)); 
    current_contour = current_contour->h_next; 
} 

어딘가에 영역을 저장하는 것을 잊지 마십시오.

+0

정말 감사합니다. 만약 내가 하얀 픽셀을 계산하고 싶다면 어떻게 할 것인가? – thusi

+0

하지만 윤곽선을 찾기 전에 이미지를 이진화해야할까요? thresholding 후에는 모든 것이 검정 또는 흰색이 될 것이므로 cvContourArea는 각 등고선 (흰색 픽셀 포함)의 면적을 계산합니다. – cyberdecker

+0

감사합니다. 나는 cvContourArea ..와 함께 그것을 할 수 있었다. 다시 한번 감사드립니다. – thusi

관련 문제