Ejemplo n.º 1
0
void Nieto::ExpectationMaximizationArmadillo(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}

	Mat1b grayFrameRoiClone = inGrayFrameRoi.clone();
	Mat1b trainGrayFrameRoiClone = inGrayFrameRoi.clone();
	resize(grayFrameRoiClone, trainGrayFrameRoiClone, Size(160, 35), 0, 0, INTER_NEAREST);
	Mat1d samples = trainGrayFrameRoiClone.reshape(1, trainGrayFrameRoiClone.rows * trainGrayFrameRoiClone.cols);
	arma::mat armaSamples(reinterpret_cast<double*>(samples.data), samples.rows, samples.cols);

	// cout << "size armaSamples: " << arma::size(armaSamples) << endl;

	// formata o _means0
	arma::mat means0(1, nClusters);
	means0.at(0, 0) = _means0["pavement"];
	means0.at(0, 1) = _means0["markings"];
	means0.at(0, 2) = _means0["objects"];
	means0.at(0, 3) = 255.0 / 2.0;

	// cout << "size means0: " << arma::size(means0) << endl;

	// formata o _covs0
	arma::mat covs0(1, nClusters);
	covs0.at(0, 0) = _covs0["pavement"];
	covs0.at(0, 1) = _covs0["markings"];
	covs0.at(0, 2) = _covs0["objects"];
	covs0.at(0, 3) = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3));

	// cout << "size covs0: " << arma::size(covs0) << endl;

	// formata o _weights0
	arma::mat weights0(1, nClusters);
	weights0.at(0, 0) = _weights0["pavement"];
	weights0.at(0, 1) = _weights0["markings"];
	weights0.at(0, 2) = _weights0["objects"];
	weights0.at(0, 3) = _weights0["unknown"];

	// cout << "size weights0: " << arma::size(weights0) << endl;

	// if (!(size(means0) != size(covs0))) cout << "1 - ok!" << endl;
	// if (!(weights0.n_cols != means0.n_cols)) cout << "2 - ok!" << endl;
	// if (!(weights0.n_rows != 1)) cout << "3 - ok!" << endl;

	arma::gmm_diag em;
	em.set_params(means0, covs0, weights0);
	em.learn(armaSamples.t(), nClusters, arma::eucl_dist, arma::keep_existing, 0, maxIters, 1e-10, false);

	// 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 armadillo (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) {
				arma::vec v;
				v << ptRowSrc[i];
				int emPredicted = em.assign(v, arma::eucl_dist);
				switch (emPredicted) {
				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 Armadillo - 1 Feature", predictedImage);
	}
}
Ejemplo n.º 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);
	}
}