Koi-Blue / Koi-Blue.github.io

1 stars 0 forks source link

OpenCV -- first project #7

Open Koi-Blue opened 2 weeks ago

Koi-Blue commented 2 weeks ago

声明:仅作为今天交流内容参考

0 构建CMakeLists.txt文件

  • 先声明 cmake 版本及项目名称
  • 查找OpenCV包
    find_package(OpenCV REQUIRED)
  • 包含OpenCV头文件
    include_directories(${OpenCV_INCLUDE_DIRS})
  • 添加可执行文件
  • 链接OpenCV库
    target_link_libraries(ImageToGray ${OpenCV_LIBS})

1 主要功能的C++实现

1.1 导入opencv头文件

#include <opencv2/opencv.hpp>

1.2 主函数

int main(int argc, char** argv) 

1.3 参数检查

if (argc != 2) {
    std::cout << "Usage: ./ImageToGray <image_path>\n";
    return -1;
}

1.4 图像读取

cv::Mat image = cv::imread(argv[1], cv::IMREAD_COLOR);
if (image.empty()) {
    std::cout << "Could not open or find the image\n";
    return -1;
}

1.5 图像转换为灰度

cv::Mat gray_image;
cvtColor(image, gray_image, cv::COLOR_BGR2GRAY);

1.6 显示图像

cv::namedWindow("Original Image", cv::WINDOW_NORMAL);
cv::imshow("Original Image", image);

cv::namedWindow("Gray Image", cv::WINDOW_NORMAL);
cv::imshow("Gray Image", gray_image);

1.7 等待用户输入

cv::waitKey(0);

注意

Tatsuyxa commented 2 weeks ago

老大的注释就是简单易懂