我将 OpenCV (Java) 与 Eclipse Luna 一起使用。我遇到了 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);}
}
这可能是一个简单的修复,但我找不到任何关于它的东西。