阿丑有点丑 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日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效