1. 程式人生 > >OpenCV3之——重對映remap()

OpenCV3之——重對映remap()

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

int main() {
	Mat srcImage, dstImage;
	Mat map_x, map_y;
	srcImage = imread("1.jpg");
	if (!srcImage.data) {
		printf("讀取圖片錯誤\n");
		return false;
	}
	imshow("原始圖", srcImage);
	dstImage.create(srcImage.size(), srcImage.type());
	map_x.create(srcImage.size(), CV_32FC1);
	map_y.create(srcImage.size(), CV_32FC1);
	for (int j = 0; j < srcImage.rows; j++) {
		for (int i = 0; i < srcImage.cols; i++) {
			//改變map_x、map_y的值
			map_x.at<float>(j, i) = static_cast<float>(srcImage.cols - i);
			map_y.at<float>(j, i) = static_cast<float>(j);
		}
	}

	//重對映
	remap(srcImage, dstImage, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));

	//顯示效果圖
	imshow("效果圖", dstImage);
	waitKey(0);
	return 0;
}

效果圖: