• OpenCV --- 修改图像的对比度、亮度 、RGB转Gray图像、修改图像的尺寸


    #include <opencv2/core.hpp>
    #include <opencv2/imgcodecs.hpp>
    #include <opencv2/highgui.hpp>
    #include <opencv2/imgproc.hpp>
    #include <iostream>
    using namespace cv;
    using namespace std;
    
    // 计时函数
    void PrintMs(const char *text = "")
    {
        static long long last = 0;
        long long cur = getTickCount();
        if (last == 0)
        {
            last = cur;
            return;
        }
        long long ms = 0;
        ms = ((double)(cur - last) / getTickFrequency()) * 1000;
        if (*text != 0)
        {
            printf("%s = %dms
    ", text,ms);
        }
        last = getTickCount();
    }
    
    // RGB图像转Gray图像
    void RGBToGray(Mat &src, Mat &des)
    {
        // GRay = (R*30 + G*59 + B*11 +50)/100
        des.create(src.rows,src.cols,CV_8UC1);
        for (int r = 0; r < src.rows; r++)
        {
            for (int c = 0; c < src.cols; c++)
            {
                Vec3b &m = src.at<Vec3b>(r, c);
                int gray = (m[2] * 30 + m[1] * 59 + m[0] * 11 + 50) / 100;
                des.at<uchar>(r, c) = gray;
            }
        }
    }
    
    /////////////改变图像的对比度和亮度/////////////////////////////////////
    ///@para a float 对比度 1.0~3.0
    ///@para b int 亮度 0~100
    void ChangeGain(Mat &src, Mat &des, float a, int b)
    {
        //g(r,c) = a*f(r,c) + b
        des.create(src.rows, src.cols, src.type());
        for (int r = 0; r < src.rows; r++)
        {
            for (int c = 0; c < src.cols; c++)
            {
                for (int i = 0; i < 3; i++)
                {
                    des.at<Vec3b>(r, c)[i] = saturate_cast<uchar>(a * src.at<Vec3b>(r, c)[i] + b);
                }
            }
        }
    }
    
    // 改变图像的尺寸
    void xresize(Mat &src, Mat &des, Size size)
    {
        des.create(size, src.type());
        //映射的原图坐标
        int sx, sy = 0;
        float fx = (float)src.cols / des.cols;
        float fy = (float)src.rows / des.rows; 
        for (int x = 0; x < des.cols; x++)
        {
            sx = fx * x + 0.5;
            for (int y = 0; y <des.rows; y++)
            {
                sy = fy * y + 0.5;
                des.at<Vec3b>(y, x) = src.at<Vec3b>(sy, sx);
            }
        }
    
    }
    
    int main(int argc, char *argv[])
    {
        Mat src = imread("1.png");
        src.create(3000, 4000, CV_8UC3);
        Mat gray;
        PrintMs("");
        cvtColor(src, gray, COLOR_BGR2GRAY);
        PrintMs("cvtColor1");
    
        cvtColor(src, gray, COLOR_BGR2GRAY);
        PrintMs("cvtColor2");
        Mat mygray;
        RGBToGray(src, mygray);
        PrintMs("RGBToGray");
    
        namedWindow("src");
        namedWindow("gray");
        namedWindow("mygray");
    
        imshow("src", src);
        imshow("gray", gray);
        imshow("mygray", mygray);
    
        waitKey(0);
    
    
        return 0;
    }
  • 相关阅读:
    nginx系列11:负载均衡哈希算法ip_hash与hash模块
    nginx系列10:通过upstream模块选择上游服务器和负载均衡策略round-robin
    nginx系列9:HTTP反向代理请求处理流程
    css 滚动条样式
    Vue优化:常见会导致内存泄漏问题及优化
    vue自定义指令导致的内存泄漏问题解决
    vue动态绑定class的几种方式
    前端知识点回顾之重点篇——CSS中flex布局
    Javascript数组操作
    pc端常用电脑屏幕 ((响应式PC端媒体查询)电脑屏幕分辨率尺寸大全)
  • 原文地址:https://www.cnblogs.com/hs-pingfan/p/10656898.html
Copyright © 2020-2023  润新知