查找并绘制轮廓[OpenCV 笔记XX]

好久没有更新了,原谅自己放了个假最近又在赶进度,所以。。。更新的内容是很靠后的第八章,因为最近工作要用就先跳了,后面会更新笔记编号。。。加油加油!

 

在二值图像中寻找轮廓

void cv::findContours    (    InputOutputArray     image,
                        OutputArrayOfArrays     contours,
                        OutputArray     hierarchy,
                        int     mode,
                        int     method,
                        Point     offset = Point() 
                      )    
  • image: 输入图像,需为8位单通道图像,图像非0像素视为1。 可以用compare(), imrange(), threshold(), adaptivethreshold(), canny()等函数创建,注意:此函数会在提取图像轮廓的同时修改图像内容。
    • If mode equals to RETR_CCOMP or RETR_FLOODFILL, the input can also be a 32-bit integer image of labels (CV_32SC1).
  • contours: 检测到的轮廓,每个轮廓存储为一个点向量,即用point类型的vector,例如可为类型vector<vector<Point> >。
  • hierarchy: 可选的输出向量,包含图像的拓扑信息。 每个轮廓contours[i],
    • hierarchy[i][0] , 后一个轮廓,
    • hierarchy[i][1] , 前一个轮廓,
    • hierarchy[i][2] , 父轮廓,
    • hierarchy[i][3], 内嵌轮廓的索引编号。
    • 如果没有对应项,hierarchy[i]中的对应项设为负数。
  • mode: 检索模式,可选模式包括
    • RETR_EXTERNAL: 只监测最外层轮扩。hierarchy[i][2] = hierarchy[i][3] = -1
    • RETR_LIST: 提取所有轮廓,并放置在list中。检测的轮廓不建立等级关系。
    • RETR_CCOMP: 提取所有轮廓,并将其组织为双层结构,顶层为联通域的外围边界,次层为空的内层边界。
    • RETR_TREE: 提取所有轮廓,并重新建立网状的轮廓结构。
  • method: 轮廓的近似办法,包括
    • CHAIN_APPROX_NONE: 获取每个轮廓的每个像素,相邻两点像素位置差不超过1,max(abs(x1-x2),abs(y1-y2)) == 1
    • CHAIN_APPROX_SIMPLE: 压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标
    • CHAIN_APPROX_TC89_LI /CHAIN_APPROX_TC89_KCOS: 使用Teh-Chinl链逼近算法中的一个
      • [135] C-H Teh and Roland T. Chin. On the detection of dominant points on digital curves. Pattern Analysis and Machine Intelligence, IEEE Transactions on, 11(8):859–872, 1989.
  • offSet: 每个轮廓点的可选偏移量,默认Point(), 当ROI图像中找出的轮廓需要在整个图中进行分析时,可利用这个参数。

绘制轮廓

void cv::drawContours    (    InputOutputArray     image,
                        InputArrayOfArrays     contours,
                        int     contourIdx,
                        const Scalar &     color,
                        int     thickness = 1,
                        int     lineType = LINE_8,
                        InputArray     hierarchy = noArray(),
                        int     maxLevel = INT_MAX,
                        Point     offset = Point() 
                      )    
  • image: 目标图像
  • contours: 输入轮廓,每个轮廓存储为一个点向量
  • contourIdx: 需要绘制的轮廓的编号,如果为负,绘制所有轮廓
  • color: 轮廓颜色
  • thickness: 轮廓线条粗细度,如果为负值(如thickness==cv_filled),绘制在轮廓内部
  • lineType: 线条类型
    • 8: 8连通线型
    • 4: 4连通线型
    • LINE_AA (OpenCV2: CV_AA): 抗锯齿线型
  • hierarchy: 可选层次结构
  • maxLevel: 绘制轮廓的最大等级
  • offset: 可选轮廓偏移参数

事例程序1

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>

// main
int main( int argc, char** argv )
{
    // loading image
    cv::Mat srcImage = cv::imread("1.jpg", 0);
    imshow("original image", srcImage);
    
    // initialize result image
    cv::Mat dstImage = cv::Mat::zeros(srcImage.rows, srcImage.cols, CV_8UC3);
    
    // thresholding image
    srcImage = srcImage > 119;
    imshow("thresholding image", srcImage);
    
    // finding contours
    std::vector<std::vector<cv::Point> > contours;
    std::vector<cv::Vec4i> hierarchy;
    // for opencv 2
    // cv::findContours(srcImage, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    // for opencv 3
    cv::findContours(srcImage, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);
    
    // iterate through all levels, and draw contours in random color
    int index = 0;
    for (; index>=0; index = hierarchy[index][0]) {
        cv::Scalar color(rand()&255, rand()&255, rand()&255);
        // for opencv 2
        // cv::drawContours(dstImage, contours, index, color,  CV_FILLED, 8, hierarchy);
        // for opencv 3
        cv::drawContours(dstImage, contours, index, color,  cv::FILLED, 8, hierarchy);
        
        imshow("contours", dstImage);
        cv::waitKey(150);
    }
    cv::imwrite("result.jpg", dstImage);
    return 0;
}

1.jpg

 

result.jpg

事例程序2

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <vector>

#define WINDOW_NAME1 "original image"
#define WINDOW_NAME2 "contours"

// global variables
cv::Mat g_srcImage;
cv::Mat g_grayImage;
cv::Mat g_cannyMat_output;
int g_nThresh = 80;
int g_nThresh_max = 255;
cv::RNG g_rng(12345);
std::vector<std::vector<cv::Point> > g_vContours;
std::vector<cv::Vec4i> g_vHierarchy;

// functions
void on_ThreshChange(int, void*);

// main
int main( int argc, char** argv )
{
    // change the text color of console
    system("color 1F");
    
    // loading image
    g_srcImage = cv::imread("1.jpg", 1);
    if (!g_srcImage.data){
        std::cerr << "ERROR while loading image." << std::endl;
        return false;
    }
    
    // convert to gray-scale and blur
    cv::cvtColor(g_srcImage, g_grayImage, cv::COLOR_BGR2GRAY);
    cv::blur(g_grayImage, g_grayImage, cv::Size(3,3));
    
    // create window
    cv::namedWindow(WINDOW_NAME1, cv::WINDOW_AUTOSIZE);
    imshow(WINDOW_NAME1, g_srcImage);
    
    // create tracker bar
    cv::createTrackbar("Canny Threshold", WINDOW_NAME1, &g_nThresh, g_nThresh_max, on_ThreshChange);
    on_ThreshChange(0, 0);
    
    cv::waitKey(0);
    return 0;
}

void on_ThreshChange(int, void*)
{
    cv::Canny(g_grayImage, g_cannyMat_output, g_nThresh, g_nThresh*2, 3);
    
    cv::findContours(g_cannyMat_output, g_vContours, g_vHierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
    
    cv::Mat drawing = cv::Mat::zeros(g_cannyMat_output.size(), CV_8UC3);
    for (int i = 0; i<g_vContours.size(); i++) {
        cv::Scalar color(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));
        cv::drawContours(drawing, g_vContours, i, color,  2, 8, g_vHierarchy);
    }
    imshow(WINDOW_NAME2, drawing);
}

结果图:

 

 

 

  

 

转载于:https://www.cnblogs.com/Xiaoyan-Li/p/5761510.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。 首先,你需要安装 OpenCV 库,并导入相应的模块。然后,你可以使用 `cv2.findContours()` 函数查找图像中的轮廓,该函数会返回一个轮廓列表和层次结构。 下面是一个例子,演示如何查找绘制轮廓,返回轮廓数和不同轮廓点数: ```python import cv2 # 读取图像 img = cv2.imread('image.png') # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化处理 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 查找轮廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 绘制轮廓 cv2.drawContours(img, contours, -1, (0, 255, 0), 3) # 返回轮廓数和不同轮廓点数 print('轮廓数:', len(contours)) for i in range(len(contours)): print('轮廓', i+1, '的点数:', len(contours[i])) # 显示图像 cv2.imshow('image', img) cv2.waitKey(0) ``` 在上面的代码中,我们首先读取一张图像,并将其转换为灰度图像。然后,我们使用 `cv2.threshold()` 函数对图像进行二值化处理,这是因为 `cv2.findContours()` 函数只能处理二值图像。接着,我们使用 `cv2.findContours()` 函数查找图像中的轮廓,其中 `cv2.RETR_TREE` 表示获取轮廓的整个层次结构,`cv2.CHAIN_APPROX_SIMPLE` 表示压缩水平、垂直和对角方向的像素,从而仅保留端点。 然后,我们使用 `cv2.drawContours()` 函数将轮廓绘制到原始图像上,其中 `-1` 表示绘制所有轮廓,`(0, 255, 0)` 表示轮廓的颜色,`3` 表示轮廓的线宽。 最后,我们返回轮廓数和不同轮廓点数,并在屏幕上显示图像。 希望这个例子能够帮助到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值