【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;
    
}
相关推荐
智算菩萨几秒前
GPT-5.4 Pro与Thinking模型全面研究报告
人工智能·gpt·ai·chatgpt·ai-native
2301_816651222 分钟前
嵌入式C++低功耗设计
开发语言·c++·算法
团子和二花9 分钟前
openclaw平替之nanobot源码解析(八):Gateway进阶——定时任务与心跳机制
人工智能·gateway
机器之心12 分钟前
昨晚,OpenClaw大更新,亲手终结「旧插件」时代
人工智能·openai
码路高手19 分钟前
Trae-Agent源码重点
人工智能·架构
剑穗挂着新流苏31227 分钟前
114_PyTorch 进阶:模型保存与读取的两大方式及“陷阱”避坑指南
人工智能·pytorch·深度学习
qq_4160187228 分钟前
分布式缓存一致性
开发语言·c++·算法
CoovallyAIHub30 分钟前
把 Whisper、Moonshine、SenseVoice 统统装进手机:sherpa-onnx 离线语音部署框架,GitHub 10.9K Star
人工智能·架构
干啥啥不行,秃头第一名36 分钟前
STL容器内部实现剖析
开发语言·c++·算法
一只叫煤球的猫37 分钟前
RAG 如何落地?从原理解释到工程实现
人工智能·后端·ai编程