重映射:
把一个图像中一个位置的像素放置到另一个图片指定位置的过程.
为了完成映射过程, 有必要获得一些插值为非整数像素坐标,因为源图像与目标图像的像素坐标不是一一对应的.
简单的说就是改变图片的位置(左,右,上,下,颠倒)。程序比较简单,主要是x,y坐标的重映射,以及函数remap()的使用
void remap(InputArray src, OutputArray dst, InputArray map1, InputArray map2, intinterpolation, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
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 if map1 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() ). When borderMode=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.
The function remap transforms the source image using the specified map:
示例程序033--重映射
代码及部分注释:
// 041 重映射.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
Mat src, dst;
Mat map_x, map_y;
char* remap_window = "Remap demo";
int ind = 0;
///更新映射矩阵参数的函数声明
void update_map();
int main( int argc, char** argv )
{
src = imread( "Lena.jpg");
///创建目标图像和两个映射矩阵.( x 和 y )
dst.create( src.size(), src.type() );
map_x.create( src.size(), CV_32FC1 ); //代表目标图像的某像素的x坐标
map_y.create( src.size(), CV_32FC1 ); //代表目标图像的某像素的y坐标
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();
//对源图像进行重映射处理
remap( src, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0,0, 0) );
imshow( remap_window, dst );
}
return 0;
}
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++;
}
运行结果:
示例程序033--重映射示例程序033--重映射示例程序033--重映射示例程序033--重映射