This commit is contained in:
2026-04-25 12:53:13 +08:00
commit 4f0c53087e
3 changed files with 62 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
# Xmake cache
.xmake/
build/
.vscode/
# MacOS Cache
.DS_Store

38
src/main.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
int main(int argc, const char* argv[]){
cv::Mat img = cv::imread("armor.jpg", cv::IMREAD_COLOR);
if (img.empty()) {
std::cerr << "Failed to load image!" << std::endl;
return -1;
}
int height = img.rows; // 480
int width = img.cols; // 720
int th = 128; // 亮度阈值
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
for (int j = 0; j < height; j++){
for (int i = 0; i < width; i++){
// 注意OpenCV默认是BGR顺序
uchar val = (uchar)(
img.at<cv::Vec3b>(j,i)[0] * 0.0722 + // B
img.at<cv::Vec3b>(j,i)[1] * 0.7152 + // G
img.at<cv::Vec3b>(j,i)[2] * 0.2126 // R
);
out.at<uchar>(j,i) = (val < th) ? 0 : 255;
}
}
cv::imshow("RESULT", out);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}

15
xmake.lua Normal file
View File

@@ -0,0 +1,15 @@
add_rules("mode.debug", "mode.release")
set_languages("c++11")
set_optimize("faster")
add_requires("cmake::OpenCV", {alias = "opencv", system = true})
set_policy("generator.compile_commands", true)
target("hello-cv")
set_kind("binary")
add_packages("opencv")
add_files("src/*.cpp")