23. Pixel remapping

1. Main content

  • Introduction to pixel remapping
  • API introduction
  • Code demo

2. Introduction to pixel remapping

  • To put it simply, each pixel in the input image is mapped to the corresponding position of another image according to certain rules to form a new image.
    Insert picture description here
    g (x, y) is the image after remapping, h (x, y) is the function function, and f is the source image.
    Insert picture description here
    Suppose there is a mapping function

Insert picture description here
Insert picture description here

3. API introduction cv :: remap

Remap(
InputArray src,// 输入图像,需要单通道8位或者浮点类型的图像
OutputArray dst,// 输出图像,需和原图形一样的尺寸和类型
InputArray  map1,
//它有两种可能表示的对象:(1)表示点(x,y)的第一个映射;(2)表示CV_16SC2,CV_32FC1等
InputArray map2,
//它有两种可能表示的对象:(1)若map1表示点(x,y)时,这个参数不代表任何值;(2)表示CV_16UC1,CV_32FC1类型的Y值
int interpolation,// 选择的插值方法,常见线性插值,可选择立方等
int borderMode,//默认BORDER_CONSTANT,边缘填充方式
const Scalar borderValue// color,填充颜色,默认黑色
)

There are four interpolation methods:
(1) INTER_NEAREST-nearest neighbor interpolation
(2) INTER_LINEAR-bilinear interpolation (default)
(3) INTER_CUBIC-double triple spline interpolation (default)
(4) INTER_LANCZOS —Lanczos interpolation (default)

  • Reduce by half (this time use a linear difference method)
    Insert picture description here
  • Y direction swap
    Insert picture description here
  • X direction swap
    Insert picture description here
  • XY direction swapped at the same time
    Insert picture description here

4. Demo code

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
Mat src, dst, map_x, map_y;
const char* OUTPUT_TITLE = "remap demo";
int index = 0;
void update_map(void);
int main(int argc, char** argv) {
	src = imread("D:/vcprojects/images/test.png");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	char input_win[] = "input image";
	namedWindow(input_win, CV_WINDOW_AUTOSIZE);
	namedWindow(OUTPUT_TITLE, CV_WINDOW_AUTOSIZE);
	imshow(input_win, src);

	map_x.create(src.size(), CV_32FC1);
	map_y.create(src.size(), CV_32FC1);

	int c = 0;
	while (true) {
		c = waitKey(500);
		if ((char)c == 27) {
			break;
		}
		index = c % 4;
		update_map();//被调用的代码映射函数
		remap(src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 255, 255));
		imshow(OUTPUT_TITLE, dst);
	}

	return 0;
}

void update_map(void) {
	for (int row = 0; row < src.rows; row++) {
		for (int col = 0; col < src.cols; col++) {
			switch (index) {
			case 0:
				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));
					map_y.at<float>(row, col) = 2 * (row - (src.rows*0.25));
				}
				else {
					map_x.at<float>(row, col) = 0;
					map_y.at<float>(row, col) = 0;
				}
				break;
			case 1:
				map_x.at<float>(row, col) = (src.cols - col - 1);
				map_y.at<float>(row, col) = row;
				break;
			case 2:
				map_x.at<float>(row, col) = col;
				map_y.at<float>(row, col) = (src.rows - row - 1);
				break;
			case 3:
				map_x.at<float>(row, col) = (src.cols - col - 1);
				map_y.at<float>(row, col) = (src.rows - row - 1);
				break;
			}

		}
	}
}

The return value of the waitKey function is the ASCIOI code of the current key

5. Extracurricular expansion

Note: A bloody crime caused by waitkey in opencv [OpenCV] OpenCV input processing (mouse event_keyboard event_slider
event)

(Note This function is the only method in HighGUI that can fetch and handle events) It is equal to the default operation of this function.

Image Watch: View images in memory in the Visual Studio debugger

Published 66 original articles · won praise 53 · views 6812

Guess you like

Origin blog.csdn.net/qq_43367829/article/details/105424623