PPC的C/C++和人工智能学习笔记
每一篇学习笔记,都只是为了更好地掌握和理解

数字图像处理与opencv(19)-像素重映射

本次学习:openCV像素重映射,用来反转图像、镜面图像、缩放图像。

remap的重点在于建立两张映射表:

两个方向的映射表,里面存放的内容的含义是:remap的时候,我从这2张表中取坐标,得到的值分别对应原图的坐标,然后把原图的像素值填充过来。

 

remap函数:

void remap( InputArray src, OutputArray dst,

InputArray map1, InputArray map2,

int interpolation, int borderMode = BORDER_CONSTANT,

const Scalar& borderValue = Scalar());

 

参数:

map1–X映射表,map2–Y映射表,只能是 CV_32FC1或者CV_32FC2

int interpolation, 选线性插值 INTER_LINEAR

int borderMode = BORDER_CONSTANT, 边缘填充方式(默认0)

Scalar& borderValue,填充的颜色Scalar(0,0,255)类似这样写

//像素重映射

#include <opencv2/opencv.hpp>
using namespace cv;

int main() {
 Mat src, d1, d2, d3, d4, map_x, map_y;
 src = imread("1.jpg");
 if (src.empty()) return -1;
 imshow("src", src);
 //1.缩小1倍
 map_x.create(src.size(), CV_32FC1);
 map_y.create(src.size(), CV_32FC1);
 for (int row = 0; row < src.rows; ++row) {
 for (int col = 0; col < src.cols; ++col) {
 if (col >= (src.cols*0.25) && col < (src.cols*0.75) 
 && row >= (src.rows*0.25) && row < (src.rows*0.75)) {
 map_x.at<float>(row, col) = 2 * (col - src.cols*0.25 ) + 0.5;
 map_y.at<float>(row, col) = 2 * (row - src.rows*0.25 ) + 0.5;
 }
 else {
 map_x.at<float>(row, col) = 0;
 map_y.at<float>(row, col) = 0;
 }
 }
 }
 remap(src, d1, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT,Scalar(0,0,255));
 imshow("d1", d1);
 //2.左右交换
 for (int row = 0; row < src.rows; ++row) {
 for (int col = 0; col < src.cols; ++col) {
 map_x.at<float>(row, col) = src.cols - col -1;
 map_y.at<float>(row, col) = row;
 }
 }
 remap(src,d2, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 255));
 imshow("d2", d2);
 //3.上下交换
 for (int row = 0; row < src.rows; ++row) {
 for (int col = 0; col < src.cols; ++col) {
 map_x.at<float>(row, col) = col;
 map_y.at<float>(row, col) = src.rows - row -1;
 }
 }
 remap(src, d3, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 255));
 imshow("d3", d3);
 //4.左右上下都交换
 for (int row = 0; row < src.rows; ++row) {
 for (int col = 0; col < src.cols; ++col) {
 map_x.at<float>(row, col) = src.cols - col - 1;
 map_y.at<float>(row, col) = src.rows - row - 1;
 }
 }
 remap(src, d4, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 255));
 imshow("d4", d4);

 waitKey(0);
 return 0;
}

(2017-03-30 www.vsppc.com)

学习笔记未经允许不得转载:PPC的C/C++和人工智能学习笔记 » 数字图像处理与opencv(19)-像素重映射

分享到:更多 ()

评论 抢沙发

评论前必须登录!