zoukankan      html  css  js  c++  java
  • opencv-图像金字塔

    图像金字塔

    目标

    原理摘自:http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/pyramids/pyramids.html

    本文档尝试解答例如以下问题:

    • 怎样使用OpenCV函数 pyrUp 和 pyrDown 对图像进行向上和向下採样。

    原理

    Note

     

    下面内容来自于Bradski和Kaehler的大作: Learning OpenCV 。

    • 当我们须要将图像转换到还有一个尺寸的时候。 有两种可能:
      1. 放大 图像 或者
      2. 缩小 图像。
    • 虽然OpenCV 几何变换 部分提供了一个真正意义上的图像缩放函数(resize, 在以后的教程中会学到),只是在本篇我们首先学习一下使用 图像金字塔 来做图像缩放, 图像金字塔是视觉运用中广泛採用的一项技术。

    图像金字塔

    • 一个图像金字塔是一系列图像的集合 - 全部图像来源于同一张原始图像 - 通过梯次向下採样获得,直到达到某个终止条件才停止採样。
    • 有两种类型的图像金字塔经常出如今文献和应用中:
      • 高斯金字塔(Gaussian pyramid): 用来向下採样
      • 拉普拉斯金字塔(Laplacian pyramid): 用来从金字塔低层图像重建上层未採样图像
    • 在这篇文档中我们将使用 高斯金字塔 。

    高斯金字塔

    • 想想金字塔为一层一层的图像,层级越高,图像越小。

      Pyramid figure
    • 每一层都按从下到上的次序编号, 层级 (i+1) (表示为 G_{i+1} 尺寸小于层级 i (G_{i}))。

    • 为了获取层级为 (i+1) 的金字塔图像。我们採用例如以下方法:

      • 将 G_{i} 与高斯内核卷积:

        frac{1}{16} egin{bmatrix} 1 & 4 & 6 & 4 & 1  \ 4 & 16 & 24 & 16 & 4  \ 6 & 24 & 36 & 24 & 6  \ 4 & 16 & 24 & 16 & 4  \ 1 & 4 & 6 & 4 & 1 end{bmatrix}

      • 将全部偶数行和列去除。

    • 显而易见,结果图像仅仅有原图的四分之中的一个。通过对输入图像 G_{0} (原始图像) 不停迭代以上步骤就会得到整个金字塔。

    • 以上过程描写叙述了对图像的向下採样,假设将图像变大呢?

      :

      • 首先,将图像在每一个方向扩大为原来的两倍。新增的行和列以0填充(0)
      • 使用先前相同的内核(乘以4)与放大后的图像卷积。获得 “新增像素” 的近似值。
    • 这两个步骤(向下和向上採样) 分别通过OpenCV函数 pyrUp 和 pyrDown 实现, 我们将会在以下的演示样例中演示怎样使用这两个函数。

    • 实例:(放大


    • 缩小


    • // ConsoleApplication3_6_23.cpp : Defines the entry point for the console application.
      //
      
      #include "stdafx.h"
      #include<opencv2/opencv.hpp>
      #include<iostream>
      using namespace std;
      using namespace cv;
      
      
      Mat src,dst;
      int model = 0;
      const int max_model = 1;
      char* windowName = "Demo";
      
      void Image_pro(int,void*);
      int _tmain(int argc, _TCHAR* argv[])
      {
      	src = imread("hwl.jpg");
      	namedWindow("原图",CV_WINDOW_AUTOSIZE);
      	imshow("原图",src);
      	if(!src.data)
      		return -1;
      	namedWindow(windowName,CV_WINDOW_AUTOSIZE);
      	createTrackbar("0-放大 
       1-缩小",
      		windowName,&model,max_model,Image_pro);
      
      	Image_pro(0,0);
      	waitKey(0);
      	return 0;
      }
      void Image_pro(int,void*){
      
      	if(0 == model)
      	{
      		pyrUp(src,dst,Size(src.cols * 2,src.rows * 2));
      	}else{
      		pyrDown(src,dst,Size(src.cols / 2,src.rows / 2));
      	}
      	imshow(windowName,dst);
      }
      
      


    Note

     

    我们向下採样缩小图像的时候, 我们实际上 丢失 了一些信息。

  • 相关阅读:
    013.ES6 -对象字面量增强型写法
    012. ES6
    011. ES6 语法
    10. 9. Vue 计算属性的setter和getter 以及 计算属性的缓存讲解
    4. Spring MVC 数据响应方式
    3. SpringMVC 组件解析
    9. Vue 计算属性
    【洛谷 2984】给巧克力
    【洛谷 1821】捉迷藏 Hide and Seek
    【洛谷 1821】银牛派对Silver Cow Party
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6952382.html
Copyright © 2011-2022 走看看