void testConnectedComponent() { Mat testMat(480,640,CV_32FC1); Mat mask(480,640,CV_32FC1); mask.setTo(Scalar(1.0)); Mat firstGroup = testMat(Range(0,120),Range(0,200)); Mat secondGroup = testMat(Range(120,240),Range(200,400)); Mat thirdGroup = testMat(Range(240,480),Range(400,640)); firstGroup.setTo(Scalar(15)); secondGroup.setTo(Scalar(21)); thirdGroup.setTo(Scalar(15)); FileStorage fs1("testCompon.yml",FileStorage::READ); fs1["Depth"] >> testMat; fs1.release(); //Next we want to dilate and erode just a little dilate(testMat,testMat,Mat(),Point(-1,-1),2); //FileStorage fs3("FilteredDepthDilate.yml",FileStorage::WRITE); //fs3 << "FilteredDepthDilate" << filteredDepth; //fs3.release(); //Next we want to dilate and erode just a little erode(testMat,testMat,Mat(),Point(-1,-1),2); //We will need to normalize to such that it fits in 255 to 0 for the canny function so create a mat for that Mat normalizedFilteredDepth(testMat.size(),CV_8UC1); normalize(testMat,normalizedFilteredDepth,0,255,NORM_MINMAX,CV_32FC1); testMat = normalizedFilteredDepth; imshow("TestMat",testMat); Mat testComponentMat; struct ConnectedComponentConfig config; config.eightWayNeighborhood = true; config.depthDifferenceThreshold = .05f; config.minPixelArea = 10; vector<vector <Point> > testComponents = findConnectedComponents(config, testMat, mask, testComponentMat); displayComponentResults(testComponentMat); displayComponentResults(testComponents, testMat.size()); }
int main() { int Error(0); Error += testVec(); Error += testMat(); return Error; }
*outStream << "FAILURE --> "; } *outStream << "entry[" << i << "," << j << "]:" << " " << testMat(i, j) << " " << num1 << "/" << num2 << " " << absdiff << " " << "<?" << " " << abstol << "\n"; } else { std::istringstream chunkstream(chunk); if (analyticDataType == INTREPID2_UTILS_FRACTION) { chunkstream >> num1; testentry = (ValueType)(num1); } else if (analyticDataType == INTREPID2_UTILS_SCALAR) chunkstream >> testentry; abstol = ( std::fabs(testentry) < reltol ?reltol : std::fabs(reltol*testentry) ); absdiff = std::fabs(testentry - testMat(i, j)); if (absdiff > abstol) { ++err; *outStream << "FAILURE --> "; } *outStream << "entry[" << i << "," << j << "]:" << " " << testMat(i, j) << " " << testentry << " " << absdiff << " " << "<?" << " " << abstol << "\n"; } ++j; } ++i; } // reset format state of std::cout std::cout.copyfmt(oldFormatState);
void testVuFunctions() { MatrixData testMat(10,10,CV_32FC1); MatrixData testMat2(10,10,CV_32FC1); MatrixData buffer(10,10,CV_32FC1); MatrixSize windowSize; windowSize.height = 5; windowSize.width = 5; testMat.setTo(Scalar(1)); testMat.at<float>(5,5) = 0; namedWindow("Before"); imshow("Before",testMat); waitKey(0); vuErodeFloat(&testMat,windowSize, &buffer, &testMat2); namedWindow("After"); namedWindow("Results"); namedWindow("Buffer"); imshow("Before",testMat); imshow("After",testMat); imshow("Results",testMat2); imshow("Buffer",buffer); cout <<"TestMat:\n" << endl; printFloatMatrixToConsole(&testMat); cout << "\n\n" << endl; cout <<"TestMat2\n:" << endl; printFloatMatrixToConsole(&testMat2); cout << "\n\n" << endl; cout << "Matrix Results\n" << testMat2 << endl; waitKey(0); vuDilateFloat(&testMat2,windowSize, &buffer, &testMat); imshow("Before",testMat2); imshow("After",testMat2); imshow("Results",testMat); imshow("Buffer",buffer); cout <<"TestMat:\n" << endl; printFloatMatrixToConsole(&testMat2); cout << "\n\n" << endl; cout <<"TestMat2\n:" << endl; printFloatMatrixToConsole(&testMat); cout << "\n\n" << endl; cout << "Matrix Results\n" << testMat << endl; waitKey(0); testMat.create(10,10,CV_16SC1); testMat2.create(10,10,CV_16SC1); buffer.create(10,10,CV_16SC1); vuErodeInt(&testMat,windowSize, &buffer, &testMat2); MatrixData queryDataIn(5,3,CV_32FC1); MatrixData trainDataIn(10,3,CV_32FC1); MatrixData rows; vector<FeatureMatch> matchResults; int numberOfMatches = 15; matchResults.resize(queryDataIn.rows * numberOfMatches); for(int i = 0; i< trainDataIn.rows;i++) { rows= trainDataIn.row(i); rows.setTo(Scalar(i)); } for(int i = 0; i< queryDataIn.rows;i++) { rows= queryDataIn.row(i); rows.setTo(Scalar(i)); } rows = trainDataIn.row(9); rows.setTo(Scalar(1)); vuTopNMatchesFloat(&queryDataIn, &trainDataIn, numberOfMatches,&matchResults[0]); for(int i = 0; i < queryDataIn.rows; i ++) { cout << "Query Feature vector " << i << ":"<<endl; vuPrintFeatureVectorToConsole(&queryDataIn, i); for(int j = 0; j< numberOfMatches; j++) { vuPrintFeatureMatchToConsole(&matchResults[i*numberOfMatches + j], &queryDataIn, &trainDataIn); } } vector< vector<DMatch > > testMatches; converFeatureMatchToDMatch(&matchResults[0], queryDataIn.rows, numberOfMatches,testMatches); for(int i = 0; i < (int)testMatches.size(); i ++) { cout << "DMatch Query Feature vector " << i << ":"<<endl; for(int j = 0; j< (int)testMatches[i].size(); j++) { printf("\t Dist: %g, queryId %d -> train Id %d\n",testMatches[i][j].distance,testMatches[i][j].queryIdx,testMatches[i][j].trainIdx); } } waitKey(0); }
void VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction(const std::vector<P_V*>& fieldPositions, Q_V& sampleValues) { if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) { *m_env.subDisplayFile() << "Entering VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()" << std::endl; } queso_require_equal_to_msg(( sampleValues.sizeLocal() % fieldPositions.size() ), 0, "input data is not multiple of each other"); unsigned int numberOfImageValuesPerIndex = sampleValues.sizeLocal()/fieldPositions.size(); queso_require_equal_to_msg(numberOfImageValuesPerIndex, m_imageSetPerIndex.vectorSpace().dimLocal(), "invalid input data dimension"); if ((m_savedPositions.size() == 0 ) && (m_savedRvImageSpace == NULL) && (m_savedRvLawExpVector == NULL) && (m_savedRvLawCovMatrix == NULL) && (m_savedRv == NULL)) { // Ok } else if ((m_savedPositions.size() != 0 ) && (m_savedRvImageSpace != NULL) && (m_savedRvLawExpVector != NULL) && (m_savedRvLawCovMatrix != NULL) && (m_savedRv != NULL)) { // Ok } else { queso_error_msg("invalid combination of pointer values"); } unsigned int numberOfPositions = fieldPositions.size(); bool instantiate = true; if (m_savedPositions.size() == numberOfPositions) { bool allPositionsAreEqual = true; for (unsigned int i = 0; i < numberOfPositions; ++i) { queso_require_msg(m_savedPositions[i], "m_savedPositions[i] should not be NULL"); if ((m_savedPositions[i]->sizeLocal() == fieldPositions[i]->sizeLocal()) && (*(m_savedPositions[i]) == *(fieldPositions[i]) )) { // Ok } else { allPositionsAreEqual = false; break; } } // for i instantiate = !allPositionsAreEqual; } if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) { *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()" << ": numberOfPositions = " << numberOfPositions << ", instantiate = " << instantiate << std::endl; } if (instantiate) { delete m_savedRv; delete m_savedRvLawCovMatrix; delete m_savedRvLawExpVector; delete m_savedRvImageSpace; for (unsigned int i = 0; i < m_savedPositions.size(); ++i) { delete m_savedPositions[i]; } m_savedPositions.clear(); // Set m_savedPositions m_savedPositions.resize(numberOfPositions,NULL); for (unsigned int i = 0; i < m_savedPositions.size(); ++i) { m_savedPositions[i] = new P_V(*(fieldPositions[i])); } // Set m_savedRvImageSpace m_savedRvImageSpace = new VectorSpace<Q_V,Q_M>(m_env, "grf_", numberOfPositions*numberOfImageValuesPerIndex, NULL); // Set m_savedRvLawExpVector Q_V tmpVec(m_imageSetPerIndex.vectorSpace().zeroVector()); m_savedRvLawExpVector = new Q_V(m_savedRvImageSpace->zeroVector()); for (unsigned int i = 0; i < numberOfPositions; ++i) { m_meanFunction.compute(*(fieldPositions[i]),NULL,tmpVec,NULL,NULL,NULL); for (unsigned int j = 0; j < numberOfImageValuesPerIndex; ++j) { (*m_savedRvLawExpVector)[i*numberOfImageValuesPerIndex + j] = tmpVec[j]; } } // Set m_savedRvLawCovMatrix Q_M tmpMat(m_imageSetPerIndex.vectorSpace().zeroVector()); m_savedRvLawCovMatrix = new Q_M(m_savedRvImageSpace->zeroVector()); if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) { *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()" << ": m_savedRvLawCovMatrix order = " << m_savedRvLawCovMatrix->numCols() << ", numberOfPositions = " << numberOfPositions << ", tmpMat order = " << tmpMat.numCols() << ", numberOfImageValuesPerIndex = " << numberOfImageValuesPerIndex << std::endl; } for (unsigned int i = 0; i < numberOfPositions; ++i) { for (unsigned int j = 0; j < numberOfPositions; ++j) { m_covarianceFunction.covMatrix(*(fieldPositions[i]),*(fieldPositions[j]),tmpMat); #if 1 Q_M testMat(tmpMat); if (testMat.chol() != 0) { *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()" << ": i = " << i << ", j = " << j << ", *(fieldPositions[i]) = " << *(fieldPositions[i]) << ", *(fieldPositions[j]) = " << *(fieldPositions[j]) << ", tmpMat = " << tmpMat << ", testMat = " << testMat << ", tmpMat is not positive definite" << std::endl; queso_error_msg("tmpMat is not positive definite"); } #endif for (unsigned int k1 = 0; k1 < numberOfImageValuesPerIndex; ++k1) { for (unsigned int k2 = 0; k2 < numberOfImageValuesPerIndex; ++k2) { unsigned int tmpI = i*numberOfImageValuesPerIndex + k1; unsigned int tmpJ = j*numberOfImageValuesPerIndex + k2; (*m_savedRvLawCovMatrix)(tmpI,tmpJ) = tmpMat(k1,k2); if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) { *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()" << ": i = " << i << ", j = " << j << ", k1 = " << k1 << ", k2 = " << k2 << ", tmpI = " << tmpI << ", tmpJ = " << tmpJ << ", *(fieldPositions[i]) = " << *(fieldPositions[i]) << ", *(fieldPositions[j]) = " << *(fieldPositions[j]) << ", (*m_savedRvLawCovMatrix)(tmpI,tmpJ) = " << (*m_savedRvLawCovMatrix)(tmpI,tmpJ) << std::endl; } } } } } // Set m_savedRv m_savedRv = new GaussianVectorRV<Q_V,Q_M>("grf_", *m_savedRvImageSpace, *m_savedRvLawExpVector, *m_savedRvLawCovMatrix); if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) { *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()" << ": just instantiated Gaussian RV" << "\n *m_savedRvLawExpVector = " << *m_savedRvLawExpVector << "\n *m_savedRvLawCovMatrix = " << *m_savedRvLawCovMatrix << std::endl; for (unsigned int i = 0; i < numberOfPositions; ++i) { *m_env.subDisplayFile() << " *(m_savedPositions[" << i << "]) = " << *(m_savedPositions[i]) << std::endl; } } } // if (instantiate) // Generate sample function if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) { *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()" << ": about to realize sample values" << std::endl; } m_savedRv->realizer().realization(sampleValues); if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) { *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()" << ": just realized sample values" << std::endl; } if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) { *m_env.subDisplayFile() << "Leaving VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()" << std::endl; } return; }