every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog
0. 前言
灰度变换、对数变换、伽马变换
1. 灰度变换
#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. 对数变换
#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;
}
文章来源:https://www.toymoban.com/news/detail-736990.html
3. 伽马变换
#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;
}
文章来源地址https://www.toymoban.com/news/detail-736990.html
到了这里,关于【c++|opencv】二、灰度变换和空间滤波---1.灰度变换、对数变换、伽马变换的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!