2012-07-11 32 views
7

Tôi đang phát triển dự án trên javacv và tôi cần biết cách xác định hình ảnh sau đây và điền vào Hình ảnh đó bằng cách sử dụng màu cụ thể?Làm thế nào để xác định và điền vào tôi hình dạng Contours trong javacv?

tôi cố gắng đi qua question này và Đây là hình ảnh mà tôi sử dụng

enter image description here

tôi cố gắng đi qua đoạn mã này và tôi đã phát triển một mã trong javacv

import com.googlecode.javacpp.Loader; 
import com.googlecode.javacv.CanvasFrame; 
import static com.googlecode.javacpp.Loader.*; 
import static com.googlecode.javacv.cpp.opencv_core.*; 
import static com.googlecode.javacv.cpp.opencv_highgui.*; 
import static com.googlecode.javacv.cpp.opencv_imgproc.*; 
import java.io.File; 
import javax.swing.JFileChooser; 

public class PolyGonIdentification { 
    public static void main(String[] args) { 
     CanvasFrame cnvs=new CanvasFrame("Polygon"); 
     cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); 

     CvMemStorage storage=CvMemStorage.create(); 
     CvSeq squares = new CvContour(); 
     squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage); 
     JFileChooser f=new JFileChooser(); 
     int result=f.showOpenDialog(f);//show dialog box to choose files 
     File myfile=null; 
     String path=""; 
     if(result==0){ 
      myfile=f.getSelectedFile();//selected file taken to myfile 
      path=myfile.getAbsolutePath();//get the path of the file 
     } 
     IplImage src = cvLoadImage(path);//hear path is actual path to image 
     IplImage gry=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); 
     cvCvtColor(src, gry, CV_BGR2GRAY); 
     cvThreshold(gry, gry, 230, 255, CV_THRESH_BINARY_INV); 
     cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); 
     System.out.println(squares.total()); 
     for (int i=0; i<squares.total(); i++) 
     { 
      cvDrawContours(gry, squares, CvScalar.ONE, CvScalar.ONE, 127, 1, 8); 
     } 
     IplConvKernel mat=cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_RECT, null); 
     cvDilate(gry, gry, mat, CV_C); 
     cvErode(gry, gry, mat, CV_C); 
     cnvs.showImage(gry); 

    } 
} 

My kết quả cuối cùng phải giống như hình ảnh này

enter image description here

Đặt mã ở trên dẫn đến loại hình ảnh này. Xin vui lòng một số người có thể giúp tôi giải quyết vấn đề này?

enter image description here

Trả lời

1

Bạn có thể lưu trữ rằng việc sử dụng mã này

import com.googlecode.javacpp.Loader; 
    import com.googlecode.javacv.CanvasFrame; 
    import static com.googlecode.javacpp.Loader.*; 
    import static com.googlecode.javacv.cpp.opencv_core.*; 
    import static com.googlecode.javacv.cpp.opencv_highgui.*; 
    import static com.googlecode.javacv.cpp.opencv_imgproc.*; 
    import java.io.File; 
    import javax.swing.JFileChooser; 

    public class Ishape { 
     public static void main(String[] args) { 
      CanvasFrame cnvs=new CanvasFrame("Polygon"); 
      cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); 

      CvMemStorage storage=CvMemStorage.create(); 
      CvSeq squares = new CvContour(); 
      squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage); 
      JFileChooser f=new JFileChooser(); 
      int result=f.showOpenDialog(f);//show dialog box to choose files 
       File myfile=null; 
       String path=""; 
      if(result==0){ 
       myfile=f.getSelectedFile();//selected file taken to myfile 
       path=myfile.getAbsolutePath();//get the path of the file 
      } 
      IplImage src = cvLoadImage(path);//hear path is actual path to image 
      IplImage gry=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); 
      cvCvtColor(src, gry, CV_BGR2GRAY); 
      cvThreshold(gry, gry, 230, 255, CV_THRESH_BINARY_INV); 
      cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); 
      CvSeq ss=null; 
      for (int i=0; i<1; i++) 
      { 
       cvDrawContours(gry, squares, CvScalar.WHITE, CV_RGB(248, 18, 18), 1, -1, 8); 
       ss=cvApproxPoly(squares, sizeof(CvContour.class), storage, CV_POLY_APPROX_DP, 8, 0); 
      } 
      IplConvKernel mat=cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_RECT, null); 
      cvDilate(gry, gry, mat, CV_C); 
      cvErode(gry, gry, mat, CV_C); 
      cnvs.showImage(gry); 

     } 
    } 

quả

enter image description here

này sẽ được dẫn ra đặt và tôi tin rằng điều này có thể giúp bạn giải quyết vấn đề của bạn.

1

Mã này thực hiện chính xác những gì đang phải làm:

// as stated [in the answer to your previous question][1], findContours leaves gry set to 0, or black 
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), 
     CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); 

    System.out.println(squares.total()); // nothing interesting 

    for (int i = 0; i < squares.total(); i++) 
    { 
     // draw a gray contour (127) on a black image 
     cvDrawContours(gry, squares, CvScalar.ONE, CvScalar.ONE, 127, 1, 8); 
    } 

Để khắc phục điều đó, bạn phải thiết lập gry đến 255 (trắng, trước khi bất kỳ cuộc gọi đến drawContours, và vẽ đường viền đen ! đó là 0, không 127.)

+0

Hay bạn có thể giải thích cách truy cập các giá trị điểm Contours được tìm thấy không? –

+0

Có rất nhiều ví dụ trong tài liệu và mẫu mã. – Sam

1

Bạn chỉ cần phải tìm đường nét sử dụng chế độ phục hồi bên ngoài:

CV_RETR_EXTERNAL retrives only the extreme outer contours 

Trong trường hợp của bạn, đây là chế độ tốt nhất vì bạn có một đường viền bên ngoài và đường bao này là thứ bạn đang tìm kiếm. Here's documantation.

Sau này, chỉ cần vẽ nó bằng cách sử dụng drawContours với CV_FILLED làm tham số độ dày để điền vào đa giác bên ngoài.

Các vấn đề liên quan