• OpenCV 计算机视觉(十七)像素重映射


    像素重映射:

    就是把原图像中的像素点通过相应的操作,变换到另一图片的相应位置。

    #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:/OpenCVprj/image/test3.jpg");
    	if (!src.data) {
    		printf("could not load image...
    ");
    		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;
    			}
    
    		}
    	}
    }
    

      

      

  • 相关阅读:
    python3 数据类型测试
    python基础 数据类型
    python 运算符
    我的博客终于开通了
    Unity3d使用蓝牙(bluetooth)开发IOS点对点网络游戏
    ASP.NET 使用application和session对象写的简单聊天室程序
    今日开博 留个脚印
    ASP.NET Visual Studio2010 发布Web网站问题详解
    C# 使用Linq递归查询数据库遇到的问题及解决方法
    C# 一个简单的秒表引发的窗体卡死问题
  • 原文地址:https://www.cnblogs.com/haiboxiaobai/p/11248654.html
Copyright © 2020-2023  润新知