2012-07-11 8 views
7

Ich entwickle Projekt auf javacv und ich muss wissen, wie man folgendes Bild erkennt und das Bild mit einer bestimmten Farbe füllt?Wie identifiziere ich und fülle ich Konturen in javacv?

Ich versuche, diese question zu durchlaufen und das ist das Bild, das ich verwende

enter image description here

Ich versuche, durch diesen Code zu gehen, und ich entwickelte einen Code in 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); 

    } 
} 

Meine Endergebnis sollte wie dieses Bild sein

enter image description here

Put oben Code ergab diese Art von Bild. Kann mir bitte jemand helfen, dieses Problem zu lösen?

enter image description here

Antwort

1

können Sie archivieren, dass mit diesem Code

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

     } 
    } 

Ergebnis

enter image description here

Dies führte verlöschen wird und ich glaube, das Sie Ihr Problem lösen helfen könnte.

1

Der Code genau das tut, was tun soll:

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

es zu beheben, müssen Sie Spiele bis 255 (weiß setzen, vor jedem Aufruf zu drawContours und schwarzen Konturen zeichnen ! das ist 0, nicht 127.)

+0

Hay könnten Sie bitte erklären, wie Sie auf die gefundenen Konturenwerte zugreifen können? –

+0

In den Dokumenten und Codebeispielen finden Sie zahlreiche Beispiele. – Sam

1

Sie müssen nur Konturen Modus mit externen Retrieval finden:

CV_RETR_EXTERNAL retrives only the extreme outer contours 

In Ihrer Situation ist dies der beste Modus, weil Sie eine externe Kontur haben und diese Kontur ist, was Sie suchen. Here's Dokumantation.

Danach zeichnen Sie es einfach mit drawContours mit CV_FILLED als Dicke Parameter zum externen Polygon zu füllen.

Verwandte Themen