一、前言

图是一种重要的数据结构,本文主要表示图像的无向图。所谓无向图是指,图的节点间通过没有方向的边连接。

无向图的表示:

无向图G=<V,E>,其中:
1.V是非空集合,称为顶点集。
2.E是V中元素构成的无序二元组的集合,称为边集。
对于图像来说,每一个像素都可以看做是一个节点,根据具体节点连接选择方式的不同,可以分为KNN构图和Sparse构图等等。
所谓KNN构图是指,每个像素的节点都与图像中与改点距离最小的K个点连接,连接的值可以通过最小二乘重构计算。
Sparse构图也是一样,主要是将每个像素在其余像素构成的字典中位置原子连接。具体算法可以参考相关文献。
 
二、实现
本节主要实现单个点的图连接情况。稀疏构图采用的是OMP算法解算的。
主要代码函数如下:
 void SparseGraphic::KNNSparseGraphics(const QString fileName, const QPoint curPos,
const int K, QVector<QPoint> &resPoint, const int flag)
{
if(curPos.x()< || curPos.y()<)
return;
cv::Mat Img = GDALOpenCV::GDAL2Mat(fileName);
int row = Img.rows;
int col = Img.cols;
if(curPos.x()>=col || curPos.y() >= row)
return;
if(flag != && flag != )
return;
cv::Mat imgVec = Img.reshape(,row*col);
cv::transpose(imgVec,imgVec);
int curPosVec = curPos.y()*col + curPos.x();
cv::Mat Dict;
if(curPosVec != )
{
cv::Mat Dict1 = imgVec.colRange(,curPosVec-);
cv::Mat Dict1_T = Dict1.t();
cv::Mat Dict2 = imgVec.colRange(curPosVec,imgVec.cols);
cv::Mat Dict2_T = Dict2.t();
Dict1_T.push_back(Dict2_T);
cv::Mat Dict_T = Dict1_T.clone();
Dict = Dict_T.t();
Dict = Dict.clone();
Dict1.release();
Dict2.release();
Dict_T.release();
Dict1_T.release();
Dict2_T.release();
}else
{
cv::Mat Dict1 = imgVec.colRange(,imgVec.cols);
Dict = Dict1.clone();
Dict1.release();
}
cv::Mat curPosImgVec = imgVec.colRange(curPosVec-,curPosVec);
QVector<int> index;
for(int i = ;i<row*col;i++)
index.push_back(i);
index.removeAt(curPosVec); if(flag == )
{
cv::Mat tmpCurPosImgVec = cv::repeat(curPosImgVec,,Dict.cols);
cv::Mat subMat = Dict - tmpCurPosImgVec;
subMat = subMat.mul(subMat);
cv::sqrt(subMat,subMat);
cv::reduce(subMat,subMat,,CV_REDUCE_SUM);
QuickSort(subMat,index,,row*col-);
for(int i = ;i<K;i++)
{
int r = index[i]/col;
int c = index[i]%col;
QPoint mPos;
mPos.setX(c);
mPos.setY(r);
resPoint.push_back(mPos);
}
}else
{
QVector<int> tmpIndex;
cv::Mat A = ormpSparseRepresentation::ompSparseL2(Dict,curPosImgVec,tmpIndex,K);
for(int i = ;i<K;i++)
{
int r = index[tmpIndex[i]]/col;
int c = index[tmpIndex[i]]%col;
QPoint mPos;
mPos.setX(c);
mPos.setY(r);
resPoint.push_back(mPos);
}
}
}

其中:排序与omp算法的代码见文档8和文档7

三、显示

9. KNN和Sparse构图-LMLPHP

05-11 11:33