【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;
    
}
相关推荐
大侠归来4 分钟前
C 语言 | 在函数中操作数组
c语言·c++·算法
老金带你玩AI3 小时前
这几天,我都是拿手机让dot帮我干活
人工智能
朝朝辞暮i6 小时前
C++ 第 23 课:class —— 开始真正进入面向对象
开发语言·c++·算法
7yewh6 小时前
SLAM 三维空间刚体运动(2)
数据结构·人工智能·机器人·嵌入式·slam
小虎AI生活7 小时前
WorkBuddy 模型选型实操:0.03 倍的 Space-Bunny 怎么用、派什么活、避什么坑
人工智能·超级个体·一人公司·青玥ai
ai小陈7 小时前
GPU服务器租用存储验收:检查点写入与磁盘吞吐实战
运维·服务器·人工智能·ai·ssh·gpu算力
微三云马玮均—GEO源码系统 私有化部署7 小时前
消费返物业费:消费+服务趋势的必然产物!
大数据·人工智能·物联网·区块链·生活
明月_清风7 小时前
Muse 登顶 App Store 第一,SDK 直接开源:AI Agent 开始进入下一个阶段
人工智能·后端
郑同学的笔记7 小时前
【c++随笔28】strcmp 和 memcmp对比
开发语言·c++