OpenCV drawContours问题

时间:2014-11-22 20:32:56

标签: java opencv-drawcontour

我在Eclipse Luna中使用OpenCV(Java)。我对drawContours方法有疑问。 代码没有错误,但是当我去运行它时,图像会加载,但是没有可见的计数器 图像类:

public static void main(String[] args){
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat m = Highgui.imread("C:/Users/MyName/Desktop/shapes.png", Highgui.CV_LOAD_IMAGE_COLOR);
    Mat hsv = new Mat();
    Mat mask = new Mat();
    Mat dilmask = new Mat();
    Mat fin = new Mat();
    Scalar color = new Scalar(239, 117, 94);
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.cvtColor(m, hsv, Imgproc.COLOR_RGB2HSV);
    new LoadImage("C:/Users/MyName/Desktop/shapes2.png", m);
    Scalar lowerThreshold = new Scalar ( 120, 100, 100 );
    Scalar upperThreshold = new Scalar ( 179, 255, 255 );
    Core.inRange (hsv, lowerThreshold , upperThreshold, mask);
    Imgproc.dilate ( mask, dilmask, new Mat() );
    Imgproc.findContours(dilmask, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
    //Imgproc.drawContours(fin, contours, -1, color, 0);
     for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ )
      {
         if(contours.size()>10)  // Minimum size allowed for consideration
         { 
             Imgproc.drawContours ( fin, contours, contourIdx, color, 3);
         }
      }
    //Highgui.imwrite("C:/Users/MyName/Desktop/shapes2.png", fin);



}

}

LoadImage类(不是很重要,只是制作一个框架来显示图像):

public class LoadImage extends JFrame{
public LoadImage(String imgStr,Mat m)
{
Highgui.imwrite(imgStr,m);
JFrame frame = new JFrame("My GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setResizable(true);
frame.setLocationRelativeTo(null);

// Inserts the image icon
ImageIcon image = new ImageIcon(imgStr);
frame.setSize(image.getIconWidth()+10,image.getIconHeight()+35);
// Draw the Image data into the BufferedImage
JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
frame.getContentPane().add(label1);

frame.validate();
frame.setVisible(true);}



}

Input image

这可能是一个简单的解决方法,但我找不到任何相关内容。

1 个答案:

答案 0 :(得分:-2)

我知道答案,玩的是:

Scalar lowerThreshold = new Scalar (h1, s1, v1);
Scalar upperThreshold = new Scalar (h2, s2, v2);