zoukankan      html  css  js  c++  java
  • 图像映射

    #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(".//pic//kate.png");
        if (!src.data) {
            printf("can 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();
            //map_x,是x的映射关系
            //map_y,是y的映射关系
            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))
                    {
                        //src.cols * 0.25相当于减去左边的长度,乘以二是以2进行采样
                        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;
                }
    
            }
        }
    }
  • 相关阅读:
    HDU4366 Successor 线段树+预处理
    POJ2823 Sliding Window 单调队列
    HDU寻找最大值 递推求连续区间
    UVA846 Steps 二分查找
    HDU3415 Max Sum of MaxKsubsequence 单调队列
    HDU时间挑战 树状数组
    UVA10168 Summation of Four Primes 哥德巴赫猜想
    UESTC我要长高 DP优化
    HDUChess 递推
    HDU4362 Dragon Ball DP+优化
  • 原文地址:https://www.cnblogs.com/xiaochi/p/12030135.html
Copyright © 2011-2022 走看看