【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;
    
}
相关推荐
梁辰兴几秒前
数据结构实验10.1:内部排序的基本运算
数据结构·c++·算法·排序算法·c·内部排序
虾球xz3 分钟前
游戏引擎学习第291天:跳跃的怪物与占据的树木
c++·学习·游戏引擎
斯普信专业组9 分钟前
Kubernetes MCP服务器(K8s MCP):如何使用?
人工智能·kubernetes
冷yan~24 分钟前
构建下一代AI智能体:基于Spring AI的多轮对话应用
java·人工智能·spring·ai
fouen26 分钟前
【语义分割专栏】先导篇:评价指标(PA,CPA,IoU,mIoU,FWIoU,F1)
人工智能·算法·机器学习·计算机视觉
共享家952736 分钟前
布隆过滤器
c++
Jamence39 分钟前
多模态大语言模型arxiv论文略读(八十三)
论文阅读·人工智能·深度学习·语言模型·论文笔记
纪伊路上盛名在42 分钟前
LLM大语言模型系列1-token
字符编码·人工智能·语言模型·自然语言处理·token·文本处理
Johny_Zhao1 小时前
HSRP、GLBP、VRRP、NSRP 协议对比与配置指南
网络·人工智能·网络安全·信息安全·云计算·cisco·huawei·系统运维·华三
mochensage1 小时前
2025年全国青少年信息素养大赛C++小学全年级初赛试题
开发语言·c++·算法