OpenCV Tutorials —— Remapping

重定向,也就是把图像中的像素从一个地方对应到另一个地方

To accomplish the mapping process, it might be necessary to do some interpolation for non-integer pixel locations, since there will not always be a one-to-one-pixel correspondence between source and destination images.

会涉及到插值 ~~ 可能像素点不是一一对应

 

程序中的四种处理方式

  1. 1,Reduce the picture to half its size and will display it in the middle:

    for all pairs such that: and   缩放一半并将图像置于窗口中心

  2. 2,Turn the image upside down:
  3. 3,Reflect the image from left to right:
  4. 4,Combination of b and c:

 

remap

void remap(InputArray src, OutputArray dst, InputArray map1, InputArray map2, int interpolation, intborderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())

Parameters:

  • src – Source image.
  • dst – Destination image. It has the same size as map1 and the same type as src .
  • map1 – The first map of either (x,y) points or just x values having the type CV_16SC2 , CV_32FC1, or CV_32FC2 . See convertMaps() for details on converting a floating point representation to fixed-point for speed.
  • map2 – The second map of y values having the type CV_16UC1 , CV_32FC1 , or none (empty map ifmap1 is (x,y) points), respectively.
  • interpolation – Interpolation method (see resize() ). The method INTER_AREA is not supported by this function.
  • borderMode – Pixel extrapolation method (see borderInterpolate() ). WhenborderMode=BORDER_TRANSPARENT , it means that the pixels in the destination image that corresponds to the “outliers” in the source image are not modified by the function.
  • borderValue – Value used in case of a constant border. By default, it is 0.

 

 

Code

#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
 #include "opencv2/imgproc/imgproc.hpp"
 #include <iostream>
 #include <stdio.h>

 using namespace cv;

 /// Global variables
 Mat src, dst;
 Mat map_x, map_y;
 char* remap_window = "Remap demo";
 int ind = 0;

 /// Function Headers
 void update_map( void );

 /**
 * @function main
 */
 int main( int argc, char** argv )
 {
   /// Load the image
   src = imread( "zanxin.jpg", 1 );

  /// Create dst, map_x and map_y with the same size as src:
  dst.create( src.size(), src.type() );
  map_x.create( src.size(), CV_32FC1 );
  map_y.create( src.size(), CV_32FC1 );

  /// Create window
  namedWindow( remap_window, CV_WINDOW_AUTOSIZE );

  /// Loop
  while( true )
  {
    /// Each 1 sec. Press ESC to exit the program
    int c = waitKey( 1000 );

    if( (char)c == 27 )
      { break; }

    /// Update map_x & map_y. Then apply remap
    update_map();
    remap( src, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0,0, 0) );

    /// Display results
    imshow( remap_window, dst );
  }
  return 0;
 }

 /**
 * @function update_map
 * @brief Fill the map_x and map_y matrices with 4 types of mappings
 */
 void update_map( void )
 {
   ind = ind%4;

   for( int j = 0; j < src.rows; j++ )
   { for( int i = 0; i < src.cols; i++ )
       {
         switch( ind )
         {
           case 0:
             if( i > src.cols*0.25 && i < src.cols*0.75 && j > src.rows*0.25 && j < src.rows*0.75 )
               {
                 map_x.at<float>(j,i) = 2*( i - src.cols*0.25 ) + 0.5 ;
                 map_y.at<float>(j,i) = 2*( j - src.rows*0.25 ) + 0.5 ;
                }
             else
               { map_x.at<float>(j,i) = 0 ;
                 map_y.at<float>(j,i) = 0 ;
               }
                 break;
           case 1:
                 map_x.at<float>(j,i) = i ;
                 map_y.at<float>(j,i) = src.rows - j ;
                 break;
           case 2:
                 map_x.at<float>(j,i) = src.cols - i ;
                 map_y.at<float>(j,i) = j ;
                 break;
           case 3:
                 map_x.at<float>(j,i) = src.cols - i ;
                 map_y.at<float>(j,i) = src.rows - j ;
                 break;
         } // end of switch
       }
    }
  ind++;
}

 

重点是构造 x,y坐标的map,用来映射像素点 ~~

时间: 2024-10-07 02:12:48

OpenCV Tutorials —— Remapping的相关文章

学习opencv tutorials

1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

OpenCV Tutorials &mdash;&mdash; Histogram Equalization

直方图均衡化 -- 其潜在的数学原理是一个分布(输入的亮度直方图)被映射到另一个分布 其目的是拉伸原始图像直方图,增强其对比度   To accomplish the equalization effect, the remapping should be the cumulative distribution function (cdf) (more details, refer to Learning OpenCV). For the histogram , its cumulative d

OpenCV Tutorials &mdash;&mdash; Camera calibration With OpenCV

获取摄像机参数是为了来处理图像失真或者实现图像度量 ~~ Unfortunately, this cheapness comes with its price: significant distortion. Luckily, these are constants and with a calibration and some remapping we can correct this. Furthermore, with calibration you may also determine

OpenCV Tutorials &mdash;&mdash; Hough Line Transform

霍夫直线变换 -- 用于检测图像中的直线 利用图像空间和Hough参数空间的点--直线对偶性,把图像空间中的检测问题转换到参数空间,通过在参数空间进行简单的累加统计,然后在Hough参数空间中寻找累加器峰值的方法检测直线 Standard and Probabilistic Hough Line Transform OpenCV implements two kind of Hough Line Transforms: The Standard Hough Transform It consis

OpenCV Tutorials &mdash;&mdash; Feature Matching with FLANN

Extractors of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch between different algorithms solving the same problem.   DescriptorExtractor::compute Computes the descriptors for a set of keypoints

OpenCV Tutorials &mdash;&mdash; Creating a video with OpenCV

写video 需要用到 VideoWriter  视频文件可看作一个容器 视频的类型由视频文件的后缀名来指定   Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you ca

OpenCV Tutorials &mdash;&mdash; Image Moments

图像矩 Moments moments(InputArray array, bool binaryImage=false ) Parameters: array – Raster image (single-channel, 8-bit or floating-point 2D array) or an array ( or ) of 2D points (Point or Point2f ). binaryImage – If it is true, all non-zero image pi

OpenCV Tutorials &mdash;&mdash; Hough Circle Transform

Hough 圆变换 和 Hough 直线变换原理相同,只是参数空间不同 : In the line detection case, a line was defined by two parameters . In the circle case, we need three parameters to define a circle: where define the center position (gree point) and is the radius, which allows us

OpenCV Tutorials &mdash;&mdash; Sobel Derivatives

图像边缘 -- 像素灰度值变换剧烈的点 You can easily notice that in an edge, the pixel intensity changes in a notorious way. A good way to expresschanges is by using derivatives. A high change in gradient indicates a major change in the image.   To be more graphical,