标签:
一、前言
图是一种重要的数据结构,本文主要表示图像的无向图。所谓无向图是指,图的节点间通过没有方向的边连接。
无向图的表示:
1 void SparseGraphic::KNNSparseGraphics(const QString fileName, const QPoint curPos, 2 const int K, QVector<QPoint> &resPoint, const int flag) 3 { 4 if(curPos.x()<0 || curPos.y()<0) 5 return; 6 cv::Mat Img = GDALOpenCV::GDAL2Mat(fileName); 7 int row = Img.rows; 8 int col = Img.cols; 9 if(curPos.x()>=col || curPos.y() >= row) 10 return; 11 if(flag != 0 && flag != 1) 12 return; 13 cv::Mat imgVec = Img.reshape(1,row*col); 14 cv::transpose(imgVec,imgVec); 15 int curPosVec = curPos.y()*col + curPos.x(); 16 cv::Mat Dict; 17 if(curPosVec != 0) 18 { 19 cv::Mat Dict1 = imgVec.colRange(0,curPosVec-1); 20 cv::Mat Dict1_T = Dict1.t(); 21 cv::Mat Dict2 = imgVec.colRange(curPosVec,imgVec.cols); 22 cv::Mat Dict2_T = Dict2.t(); 23 Dict1_T.push_back(Dict2_T); 24 cv::Mat Dict_T = Dict1_T.clone(); 25 Dict = Dict_T.t(); 26 Dict = Dict.clone(); 27 Dict1.release(); 28 Dict2.release(); 29 Dict_T.release(); 30 Dict1_T.release(); 31 Dict2_T.release(); 32 }else 33 { 34 cv::Mat Dict1 = imgVec.colRange(1,imgVec.cols); 35 Dict = Dict1.clone(); 36 Dict1.release(); 37 } 38 cv::Mat curPosImgVec = imgVec.colRange(curPosVec-1,curPosVec); 39 QVector<int> index; 40 for(int i = 0;i<row*col;i++) 41 index.push_back(i); 42 index.removeAt(curPosVec); 43 44 if(flag == 0) 45 { 46 cv::Mat tmpCurPosImgVec = cv::repeat(curPosImgVec,1,Dict.cols); 47 cv::Mat subMat = Dict - tmpCurPosImgVec; 48 subMat = subMat.mul(subMat); 49 cv::sqrt(subMat,subMat); 50 cv::reduce(subMat,subMat,0,CV_REDUCE_SUM); 51 QuickSort(subMat,index,0,row*col-2); 52 for(int i = 0;i<K;i++) 53 { 54 int r = index[i]/col; 55 int c = index[i]%col; 56 QPoint mPos; 57 mPos.setX(c); 58 mPos.setY(r); 59 resPoint.push_back(mPos); 60 } 61 }else 62 { 63 QVector<int> tmpIndex; 64 cv::Mat A = ormpSparseRepresentation::ompSparseL2(Dict,curPosImgVec,tmpIndex,K); 65 for(int i = 0;i<K;i++) 66 { 67 int r = index[tmpIndex[i]]/col; 68 int c = index[tmpIndex[i]]%col; 69 QPoint mPos; 70 mPos.setX(c); 71 mPos.setY(r); 72 resPoint.push_back(mPos); 73 } 74 } 75 }
三、显示
标签:
原文地址:http://www.cnblogs.com/zyore2013/p/4776867.html