#include <opencv2/opencv.hpp>
#include <vector>
int main() {
cv::Mat image = cv::imread("path_to_image", cv::IMREAD_GRAYSCALE); // 读取灰度图片
cv::Mat contourImage = cv::Mat::zeros(image.size(), CV_8UC3); // 创建一个与原图同样大小的空白图片,颜色通道为3
std::vector<std::vector<cv::Point>> contours; // 存储轮廓的容器
std::vector<cv::Vec4i> hierarchy; // 存储轮廓的层级信息
// 寻找轮廓
cv::findContours(image, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
// 绘制轮廓
int contourIdx = 0;
for (; contourIdx < contours.size(); contourIdx++) {
cv::Scalar color = cv::Scalar(0, 255, 0); // 定义颜色,这里为绿色
int thickness = 2; // 定义线宽
cv::drawContours(contourImage, contours, contourIdx, color, thickness);
}
// 展示结果
cv::imshow("Contours", contourImage);
cv::waitKey(0);
return 0;
}