阿丑有点丑 2022-05-17 16:34 采纳率: 100%
浏览 86
已结题

Opencv4中drawContours函数只能绘制出蓝色的线

color中只有蓝色的通道调整有用,红色和绿色怎么调都没有作用,不知道这是为什么?

#include <opencv2/opencv.hpp>
#include <string>
#include <iostream>

std::string Type2String(int type)
{
    std::string strType;
    uchar depth = type & CV_MAT_DEPTH_MASK;
    uchar chans = 1 + (type >> CV_CN_SHIFT);
    switch (depth)
    {
    case CV_8U:
        strType = "CV_8U"; break;
    case CV_8S:
        strType = "CV_8S"; break;
    case CV_16U:
        strType = "CV_16U"; break;
    case CV_16S:
        strType = "CV_16S"; break;
    case CV_32S:
        strType = "CV_32S"; break;
    case CV_32F:
        strType = "CV_32F"; break;
    case CV_64F:
        strType = "CV_64F"; break;
    default:
        strType = "UNKNOWN_TYPE"; break;
    }
    strType += "C";
    strType += (chans + '0');

    return strType;
}

int main()
{
    cv::Mat segmentation = cv::imread("D:/Desktop/轮廓test/segmentation.jpg");

    if (segmentation.empty())
    {
        std::cout << "图片为空" << std::endl;
        return 1;
    }

    std::cout << "segmentation:" << Type2String(segmentation.type()) << std::endl;

    cv::Mat segmentation_gary;
    cvtColor(segmentation, segmentation_gary, cv::COLOR_BGR2GRAY);

    std::cout << "segmentation_gary:" << Type2String(segmentation_gary.type()) << std::endl;

    std::vector< std::vector<cv::Point> > contours;
    std::vector<cv::Vec4i> hierarchy;
    
    findContours(segmentation_gary, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

    std::cout << "segmentation:" << Type2String(segmentation.type()) << std::endl;

    for (size_t i = 0; i < contours.size(); i++)
    {
        cv::Scalar color = (255, 0, 255);
        drawContours(segmentation, contours, i, color, 5, 8, hierarchy, 0, cv::Point(0, 0));
    }
    imshow("轮廓", segmentation);

    cv::waitKey();
    return 0;
}

这是原图

img

这是运行结果(如果蓝色通道为0,其他通道为任何值都不显示线条)

img

img

  • 写回答

1条回答 默认 最新

  • youcans_ 人工智能领域优质创作者 2022-05-17 20:48
    关注

    试试:
    Scalar color( rand()&255, rand()&255, rand()&255 );

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月2日
  • 已采纳回答 5月25日
  • 创建了问题 5月17日

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献