示例#1
0
void Nieto::ExpectationMaximizationOpenCV2Features(const Mat1b &imageI, const Mat1b &imageL,
	map<string, double> &i_means0, map<string, double> &i_covs0, map<string, double> &i_weights0,
	map<string, double> &l_means0, map<string, double> &l_covs0, map<string, double> &l_weights0, int maxIters) {

	double tempoInicio = static_cast<double>(getTickCount());
	const int nFeatures = 2; // 2 features => {I, L}
	const int nClusters = 4; // 4 classes => {pavement, markings, objects, unknown}
	const bool aplicaResize = true;

	// samples feature I
	Mat1b I_imageClone = imageI.clone();
	Mat1b I_trainImageClone = imageI.clone();
	if (aplicaResize) resize(I_imageClone, I_trainImageClone, Size(160, 35), 0, 0, INTER_NEAREST);
	Mat1d I_samples = I_trainImageClone.reshape(1, I_trainImageClone.rows * I_trainImageClone.cols);

	// samples feature L
	Mat1b L_imageClone = imageL.clone();
	Mat1b L_trainImageClone = imageL.clone();
	if (aplicaResize) resize(L_imageClone, L_trainImageClone, Size(160, 35), 0, 0, INTER_NEAREST);
	Mat1d L_samples = L_trainImageClone.reshape(1, L_trainImageClone.rows * L_trainImageClone.cols);

	// junta as amostras (uma em cada linha)
	Mat1d samplesArray[] = { I_samples, L_samples };
	Mat1d samples;
	cv::hconcat(samplesArray, 2, samples);
	
	// formata o _means0
	Mat1d means0 = Mat1d(nClusters, nFeatures, CV_64FC1);
	means0.at<double>(0, 0) = i_means0["pavement"];
	means0.at<double>(1, 0) = i_means0["markings"];
	means0.at<double>(2, 0) = i_means0["objects"];
	means0.at<double>(3, 0) = 255.0 / 2.0;
	means0.at<double>(0, 1) = l_means0["pavement"];
	means0.at<double>(1, 1) = l_means0["markings"];
	means0.at<double>(2, 1) = l_means0["objects"];
	means0.at<double>(3, 1) = 255.0 / 2.0;

	// formata o _covs0
	Mat1d covs0_pavement = Mat1d(Size(nFeatures, nFeatures), double(0));
	covs0_pavement.at<double>(0, 0) = i_covs0["pavement"];
	covs0_pavement.at<double>(1, 1) = l_covs0["pavement"];
	Mat1d covs0_markings = Mat1d(Size(nFeatures, nFeatures), double(0));;
	covs0_markings.at<double>(0, 0) = i_covs0["markings"];
	covs0_markings.at<double>(1, 1) = l_covs0["markings"];
	Mat1d covs0_objects = Mat1d(Size(nFeatures, nFeatures), double(0));;
	covs0_objects.at<double>(0, 0) = i_covs0["objects"];
	covs0_objects.at<double>(1, 1) = l_covs0["objects"];
	Mat1d covs0_unknown = Mat1d(Size(nFeatures, nFeatures), double(0));;
	covs0_unknown.at<double>(0, 0) = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3));
	covs0_unknown.at<double>(1, 1) = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3));
	vector<Mat> covs0 = {
		covs0_pavement,
		covs0_markings,
		covs0_objects,
		covs0_unknown
	};

	// formata o _weights0
	Mat1d weights0 = Mat1d(nClusters, 1, CV_64FC1);
	double total_i = i_weights0["pavement"] + i_weights0["markings"] + i_weights0["objects"] + i_weights0["unknown"];
	double total_l = l_weights0["pavement"] + l_weights0["markings"] + l_weights0["objects"] + l_weights0["unknown"];
	double total_weights = total_i + total_l;
	weights0.at<double>(0, 0) = (i_weights0["pavement"] + l_weights0["pavement"]) / total_weights;
	weights0.at<double>(1, 0) = (i_weights0["markings"] + l_weights0["markings"]) / total_weights;
	weights0.at<double>(2, 0) = (i_weights0["objects"] + l_weights0["objects"]) / total_weights;
	weights0.at<double>(3, 0) = (i_weights0["unknown"] + l_weights0["unknown"]) / total_weights;

	// cout << means0 << endl;

	// condi��es do EM
	// dims => samples.cols
	// if (!(&means0) || (!means0.empty() && means0.rows == nClusters && means0.cols == samples.cols && means0.channels() == 1)) cout << "means - ok!" << endl;

	EM em = EM(nClusters, EM::COV_MAT_DIAGONAL);
	em.set("maxIters", maxIters);
	em.trainE(samples, means0, covs0, weights0);
	
	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- em opencv (2 features): " << tempoExecutando << " ms" << endl;
	if (display) {
		// predict
		Mat1b predictedImage = Mat1b(I_imageClone.size(), uchar(0));
		for (int j = 0; j < predictedImage.rows; ++j) {
			unsigned char *ptRowI = I_imageClone.ptr<uchar>(j);
			unsigned char *ptRowL = L_imageClone.ptr<uchar>(j);
			unsigned char *ptRowDst = predictedImage.ptr<uchar>(j);
			for (int i = 0; i < predictedImage.cols; ++i) {
				
				Mat1d elementPredict = Mat1d(Size(2, 1), CV_64FC1);
				elementPredict.at<double>(0) = ptRowL[i];
				elementPredict.at<double>(1) = ptRowI[i];
				
				Vec2d emPredicted = em.predict(elementPredict);
				switch ((int)emPredicted[1]) {
				case 0: ptRowDst[i] = 160; break;
				case 1: ptRowDst[i] = 255; break;
				case 2: ptRowDst[i] = 80; break;
				case 3: ptRowDst[i] = 0; break;
				}
			}
		}
		imshow("EM OpenCV - 2 Features", predictedImage);
	}
}
示例#2
0
void Nieto::ExpectationMaximizationOpenCV(const Mat1b &inGrayFrameRoi, int maxIters, map<string, double> &_means0, map<string, double> &_covs0, map<string, double> &_weights0) {

	double tempoInicio = static_cast<double>(getTickCount());
	const int nClusters = 4; // 4 classes => {pavement, markings, objects, unknown}
	const bool aplicaResize = true;

	EM em = EM(nClusters, EM::COV_MAT_DIAGONAL);

	Mat1b grayFrameRoiClone = inGrayFrameRoi.clone();
	Mat1b trainGrayFrameRoiClone = inGrayFrameRoi.clone();
	if (aplicaResize) resize(grayFrameRoiClone, trainGrayFrameRoiClone, Size(160, 35), 0, 0, INTER_NEAREST);
	Mat1d samples = trainGrayFrameRoiClone.reshape(1, trainGrayFrameRoiClone.rows * trainGrayFrameRoiClone.cols);

	// formata o _means0
	Mat1d means0 = Mat1d(nClusters, 1, CV_64FC1);
	means0.at<double>(0) = _means0["pavement"];
	means0.at<double>(1) = _means0["markings"];
	means0.at<double>(2) = _means0["objects"];
	means0.at<double>(3) = 255.0 / 2.0;

	// formata o _covs0
	vector<Mat> covs0 = {
		Mat1d(1, 1, _covs0["pavement"]),
		Mat1d(1, 1, _covs0["markings"]),
		Mat1d(1, 1, _covs0["objects"]),
		Mat1d(1, 1, ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3)))
	};

	// formata o _weights0
	// Mat1d weights0 = *(Mat1f(nClusters, 1, CV_64FC1) << 0.75, 0.10, 0.10, 0.05);
	Mat1d weights0 = *(Mat1f(nClusters, 1, CV_64FC1) <<
		_weights0["pavement"],
		_weights0["markings"],
		_weights0["objects"],
		_weights0["unknown"]
		);

	// cout << means0 << endl;

	em.set("maxIters", maxIters);
	em.trainE(samples, means0, covs0, weights0);

	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- em opencv (1 feature): " << tempoExecutando << " ms" << endl;
	if (display) {
		// predict
		Mat1b predictedImage = Mat1b(grayFrameRoiClone.size(), uchar(0));
		for (int j = 0; j < predictedImage.rows; ++j) {
			unsigned char *ptRowSrc = grayFrameRoiClone.ptr<uchar>(j);
			unsigned char *ptRowDst = predictedImage.ptr<uchar>(j);
			for (int i = 0; i < predictedImage.cols; ++i) {
				Vec2d emPredicted = em.predict(ptRowSrc[i]);
				switch ((int)emPredicted[1]) {
				case 0: ptRowDst[i] = 160; break;
				case 1: ptRowDst[i] = 255; break;
				case 2: ptRowDst[i] = 80; break;
				case 3: ptRowDst[i] = 0; break;
				}
			}
		}
		imshow("EM OpenCV - 1 Feature", predictedImage);
	}
}