void DepthSampler::sample(cv::Mat source, std::vector<cv::Mat> layers, std::vector<cv::Point2i>& out){ cv::Mat localSource; std::vector<cv::Mat> tempLayers; source.convertTo(localSource,CV_32FC3); cv::split(localSource, tempLayers); cv::Mat dM(localSource.rows,localSource.cols, CV_32FC1); dM=(tempLayers[1]*256) + tempLayers[2]; out.resize(0); int nLayers=layers.size(); float localStep=minInd+(step*(double)nLayers); for ( std::vector<cv::Mat>::iterator it= layers.begin(); it != layers.end(); it++){ for (int xIm=0; xIm< source.cols; xIm+=(int)localStep) { for (int yIm=0; yIm<source.rows ; yIm+=(int)localStep) { if ((int)it->at<char>(yIm,xIm) != 0){ float d=dM.at<float>(yIm,xIm); if ((d != 0)&&(d<maxDistance)){ out.push_back(cv::Point2i(xIm,yIm)); } } } } localStep=localStep-step; } }
int main() { typedef Kokkos::DefaultNode::DefaultNodeType Node; typedef KokkosExamples::DummySparseKernel<Node> SparseOps; typedef Kokkos::CrsGraph < int,Node,SparseOps> Graph; typedef Kokkos::CrsMatrix<double,int,Node,SparseOps> DoubleMat; typedef Kokkos::CrsMatrix< float,int,Node,SparseOps> FloatMat; typedef Kokkos::MultiVector<double,Node> DoubleVec; typedef Kokkos::MultiVector<float,Node> FloatVec; std::cout << "Note, this class doesn't actually do anything. We are only testing that it compiles." << std::endl; // get a pointer to the default node Teuchos::RCP<Node> node = Kokkos::DefaultNode::getDefaultNode(); // create the graph G const size_t numRows = 5; Graph G(numRows,node); // create a double-valued matrix dM using the graph G DoubleMat dM(G); // create a double-valued sparse kernel using the rebind functionality SparseOps::rebind<double>::other doubleKernel(node); // initialize it with G and dM doubleKernel.initializeStructure(G); doubleKernel.initializeValues(dM); // create double-valued vectors and initialize them DoubleVec dx(node), dy(node); // test the sparse kernel operator interfaces doubleKernel.multiply( Teuchos::NO_TRANS, 1.0, dx, dy); doubleKernel.multiply( Teuchos::NO_TRANS, 1.0, dx, 1.0, dy); doubleKernel.solve( Teuchos::NO_TRANS, Teuchos::UPPER_TRI, Teuchos::UNIT_DIAG, dy, dx); // create a float-valued matrix fM using the graph G FloatMat fM(G); // create a double-valued sparse kernel using the rebind functionality SparseOps::rebind<float>::other floatKernel(node); // initialize it with G and fM floatKernel.initializeStructure(G); floatKernel.initializeValues(fM); // create float-valued vectors and initialize them FloatVec fx(node), fy(node); // test the sparse kernel operator interfaces floatKernel.multiply( Teuchos::NO_TRANS, 1.0f, fx, fy); floatKernel.multiply( Teuchos::NO_TRANS, 1.0f, fx, 1.0f, fy); floatKernel.solve( Teuchos::NO_TRANS, Teuchos::UPPER_TRI, Teuchos::UNIT_DIAG, fy, fx); std::cout << "End Result: TEST PASSED" << std::endl; return 0; }
//devuelve en out, los nBins layers con la profundidad discretizada hasta maxDistance void DepthSampler::calculateLayers(cv::Mat source, std::vector<cv::Mat>& layers){ std::vector<cv::Mat> imgLayers; cv::Mat localSource; layers.resize(0); source.convertTo(localSource,CV_32FC3); cv::split(localSource, imgLayers); cv::Mat dM(localSource.rows,localSource.cols, CV_32FC1); dM=(imgLayers[1]*256) + imgLayers[2]; cv::Mat unosc1(localSource.rows, localSource.cols, CV_8UC1, cv::Scalar(255)); cv::Mat zerosc1(localSource.rows, localSource.cols, CV_8UC1, cv::Scalar(0)); IceUtil::Time t=IceUtil::Time::now(); for (int i=0; i<nBins; i++){ cv::Mat localMask; cv::threshold(dM,localMask,(maxDistance/nBins)*(i+1), 255, CV_THRESH_BINARY_INV); localMask.convertTo(localMask,CV_8UC1); layers.push_back(localMask&unosc1); zerosc1.copyTo(unosc1,localMask); } }
//compara el muestreo normal con el muestreo por capas, empezando desde minInd, e incrementando step en cada capa, //desde la mas lejana hasta la mas cercana. void DepthSampler::evalSample(cv::Mat source, std::vector<cv::Mat> layers, int samplingRate, cv::Mat &outSNormal, cv::Mat &outSLayers){ cv::Mat imgNormalSample=cv::Mat(source.rows,source.cols, CV_8UC1, cv::Scalar(0)); cv::Mat imgNlayerSample=cv::Mat(source.rows,source.cols, CV_8UC1, cv::Scalar(0));; std::vector<int> normalSample; int outlierNormal=0; int ourlierSample=0; std::vector<int> layerSample; int nLayers=layers.size(); normalSample.resize(nLayers); layerSample.resize(nLayers); cv::Mat localSource; std::vector<cv::Mat> tempLayers; source.convertTo(localSource,CV_32FC3); cv::split(localSource, tempLayers); cv::Mat dM(localSource.rows,localSource.cols, CV_32FC1); dM=(tempLayers[1]*256) + tempLayers[2]; for (int i=0; i< nLayers;i++){ normalSample[i]=0; layerSample[i]=0; } IceUtil::Time n=IceUtil::Time::now(); for (int xIm=0; xIm< source.cols; xIm+=samplingRate) { for (int yIm=0; yIm<source.rows ; yIm+=samplingRate) { float d=dM.at<float>(yIm,xIm); if (d != 0){ imgNormalSample.at<char>(yIm,xIm)=(char)255; double pos= d/maxDistance; if (pos>1){ outlierNormal++; } else{ normalSample[floor(pos*nLayers)]=normalSample[floor(pos*nLayers)]++; } } } } std::cout << "Time for normal sampling: " << IceUtil::Time::now().toMilliSeconds() - n.toMilliSeconds() << std::endl; std::cout << "Layer sampling Size : " << std::accumulate(normalSample.begin(), normalSample.end(), 0) << ", result:" << std::endl; for (std::vector<int>::iterator it= normalSample.begin(); it!= normalSample.end(); it++){ std::cout << *it << std::endl; } n=IceUtil::Time::now(); float localStep=minInd+(step*nLayers); for ( std::vector<cv::Mat>::iterator it= layers.begin(); it != layers.end(); it++){ std::cout << "step: " << (int)localStep << std::endl; for (int xIm=0; xIm< source.cols; xIm+=(int)localStep) { for (int yIm=0; yIm<source.rows ; yIm+=(int)localStep) { if ((int)it->at<char>(yIm,xIm) != 0){ float d=dM.at<float>(yIm,xIm); if (d != 0){ imgNlayerSample.at<char>(yIm,xIm)=(char)255; double pos= d/maxDistance; if (pos>1){ outlierNormal++; } else{ layerSample[floor(pos*nLayers)]=layerSample[floor(pos*nLayers)]++; } } } } } localStep=localStep-step; } std::cout << "Time for layers sampling: " << IceUtil::Time::now().toMilliSeconds() - n.toMilliSeconds() << std::endl; std::cout << "Layer sampling Size : " << std::accumulate(layerSample.begin(), layerSample.end(), 0) << ", result:" << std::endl; for (std::vector<int>::iterator it= layerSample.begin(); it!= layerSample.end(); it++){ std::cout << *it << std::endl; } /*cv::imshow("normal", imgNormalSample); cv::imshow("layers", imgNlayerSample); cv::waitKey(0);*/ imgNormalSample.copyTo(outSNormal); imgNlayerSample.copyTo(outSLayers); }