【c++|opencv】二、灰度变换和空间滤波---1.灰度变换、对数变换、伽马变换

every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog

0. 前言

灰度变换、对数变换、伽马变换

1. 灰度变换

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main() 
{
    
    Mat img,out_img,img_gray;
    img = imread("/home/v/home.png");
    if (img.empty()){
        cout << "Could not open or find the image" << endl;
        return -1;
    }

    cvtColor(img, img_gray, COLOR_BGR2GRAY);
    imshow("img gray",img_gray); 

    out_img = img_gray.clone();
    for (int i=0;i<img_gray.rows;i++){
        for (int j=0;j<img_gray.cols;j++){
            // 灰度翻转
            out_img.at<uchar>(i,j) = 255 - img_gray.at<uchar>(i,j); 
        }
    }
    
    imshow("灰度翻转",out_img);
    waitKey(0);
    return 0;
    
}

2. 对数变换

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;


int main() 
{
    
    Mat img,out_img,img_gray;
    img = imread("/home/v/home.png");
    if (img.empty()){
        cout << "Could not open or find the image" << endl;
        return -1;
    }

    cvtColor(img, img_gray, COLOR_BGR2GRAY);
    imshow("img gray",img_gray); 

    out_img = img_gray.clone();
    for (int i=0;i<img_gray.rows;i++){
        for (int j=0;j<img_gray.cols;j++){
            // 对数变换6*log(r+1) 伽马变换
            out_img.at<uchar>(i,j) = 6*log((double)(img_gray.at<uchar>(i,j)) + 1);
        }
    }

    normalize(out_img,out_img,0,255,NORM_MINMAX); // 图像归一化
    convertScaleAbs(out_img,out_img); // 数据类型转换到CV_8U
    
    imshow("对数变换",out_img);
    waitKey(0);
    return 0;
    
}

3. 伽马变换

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>


using namespace std;
using namespace cv;


int main() 
{
    
    Mat img,out_img,img_gray;
    img = imread("/home/v/home.png");
    if (img.empty()){
        cout << "Could not open or find the image" << endl;
        return -1;
    }

    cvtColor(img, img_gray, COLOR_BGR2GRAY);
    imshow("img gray",img_gray); 

    out_img = img_gray.clone();
    for (int i=0;i<img_gray.rows;i++){
        for (int j=0;j<img_gray.cols;j++){
            // 伽马变换6*r^0.5
            out_img.at<uchar>(i,j) = 6*pow((double)(img_gray.at<uchar>(i,j)),0.5);
        }
    }

    normalize(out_img,out_img,0,255,NORM_MINMAX); // 图像归一化
    convertScaleAbs(out_img,out_img); // 数据类型转换到CV_8U
    
    imshow("伽马变换",out_img);
    waitKey(0);
    return 0;
    
}
相关推荐
救救孩子把2 分钟前
3-机器学习与大模型开发数学教程-第0章 预备知识-0-3 函数初步(多项式、指数、对数、三角函数、反函数)
人工智能·数学·机器学习
CareyWYR2 分钟前
每周AI论文速递(250908-250912)
人工智能
张晓~183399481214 分钟前
短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
c语言·c++·人工智能·矩阵·c#·php·音视频
deephub32 分钟前
量子机器学习入门:三种数据编码方法对比与应用
人工智能·机器学习·量子计算·数据编码·量子机器学习
AI 嗯啦35 分钟前
计算机视觉----opencv实战----指纹识别的案例
人工智能·opencv·计算机视觉
max50060039 分钟前
基于多元线性回归、随机森林与神经网络的农作物元素含量预测及SHAP贡献量分析
人工智能·python·深度学习·神经网络·随机森林·线性回归·transformer
trsoliu40 分钟前
前端基于 TypeScript 使用 Mastra 来开发一个 AI 应用 / AI 代理(Agent)
前端·人工智能
白掰虾1 小时前
STM32N6&AI资料汇总
人工智能·stm32·嵌入式硬件·stm32n6·stm32ai
一枝小雨1 小时前
【C++】list 容器操作
开发语言·c++·笔记·list·学习笔记
HMBBLOVEPDX1 小时前
C++(继承和多态)
开发语言·c++·继承和多态