码迷,mamicode.com
首页 > 移动开发 > 详细

OpenCV Tutorials —— Remapping

时间:2014-11-21 18:24:17      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   io   ar   os   sp   for   strong   

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

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:

    bubuko.com,布布扣

    for all pairs bubuko.com,布布扣 such that: bubuko.com,布布扣 and bubuko.com,布布扣  缩放一半并将图像置于窗口中心

  2. 2,Turn the image upside down: bubuko.com,布布扣

  3. 3,Reflect the image from left to right: bubuko.com,布布扣

  4. 4,Combination of b and c: bubuko.com,布布扣

 

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,用来映射像素点 ~~

OpenCV Tutorials —— Remapping

标签:des   style   http   io   ar   os   sp   for   strong   

原文地址:http://www.cnblogs.com/sprint1989/p/4113250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!