Opencv的findcontours函数的大坑

比较了两个图一个会产生2个轮廓,一个只产生一个轮廓

cpp 复制代码
/**
 * @function findContours_Demo.cpp
 * @brief Demo code to find contours in an image
 * @author OpenCV team
 */

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    CommandLineParser parser(argc, argv, "{@input | HappyFish.jpg | input image}");
    Mat src = imread(samples::findFile(parser.get<String>("@input")));
    Mat src_gray , binarized ;
    cvtColor(src, src_gray, COLOR_BGR2GRAY);
    threshold(src_gray, binarized, 90, 255, THRESH_BINARY); //THRESH_BINARY_INV);
    //savedata(binarized);
    vector<vector<Point>> contours;
    findContours(binarized, contours, RETR_LIST, CHAIN_APPROX_NONE); // 查找轮廓
    for (size_t i = 0; i < contours.size(); i++)
    {
        //contourArea(contours[i]);
        double area = moments(contours[i]).m00;
        cout << "轮廓" << i << " 面积:" << area << endl;
        drawContours(src, contours, i, Scalar(0, 255, 0), 2);
    }
    string windowName = "Contours " + to_string(contours.size());
    imshow(windowName, src);
    waitKey();
    return 0;
}
相关推荐
larance4 分钟前
[菜鸟教程] 机器学习教程十课-Python 机器学习应用
人工智能·python·机器学习
Aloudata6 分钟前
Metric Layer 建设指南:如何先从核心指标层启动企业语义工程
大数据·人工智能·数据分析·data agent·语义层
聪明蛋子哟7 分钟前
不仅仅是向量检索:结合知识图谱与Tool Calling的混合增强生成(Hybrid RAG)方案
人工智能·算法·知识图谱
雪兽软件8 分钟前
AI如何玩转太空探索?
人工智能·太空探索
jerryinwuhan9 分钟前
HV-DGTF数据治理框架
大数据·人工智能
qq_3691736311 分钟前
如何将 AI 生成的 HTML 网页发布成在线链接?不用自己搭建服务器
前端·人工智能·html·效率工具·html 发布
每天一道题12 分钟前
Agent 评测的关键,不是给它出难题,而是让它做选择
人工智能·ai
Solara14 分钟前
我重算了 11 天,抓出自己抄错的 1 天:AI 会把"公式"记成"数列"
人工智能·程序员·ai编程
用户3600555790015 分钟前
05 · 卸载集合:小投影为何是负收益
人工智能