• 9. KNN和Sparse构图


    一、前言

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

    无向图的表示:

    无向图G=<V,E>,其中:
    1.V是非空集合,称为顶点集。
    2.E是V中元素构成的无序二元组的集合,称为边集。
    对于图像来说,每一个像素都可以看做是一个节点,根据具体节点连接选择方式的不同,可以分为KNN构图和Sparse构图等等。
    所谓KNN构图是指,每个像素的节点都与图像中与改点距离最小的K个点连接,连接的值可以通过最小二乘重构计算。
    Sparse构图也是一样,主要是将每个像素在其余像素构成的字典中位置原子连接。具体算法可以参考相关文献。
     
    二、实现
    本节主要实现单个点的图连接情况。稀疏构图采用的是OMP算法解算的。
    主要代码函数如下:
     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 }

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

    三、显示

  • 相关阅读:
    python--tkinter桌面编程开发--记事本
    Python--面向对象编程
    Python--面向对象编程--时钟实例开发
    Python学习笔记--2--面向对象编程
    Python学习笔记--1
    epoll聊天室的实现
    操作系统--虚拟存储器
    操作系统--存储器管理
    操作系统--分页存储管理中逻辑地址转换为物理地址
    操作系统--处理机调度与死锁
  • 原文地址:https://www.cnblogs.com/zyore2013/p/4776867.html
Copyright © 2020-2023  润新知