(24)像素重映射

五迷三道 提交于 2020-03-26 17:50:16

所谓的像素重映射就是:我们对原图的每一个像素值进行提取,然后经过一个函数变换,映射到我们空白的目标图片上,就得到一张新的图片,这个就是像素重映射。具体的基础知识,看这个博客:https://blog.csdn.net/qq_41167777/article/details/84947439

===========================

 

下面这个代码运行,有好几种结果,主要就是,你按下 1,2,3,0,等等,会出现不同的图片展示。

 

===============================

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 using namespace std;
 4 using namespace cv;
 5 
 6 Mat src, dst, map_x, map_y;
 7 const char* input_win = "input";
 8 int index = 0;
 9 const char* out_put = "hough-line-detection";
10 void update_map(void);
11 int main(int agrc, char** agrv) {
12     src = imread("C:\\Users\\32829\\Desktop\\111.jpg");
13     if (!src.data) {
14         printf("no load..\n");
15         return -1;
16     }
17     namedWindow(input_win, CV_WINDOW_AUTOSIZE);
18     namedWindow(out_put, CV_WINDOW_AUTOSIZE);
19     imshow(input_win, src);
20     map_x.create(src.size(), CV_32FC1);
21     map_y.create(src.size(), CV_32FC1);
22     int c = 0;
23     while (true) {
24         c = waitKey(500);
25         if ((char)c == 27) {
26             break;
27         }
28         index = c % 4;
29         update_map();
30         remap(src, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 255, 255));
31         imshow(out_put, dst);
32     }
33 }
34 void update_map(void) {
35     for (int row = 0; row < src.rows; row++) {
36         for (int col = 0; col < src.cols; col++) {
37             switch (index) {
38             case 0:
39                 if (col > (src.cols*0.25) && col<(src.cols*0.75) && row>(src.rows*0.25) && row < (src.rows*0.75)) {
40                     map_x.at<float>(row, col) = 2 * (col - (src.cols*0.25));
41                     map_y.at<float>(row, col) = 2 * (row - (src.rows*0.25));
42                 }
43                 else {
44                     map_x.at<float>(row, col) = 0;
45                     map_y.at<float>(row, col) = 0;
46                 }
47                 break;
48             case 1:
49                 map_x.at<float>(row, col) = (src.cols - col - 1);
50                 map_y.at<float>(row, col) = row;
51                 break;
52             case 2:
53                 map_x.at<float>(row, col) = col;
54                 map_y.at<float>(row, col) = (src.rows - row - 1);
55                 break;
56             case 3:
57                 map_x.at<float>(row, col) = (src.cols - col - 1);
58                 map_y.at<float>(row, col) = (src.rows - row - 1);
59                 break;
60             }
61         }
62     }
63 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!