Opencv(C++)笔记--检测绘制轮廓-创新互联

1--检测绘制轮廓

利用OpenCV检测轮廓的一般步骤:

创新互联主营召陵网站建设的网络公司,主营网站建设方案,成都App制作,召陵h5成都微信小程序搭建,召陵网站营销推广欢迎召陵等地区企业咨询

①对原图像进行灰度化处理;

②对原图像进行二值化处理;

③检测并提取二值化图像的轮廓;

1-1--检测轮廓API

void cv::findContours(cv::InputArray image, cv::OutputArrayOfArrays contours, cv::OutputArray hierarchy, int mode, int method, cv::Point offset = cv::Point())
1-2--绘制轮廓API

void cv::drawContours(cv::InputOutputArray image, cv::InputArrayOfArrays contours, int contourIdx, const cv::Scalar &color, int thickness = 1, int lineType = 8, cv::InputArray hierarchy = noArray(), int maxLevel = 2147483647, cv::Point offset = cv::Point())
1-3--代码实例
# include# include# includecv::Mat src, dst;
int Threshold = 100;
int Thereshold_max = 255;

void Demo_Contours(int, void*){
    cv::Mat canny;
    std::vector>contours;
    std::vectorhierachy;
    cv::Canny(src, canny, Threshold, Threshold *2, 3, false); // Canny二值化
    cv::findContours(canny, contours, hierachy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point(0,0));

    dst.create(src.size(), CV_8UC3);
    cv::RNG rng(12345);
    for(size_t i = 0; i< contours.size(); i++){
        cv::Scalar color = cv::Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); // 利用随机数生成颜色
        cv::drawContours(dst, contours, i, color, 2, 8, hierachy, 0, cv::Point(0, 0));
    }
    cv::imshow("output", dst);
}

int main(int argc, char** argv){
    src = cv::imread("C:/Users/Liujinfu/Desktop/opencv_bilibili/test1.jpg"); 
    if (src.empty()){
        printf("could not load image..\n");
        return -1;
    }
    cv::imshow("input", src);
    cv::cvtColor(src, src, cv::COLOR_BGRA2GRAY); // 灰度化

    // 创建Trackbar
    cv::createTrackbar("Threshold", "input", &Threshold, Thereshold_max, Demo_Contours);
    Demo_Contours(0, 0);

    cv::waitKey(0);
    return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


本文标题:Opencv(C++)笔记--检测绘制轮廓-创新互联
标题链接:http://ybzwz.com/article/cohpch.html