我对下面的源代码进行了检查。 我想要做的是训练一个SVM模型与一组已经分类的图像。我在SO上找到了一个有趣的例子,但是要明白这里到底发生了什么是有问题的。现在让我们来看看:
public class Training{
protected static final String PATH_POSITIVE = "data/positivo/";
protected static final String PATH_NEGATIVE = "data/negativo/";
protected static final String XML = "data/test.xml";
protected static final String FILE_TEST = "data/negativo/1.jpg";
private static Mat trainingImages;
private static Mat trainingLabels;
private static Mat trainingData;
private static Mat classes;
private static CvSVM clasificador;
static {
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
trainingImages = new Mat();
trainingLabels = new Mat();
trainingData = new Mat();
classes = new Mat();
}
public static void main( String[ ] args ) {
trainPositive();
trainNegative();
train();
test();
}
protected static void test() {
Mat in = Highgui.imread( new File( FILE_TEST ).getAbsolutePath(), Highgui.CV_LOAD_IMAGE_GRAYSCALE );
clasificador.load( new File( XML ).getAbsolutePath() );
System.out.println( clasificador );
Mat out = new Mat();
in.convertTo( out, CvType.CV_32FC1 );
out = out.reshape( 1, 1 );
System.out.println( out );
System.out.println( clasificador.predict( out ) );
}
protected static void train() {
trainingImages.copyTo( trainingData );
trainingData.convertTo( trainingData, CvType.CV_32FC1 );
trainingLabels.copyTo( classes );
CvSVMParams params = new CvSVMParams();
params.set_kernel_type( CvSVM.LINEAR );
clasificador = new CvSVM( trainingData, classes, new Mat(), new Mat(), params );
clasificador.save( XML );
}
protected static void trainPositive() {
for ( File file : new File( PATH_POSITIVE ).listFiles() ) {
Mat img = getMat( file.getAbsolutePath() );
trainingImages.push_back( img.reshape( 1, 1 ) );
trainingLabels.push_back( Mat.ones( new Size( 1, 1 ), CvType.CV_32FC1 ) );
}
}
protected static void trainNegative() {
for ( File file : new File( PATH_NEGATIVE ).listFiles() ) {
Mat img = getMat( file.getAbsolutePath() );
trainingImages.push_back( img.reshape( 1, 1 ) );
trainingLabels.push_back( Mat.zeros( new Size( 1, 1 ), CvType.CV_32FC1 ) );
}
}
protected static Mat getMat( String path ) {
Mat img = new Mat();
Mat con = Highgui.imread( path, Highgui.CV_LOAD_IMAGE_GRAYSCALE );
con.convertTo( img, CvType.CV_32FC1, 1.0 / 255.0 );
return img;
}
}
基本上我有两个问题。
1。行protected static final String PATH_POSITIVE = "data/positivo/";
是否是所有正图像的路径?如果是这样,路径是否会导致包含您在开始时使用SIFT或Surf创建的每张图片的描述符的文件夹?或者是否有必要在其中添加图像?
2。我不想全局了。我如何定义一个对我来说很有趣的区域。这对于所有图片都是必不可少的。(训练和测试)例如每张图片的下半部分没有剪切。