#include "onf_stepfillmaskgaps.h" #include "ct_itemdrawable/tools/iterator/ct_groupiterator.h" #include "ct_result/ct_resultgroup.h" #include "ct_result/model/inModel/ct_inresultmodelgrouptocopy.h" #include "ct_result/model/outModel/tools/ct_outresultmodelgrouptocopypossibilities.h" #include "ct_view/ct_stepconfigurabledialog.h" #include "ct_itemdrawable/ct_attributeslist.h" #include "ct_itemdrawable/ct_profile.h" #include "ct_iterator/ct_groupiterator.h" #include "ct_shape2ddata/ct_polygon2ddata.h" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/imgproc/types_c.h" #include "opencv2/core/core.hpp" // Alias for indexing models #define DEFin_res "res" #define DEFin_mainGrp "maingrp" #define DEFin_mask "mask" // Constructor : initialization of parameters ONF_StepFillMaskGaps::ONF_StepFillMaskGaps(CT_StepInitializeData &dataInit) : CT_AbstractStep(dataInit) { } // Step description (tooltip of contextual menu) QString ONF_StepFillMaskGaps::getStepDescription() const { return tr("Remplir les trous dans un masque"); } // Step detailled description QString ONF_StepFillMaskGaps::getStepDetailledDescription() const { return tr("No detailled description for this step"); } // Step URL QString ONF_StepFillMaskGaps::getStepURL() const { //return tr("STEP URL HERE"); return CT_AbstractStep::getStepURL(); //by default URL of the plugin } // Step copy method CT_VirtualAbstractStep* ONF_StepFillMaskGaps::createNewInstance(CT_StepInitializeData &dataInit) { return new ONF_StepFillMaskGaps(dataInit); } //////////////////// PROTECTED METHODS ////////////////// // Creation and affiliation of IN models void ONF_StepFillMaskGaps::createInResultModelListProtected() { CT_InResultModelGroupToCopy *res = createNewInResultModelForCopy(DEFin_res, tr("Couronnes")); res->setZeroOrMoreRootGroup(); res->addGroupModel("", DEFin_mainGrp, CT_AbstractItemGroup::staticGetType(), tr("Groupe")); res->addItemModel(DEFin_mainGrp, DEFin_mask, CT_Image2D::staticGetType(), tr("Masque")); } // Creation and affiliation of OUT models void ONF_StepFillMaskGaps::createOutResultModelListProtected() { CT_OutResultModelGroupToCopyPossibilities *res = createNewOutResultModelToCopy(DEFin_res); if (res != NULL) { res->addItemModel(DEFin_mainGrp, _outMask_ModelName, new CT_Image2D(), tr("Masque Modifié")); } } void ONF_StepFillMaskGaps::compute() { QList outResultList = getOutResultList(); CT_ResultGroup* res = outResultList.at(0); // COPIED results browsing CT_ResultGroupIterator itCpy(res, this, DEFin_mainGrp); while (itCpy.hasNext() && !isStopped()) { CT_StandardItemGroup* mainGrp = (CT_StandardItemGroup*) itCpy.next(); CT_Image2D* maskIn = (CT_Image2D*)mainGrp->firstItemByINModelName(this, DEFin_mask); if (maskIn != nullptr) { CT_Image2D* maskOut = new CT_Image2D(_outMask_ModelName.completeName(), res, maskIn->minX(), maskIn->minY(), maskIn->colDim(), maskIn->linDim(), maskIn->resolution(), maskIn->level(), maskIn->NA(), 0); mainGrp->addItemDrawable(maskOut); maskOut->getMat() = maskIn->getMat().clone(); // Remplissage des trous dans le masque std::vector > contours2; cv::findContours(maskOut->getMat(), contours2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); cv::Scalar color(1); cv::drawContours(maskOut->getMat(), contours2, -1, color, -1); // Ne conserve d'un segment (le plus gros) cv::Mat labels; cv::Mat stats; cv::Mat centroids; int Nsegs = cv::connectedComponentsWithStats(maskOut->getMat(), labels, stats, centroids, 4); if (Nsegs > 2) { int maxArea = 0; int keptLabel = 1; for (int i = 1 ; i < Nsegs ; i++) { int area = stats.at(i, cv::CC_STAT_AREA); if (area > maxArea) { maxArea = area; keptLabel = i; } } for (size_t xx = 0 ; xx < maskOut->colDim() ; xx++) { for (size_t yy = 0 ; yy < maskOut->linDim() ; yy++) { size_t index; if (maskOut->index(xx, yy, index)) { if (labels.at(yy, xx) != keptLabel) { maskOut->setValue(xx, yy, maskOut->NA()); } } else { qDebug() << "Problème"; } } } } maskOut->computeMinMax(); } } setProgress(100); }