效果如图
主要是三个代码,如下图
main.cpp
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// MainWindow w;
// w.show();
Mat img=imread("C:/Users/57281/Pictures/cc3.jpg");
// namedWindow( "Display window", WINDOW_AUTOSIZE );
// imshow("Display window", img);
originalpicture(img);
grayShow(img);
logTransfer(img);
gammaTranfer(img);
colorTransfer(img);
return a.exec();
}
pro文件
项目文件pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
HEADERS +=
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
INCLUDEPATH += D:\applearn\Opencv-VC\opencv\newbuild-me\install\include
LIBS += D:\applearn\Opencv-VC\opencv\newbuild-me\lib\libopencv_*.a
mainwindow.h文件
mainwindow.h文件函数定义等
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
//ctrl+i快捷键自动对齐==声明函数
void originalpicture(Mat &image);
void grayShow(Mat &image);
void Threshold2(Mat &gray);
void logTransfer(Mat &gray);
void gammaTranfer(Mat &gray);
void colorTransfer(Mat &image);
//原始图像
void originalpicture(Mat &image)
{
Mat img;
img=image;
namedWindow( "Origin window");
imshow("Origin window", img);
}
//灰度图像
void grayShow(Mat &image)
{
Mat gray;
cvtColor(image, gray, COLOR_RGBA2GRAY);
namedWindow("gray picture");
imshow("gray picture", gray);
// namedWindow( "Display window", WINDOW_AUTOSIZE );
Threshold2(gray);
//logTransfer(gray);
//gammaTranfer(gray);
}
//二值变换
void Threshold2(Mat &gray)
{
Mat result;
threshold(gray, result, 120,255,THRESH_BINARY);
namedWindow("binary picture");
imshow("binary picture", result);
}
//对数变换
void logTransfer(Mat &gray)
{
Mat srcImage(gray);
Mat dstImage(srcImage.size(),srcImage.type());
//计算1+r
add(gray, Scalar(8), srcImage);
//转换为浮点数
srcImage.convertTo(srcImage, CV_64F);
//计算log(1+r) log:第一个参数为输入图像,第二个参数为得到的对数值
log(srcImage,dstImage);
dstImage=1*dstImage;
//归一化处理
normalize(dstImage,dstImage,0,255,NORM_MINMAX);
convertScaleAbs(dstImage, dstImage);
imshow("dstpicture", dstImage);
}
//伽马变换
void gammaTranfer(Mat &gray)
{
//gamma值,随着值不同图片呈现出不同的效果
double gamma=0.5;
Mat grayImg;
gray.convertTo(grayImg, CV_64F,1.0/255,0);
Mat gammaImg;
pow(grayImg, gamma,gammaImg);
gammaImg.convertTo(gammaImg, CV_8U,255,0);
imshow("gammapicture", gammaImg);
}
//彩色图像变换
void colorTransfer(Mat &image)
{
Mat colorpicture;
image.copyTo(colorpicture);
//HSV
Mat hsvImg;
cvtColor(image,hsvImg, COLOR_BGR2HSV);
std::vector<Mat> hsv;
split(hsvImg, hsv);
hsv[0]=(Scalar::all(180)-hsv[0]);
merge(hsv, colorpicture);
imshow("HSV_picture", colorpicture);
//RGB
colorpicture=Scalar::all(255)-image;
imshow("RBG_picture", colorpicture);
}
//int main(int argc, char** argv) {
// //读取图像
// Mat image;
// image = imread("/Users/Admin/Desktop/myProject/1.jpg", 1);
// namedWindow("Display Image", WINDOW_AUTOSIZE);
// imshow("Display Image", image);
// //grayShow(image);
// colorTransfer(image);
// waitKey(0);
// return 0;
//}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
};
#endif // MAINWINDOW_H
结果处理
一些参考来源:
1、https://blog.csdn.net/chuxuezheerer/article/details/106322311
2、https://blog.csdn.net/dongbao520/article/details/124618235[0]
一些备注
normalize()函数:
src 输入数组
dst 输出数组,支持原地运算
range normalization模式的最小值
range normalization模式的最大值,不用于norm normalization(范数归一化)模式。
normType归一化的类型,可以有以下的取值:
– NORM_MINMAX:数组的数值被平移或缩放到一个指定的范围,线性归一化,一般较常用。
–NORM_INF: 此类型的定义没有查到,根据OpenCV 1的对应项,可能是归一化数组的C-范数(绝对值的最大值)
–NORM_L1 : 归一化数组的L1-范数(绝对值的和)
–NORM_L2: 归一化数组的(欧几里德)L2-范数
原图来源侵权删除[0]
finally
![在这里插入图片描述](https://img-blog.csdnimg.cn/5b8008ae8afd460d9fb6f96c2e在这里插入图片描述
6a3267.png)
文章出处登录后可见!
已经登录?立即刷新