#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> imgPaths = {
"wow/1.png",
"wow/2.png",
"wow/3.png",
"wow/4.png"
};
for (const auto& imgPath : imgPaths) {
// 读取图像
cv::Mat img = cv::imread(imgPath);
if (img.empty()) {
std::cerr << "无法读取图像: " << imgPath << std::endl;
continue;
}
// 灰度图
cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
// 高斯模糊处理后的灰度图
cv::Mat gray_blurred;
cv::GaussianBlur(gray, gray_blurred, cv::Size(5, 5), 0);
// 大津法(OTSU)自动二值化
cv::Mat binary;
double t = cv::threshold(gray_blurred, binary, 0, 255,
cv::THRESH_BINARY | cv::THRESH_OTSU);
// 显示图像
cv::imshow("1-原图", img);
cv::imshow("2-灰度图", gray);
cv::imshow("3-高斯模糊处理后的灰度图", gray_blurred);
cv::imshow("4-大津法(OTSU)自动二值化处理后的二值化图", binary);
cv::waitKey(0);
cv::destroyAllWindows();
}
return 0;
}