灰度变换:
1.原理:灰度图的图像反转就是将 黑变白 亮变暗 原来255 的值就变为 0
2.opencv 实现:
#include <opencv2/opencv.hpp>
#include<iostream>
cv::Mat image1_gray; //灰度图
cv::Mat readImg = cv::imread("huidutu.jpeg");
cv::cvtColor(readImg, image1_gray, COLOR_BGR2GRAY); //灰度化
//cv::imshow("image", image1_gray); //显示灰度图像
//反转
for(int i = 0; i < image1_gray.rows; i++)
{
for(int j = 0; j < image1_gray.cols; j++)
{
image1_gray.at<uchar>(i , j) = 255 - image1_gray.at<uchar>(i, j);
}
}
//cv::imshow("image",image1_gray); //反转后的图像
cv::waitKey(0);
对数变换:
1.对数变换的⼀般形式:t=clog(1+s),其中c为尺度⽐例常数,s为源灰度值,t为变换
后的⽬标灰度值
2.增强图像中较暗部分 较亮部分变化不明显
cv::Mat image1_gray; //灰度图
cv::Mat readImg = cv::imread("huidutu.jpeg");
//归一化
cv::cvtColor(readImg, image1_gray, COLOR_BGR2GRAY); //灰度化
//cv::imshow("image", image1_gray); //显示灰度图像
//对数变换
for(int i = 0; i < image1_gray.rows; i++)
{
for(int j = 0; j < image1_gray.cols; j++)
{
image1_gray.at<uchar>(i , j) = log((double)(10 + image1_gray.at<uchar>(i , j)));
}
}
//归一化
cv::normalize(image1_gray, image1_gray, 0, 255, NORM_MINMAX);
cv::imshow("image",image1_gray); //后的图像
cv::waitKey(0);
变换前:
变换后:
幂律变换
基本形式: s = c*ry 其中 c,y 为常量 c 一般值为1.
y > 1 时:高灰度值会映射到较窄的灰度值区间 即 由亮变暗
y < 1 时:低灰度值会映射到较宽的灰度值区间 即由暗变亮
//c = 1 y = 3 时图像变暗
cv::Mat image1_gray; //灰度图
cv::Mat readImg = cv::imread("Fig0308(a)(fractured_spine).tif");
cv::cvtColor(readImg, image1_gray, COLOR_BGR2GRAY); //灰度化
for(int i = 0; i < image1_gray.rows; i++)
{
for(int j = 0; j < image1_gray.cols; j++)
{
int value1 = (int)image1_gray.at<uchar>(i , j);
value1 = value1 / 255;
value1 = value1 * value1 * value1 *255;
image1_gray.at<uchar>(i , j) = value1;
}
}
cv::imshow("image",image1_gray);
cv::waitKey(0);
![]() 整体变暗.jpg |
//c = 1 y = 0.7 时图像变亮
//opencv + qt
#include <QtMath>
cv::Mat image1_gray; //灰度图
cv::Mat readImg = cv::imread("Fig0308(a)(fractured_spine).tif");
cv::cvtColor(readImg, image1_gray, COLOR_BGR2GRAY); //灰度化
for(int i = 0; i < image1_gray.rows; i++)
{
for(int j = 0; j < image1_gray.cols; j++)
{
image1_gray.at<uchar>(i, j) = qPow(image1_gray.at<uchar>(i, j), 0.7);
}
}
cv::normalize(image1_gray, image1_gray, 0 , 255, NORM_MINMAX);
cv::imshow("image",image1_gray);
cv::waitKey(0);
![]() 整体变亮.jpg |
![]() 原图.jpg |
#include <opencv2/opencv.hpp>
#include<iostream>
cv::Mat image1_gray; //灰度图
cv::Mat readImg = cv::imread("D:\\book\\image\\DIP3E_Original_Images_CH03\\Fig0312(a)(kidney).tif");
cv::cvtColor(readImg, image1_gray, COLOR_BGR2GRAY); //灰度化
for(int i = 0; i < image1_gray.rows; i++)
{
for(int j = 0; j < image1_gray.cols; j++)
{
//小于155大于245的灰度值改为0
if(image1_gray.at<uchar>(i,j) < 155 | image1_gray.at<uchar>(i,j) > 245)
{
image1_gray.at<uchar>(i,j) = 0;
}
else
{
image1_gray.at<uchar>(i,j) = 255;
}
}
}
cv::imshow("image",image1_gray);
cv::waitKey(0);
方法二:让感兴趣的范围变亮或变暗,其余部分不变
#include <opencv2/opencv.hpp>
#include<iostream>
cv::Mat image1_gray; //灰度图
cv::Mat readImg = cv::imread("D:\\book\\image\\DIP3E_Original_Images_CH03\\Fig0312(a)(kidney).tif");
cv::cvtColor(readImg, image1_gray, COLOR_BGR2GRAY); //灰度化
for(int i = 0; i < image1_gray.rows; i++)
{
for(int j = 0; j < image1_gray.cols; j++)
{
//感兴趣的部分灰度值改为255其余不变
if(image1_gray.at<uchar>(i,j) >= 155 && image1_gray.at<uchar>(i,j) <= 245)
{
image1_gray.at<uchar>(i,j) = 255;
}
}
}
cv::imshow("image",image1_gray);
cv::waitKey(0);
![]() 001.jpg |
![]() 002.jpg |
![]() 003.jpg |