blur & threshold

This commit is contained in:
2026-03-19 19:16:42 +08:00
parent 9366635e88
commit 275a44cfc8
7 changed files with 107 additions and 0 deletions

15
Threshold/adaptive.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include <opencv2/opencv.hpp>
int main() {
cv::Mat img = cv::imread("../img/2.jpg", cv::IMREAD_GRAYSCALE);
if (img.empty()) return -1;
cv::Mat dst;
// 参数依次为:输入, 输出, 最大值, 自适应方法, 阈值类型, 邻域大小(须为奇数), 常数C
cv::adaptiveThreshold(img, dst, 255, cv::ADAPTIVE_THRESH_MEAN_C,
cv::THRESH_BINARY, 11, 2);
cv::imshow("Adaptive Threshold", dst);
cv::waitKey(0);
return 0;
}

18
Threshold/auto.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
cv::Mat img = cv::imread("../img/2.jpg", cv::IMREAD_GRAYSCALE);
if (img.empty()) return -1;
cv::Mat dst;
// 注意:使用 THRESH_OTSU 时,输入的 thresh 参数 (0) 会被忽略
// 它是通过 “标志位组合” 来实现的cv::THRESH_BINARY | cv::THRESH_OTSU
double otsuThresh = cv::threshold(img, dst, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
std::cout << "Otsu 自动确定的阈值是: " << otsuThresh << std::endl;
cv::imshow("Otsu Threshold", dst);
cv::waitKey(0);
return 0;
}

20
Threshold/simple.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
// 读取灰度图
cv::Mat img = cv::imread("../img/2.jpg", cv::IMREAD_GRAYSCALE);
if (img.empty()) return -1;
cv::Mat dst;
double thresh = 127;
double maxVal = 255;
// 简单阈值处理:二进制阈值
// 返回值是处理时使用的阈值
double ret = cv::threshold(img, dst, thresh, maxVal, cv::THRESH_BINARY);
cv::imshow("Simple Threshold", dst);
cv::waitKey(0);
return 0;
}