diff --git a/classifyDisclaimerDlg.ui b/classifyDisclaimerDlg.ui index c8f0091..35d360d 100644 --- a/classifyDisclaimerDlg.ui +++ b/classifyDisclaimerDlg.ui @@ -32,7 +32,7 @@ - :/CC/plugin/q3DMASC/Logo_cnrs_eub_eec.png + :/CC/plugin/q3DMASCPlugin/Logo_cnrs_eub_eec.png true diff --git a/info.json b/info.json index dffa0c4..bdf9720 100644 --- a/info.json +++ b/info.json @@ -1,7 +1,7 @@ { "type": "Standard", "name": "3DMASC", - "icon": ":/CC/plugin/q3DMASC/iconCreate.png", + "icon": ":/CC/plugin/q3DMASCPlugin/iconCreate.png", "description": "Create or apply a classifier on a point cloud.", "authors": [ { diff --git a/q3DMASC.cpp b/q3DMASC.cpp index a8e744a..7de582c 100644 --- a/q3DMASC.cpp +++ b/q3DMASC.cpp @@ -154,9 +154,10 @@ void q3DMASCPlugin::doClassifyAction() } int randomCount = 0; + int randIndex = 0; while (randomCount < testSampleCount) { - int randIndex = (std::rand() % totalSampleCount); + randIndex = ((randIndex + std::rand()) % totalSampleCount); if (isSample[randIndex]) { isSample[randIndex] = false; @@ -172,10 +173,10 @@ void q3DMASCPlugin::doClassifyAction() try { training_data.create(sampleCount, attributesPerSample, CV_32FC1); - train_labels.create(sampleCount, 1, CV_8U); + train_labels.create(sampleCount, 1, CV_32FC1); test_data.create(testSampleCount, attributesPerSample, CV_32FC1); - test_labels.create(testSampleCount, 1, CV_8U); + test_labels.create(testSampleCount, 1, CV_32FC1); } catch (const cv::Exception& cvex) { @@ -199,14 +200,14 @@ void q3DMASCPlugin::doClassifyAction() if (isSample[i]) { - train_labels.at(sampleIndex++) = static_cast(iClass); + train_labels.at(sampleIndex++) = static_cast(iClass); } else { - test_labels.at(testSampleIndex++) = static_cast(iClass); + test_labels.at(testSampleIndex++) = static_cast(iClass); } } - assert(testSampleIndex + testSampleIndex == totalSampleCount); + assert(sampleIndex + testSampleIndex == totalSampleCount); } @@ -267,14 +268,16 @@ void q3DMASCPlugin::doClassifyAction() double value = source->pointValue(i); if (isSample[i]) { + assert(sampleIndex < sampleCount); training_data.at(sampleIndex++, fIndex) = static_cast(value); } else { + assert(testSampleIndex< testSampleCount); test_data.at(testSampleIndex++, fIndex) = static_cast(value); } } - assert(testSampleIndex + testSampleIndex == totalSampleCount); + assert(sampleIndex + testSampleIndex == totalSampleCount); } cv::Ptr rtrees; @@ -290,8 +293,25 @@ void q3DMASCPlugin::doClassifyAction() //rtrees->setUseSurrogates(false); //rtrees->setMaxCategories(params.maxCategories); //not important? //rtrees->setPriors(cv::Mat()); - - rtrees->train(training_data, cv::ml::ROW_SAMPLE, train_labels); + try + { + rtrees->train(training_data, cv::ml::ROW_SAMPLE, train_labels); + } + catch (const cv::Exception& cvex) + { + ccLog::Error(cvex.msg.c_str()); + return; + } + catch (const std::exception& stdex) + { + ccLog::Error(stdex.what()); + return; + } + catch (...) + { + ccLog::Error("Unknown error"); + return; + } if (!rtrees->isTrained()) { @@ -304,7 +324,7 @@ void q3DMASCPlugin::doClassifyAction() int goodGuessCount = 0; for (int j = 0; j < testSampleCount; ++j) { - if (rtrees->predict(test_data.row(j)) == test_labels.at(j)) + if (rtrees->predict(test_data.row(j)) == test_labels.at(j)) { ++goodGuessCount; } @@ -315,6 +335,11 @@ void q3DMASCPlugin::doClassifyAction() m_app->dispToConsole(QString("Correct = %1 / %2 --> Accuracy = %3").arg(goodGuessCount).arg(testSampleCount).arg(acc), ccMainAppInterface::STD_CONSOLE_MESSAGE); } + //save the classifier + QString outputFilename = QCoreApplication::applicationDirPath() + "/classifier.yaml"; + ccLog::Print("Classifier file saved to: " + outputFilename); + rtrees->save(outputFilename.toStdString()); + } //OpenCV diff --git a/q3DMASC.qrc b/q3DMASC.qrc index 6fa4b2e..6c4a4b7 100644 --- a/q3DMASC.qrc +++ b/q3DMASC.qrc @@ -1,7 +1,8 @@ - + iconClassify.png iconCreate.png Logo_cnrs_eub_eec.png + info.json diff --git a/trainDisclaimerDlg.ui b/trainDisclaimerDlg.ui index 510681f..e5a80a3 100644 --- a/trainDisclaimerDlg.ui +++ b/trainDisclaimerDlg.ui @@ -32,7 +32,7 @@ - :/CC/plugin/q3DMASC/Logo_cnrs_eub_eec.png + :/CC/plugin/q3DMASCPlugin/Logo_cnrs_eub_eec.png true