【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;
    
}
相关推荐
RUZHUA4 分钟前
印度客机坠毁致波音美股盘前直线下跌
人工智能
魔障阿Q13 分钟前
华为服务器obsutil使用方法
服务器·人工智能·华为云
吴声子夜歌44 分钟前
OpenCV——图像平滑
人工智能·opencv·计算机视觉
凡人的AI工具箱1 小时前
PyTorch深度学习框架60天进阶学习计划 - 第58天端到端对话系统(一):打造你的专属AI语音助手
人工智能·pytorch·python·深度学习·mcp·a2a
吴声子夜歌1 小时前
OpenCV——图像的几何变化
人工智能·opencv·计算机视觉
UQI-LIUWJ1 小时前
论文略读:MUSE: Machine Unlearning Six-Way Evaluation for Language Models
人工智能·深度学习·机器学习
艾醒(AiXing-w)1 小时前
探索大语言模型(LLM):使用EvalScope进行模型评估(API方式)
人工智能·语言模型·自然语言处理
羊小猪~~2 小时前
数据库学习笔记(十五)--变量与定义条件与处理程序
数据库·人工智能·笔记·后端·sql·学习·mysql
ahhhhaaaa-2 小时前
【AI图像生成网站&Golang】部署图像生成服务(阿里云ACK+GPU实例)
开发语言·数据仓库·人工智能·后端·阿里云·golang
摘取一颗天上星️2 小时前
NLP进化史:从规则模板到思维链推理,七次范式革命全解析
人工智能·自然语言处理