无法让MLP在OpenCV中工作

时间:2013-11-24 15:22:06

标签: c++ opencv neural-network

我尝试使用此代码将图像输入网络:

Mat img = imread("img.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat output(1,1,CV_32FC1, OpenCV Error: Incorrect size of input array (input matrix must have the same number of columns as the number of neurons in the input layer) in predict, file /build/opencv/src/opencv-2.4.6.1/modules/ml/src/ann_mlp.cpp, line 283
terminate called after throwing an instance of 'cv::Exception'
what():  /build/opencv/src/opencv-2.4.6.1/modules/ml/src/ann_mlp.cpp:283: error: (-201) input matrix must have the same number of columns as the number of neurons in the input layer in function predict
);
cout << output.size() << endl;
resize(img, img, Size(16,16));
img = img.reshape(1,1);
Mat input(1,256, CV_32FC1);
input.row(0) = img;

Mat layers(2,1,CV_32SC1);
layers.row(0) = Scalar(256);
layers.row(1) = Scalar(1);

CvANN_MLP mlp;
CvANN_MLP_TrainParams params;
CvTermCriteria criteria;
criteria.max_iter = 100;
criteria.epsilon = 0.00001f;
criteria.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS;
params.train_method = CvANN_MLP_TrainParams::BACKPROP;
params.bp_dw_scale = 0.05f;
params.bp_moment_scale = 0.05f;
params.term_crit = criteria;

mlp.create(layers, CvANN_MLP::SIGMOID_SYM, 1,1);

mlp.train(input, output, Mat(), Mat(), params);

但是我收到了这个错误:

input

如您所见,1x256256,网络在输入层有{{1}}个神经元,但我收到输入网络不正确的错误。

0 个答案:

没有答案