赞
踩
- #include <opencv2/opencv.hpp>
- #include <cstdio>
-
- int main(int argc, char *argv[]){
- cv::Mat src = cv::imread("../test1.jpg");
- cv::Mat gray;
- cv::Mat bin;
- cv::Mat ibin;
-
- // 灰度图转化
- cvtColor(src, gray, cv::COLOR_BGR2GRAY);
-
- //二进制阈值化
- threshold(gray, bin, 100, 255, cv::THRESH_BINARY);
-
- //二进制反阈值化
- threshold(gray, ibin, 100, 255, cv::THRESH_BINARY_INV);
-
- cv::imshow("bin", bin);
- cv::imshow("src", src);
- cv::imshow("ibin", ibin);
- cv::waitKey(0);
-
- return 0;
- }
- cmake_minimum_required(VERSION 3.24) # 指定 cmake的 最小版本
- project(test) # 设置项目名称
-
- find_package(Opencv REQUIRED)
- INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
- add_executable(main main.cpp) # 生成可执行文件
- target_link_libraries(main ${OpenCV_LIBS}) # 设置target需要链接的库
- #include <opencv2/opencv.hpp>
- #include <cstdio>
-
- void ChangeGain(const cv::Mat &src, cv::Mat &des, float a, int b){
- // g(r, c) = a*f(r, c) + b
- for (int r = 0; r < src.rows; r++){
- for (int c = 0; c < src.cols; c++){
- for (int i = 0; i < 3; i++){
- des.at<cv::Vec3b>(r, c)[i] = cv::saturate_cast<uchar>(a * src.at<cv::Vec3b>(r, c)[i] + b);
- }
- }
- }
- }
-
- int main(int argc, char *argv[]){
- cv::Mat src = cv::imread("../test1.jpg");
- cv::Mat des(src.rows, src.cols, src.type());
- ChangeGain(src, des, 2.0, 50);
-
- cv::imshow("src", src);
- cv::imshow("des", des);
- cv::waitKey(0);
-
- return 0;
- }
①修改对比度和亮度的公式:g(i, j) = a * f(i, j) + b;
对比度a的取值范围一般为(1.0 ~ 3.0),亮度b的取值范围一般为(0 ~ 100);
②saturate_cast是OpenCV防止溢出的一个函数,当像素值超过255时,修正为255,当像素值为负数时,修正为0;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。