OpenCV 3 drawContours()

2023-11-22 18:50
文章标签 opencv drawcontours

本文主要是介绍OpenCV 3 drawContours(),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

OpenCV 3 drawContours()

OpenCV documentation index - OpenCV 文档索引
https://www.docs.opencv.org/

master (4.x)
https://www.docs.opencv.org/master/

3.4 (3.4.x)
https://www.docs.opencv.org/3.4/

2.4 (2.4.x)
https://www.docs.opencv.org/2.4/

Welcome to OpenCV-Python Tutorials’s documentation!
https://opencv-python-tutroals.readthedocs.io/en/latest/index.html

1. 3.4 (3.4.x) -> Modules -> Image Processing -> Drawing Functions -> drawContours()

1.1 Function Documentation - drawContours()

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() 
)

Python

image = cv.drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset]]]]])
#include <opencv2/imgproc.hpp>
contour [ˈkɒntʊə(r)]:n. 轮廓,等高线,周线,电路,概要 vt. 画轮廓,画等高线
fill [fɪl]:vt. 装满,使充满,满足,堵塞,任职 vi. 被充满,膨胀 n. 满足,填满的量,装填物
outline [ˈaʊtlaɪn]:n. 轮廓,大纲,概要,略图 vt. 概述,略述,描画...轮廓

Draws contours outlines or filled contours.

The function draws contour outlines in the image if thickness ≥ \geq 0 or fills the area bounded by the contours if thickness < 0 . The example below shows how to retrieve connected components from the binary image and label them:
如果 thickness ≥ \geq 0,则该函数在图像中绘制 contour 轮廓。如果 thickness < 0,则该功能填充轮廓所包围的区域。下面的示例显示了如何从二进制映像中检索连接的组件并对其进行标记:

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{Mat src;// the first command-line parameter must be a filename of the binary// (black-n-white) imageif( argc != 2 || !(src=imread(argv[1], 0)).data)return -1;Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);src = src > 1;namedWindow( "Source", 1 );imshow( "Source", src );vector<vector<Point> > contours;vector<Vec4i> hierarchy;findContours( src, contours, hierarchy,RETR_CCOMP, CHAIN_APPROX_SIMPLE );// iterate through all the top-level contours,// draw each connected component with its own random colorint idx = 0;for( ; idx >= 0; idx = hierarchy[idx][0] ){Scalar color( rand()&255, rand()&255, rand()&255 );drawContours( dst, contours, idx, color, FILLED, 8, hierarchy );}namedWindow( "Components", 1 );imshow( "Components", dst );waitKey(0);
}

Parameters
image - Destination image. (要绘制轮廓的图像。)
contours - All the input contours. Each contour is stored as a point vector. (所有输入的轮廓,每个轮廓被保存成一个 point vector。)
contourIdx - Parameter indicating a contour to draw. If it is negative, all the contours are drawn. (指定要绘制轮廓的编号,如果是负数,则绘制所有的轮廓。)
color - Color of the contours. (绘制轮廓所用的颜色。)
thickness - Thickness of lines the contours are drawn with. If it is negative (for example, thickness=FILLED ), the contour interiors are drawn. (绘制轮廓的线的粗细,如果是负数,则轮廓内部被填充。)
lineType - Line connectivity. See LineTypes (绘制轮廓的线的连通性。)
hierarchy - Optional information about hierarchy. It is only needed if you want to draw only some of the contours (see maxLevel ). (关于层级的可选参数,只有绘制部分轮廓时才会用到。)
maxLevel - Maximal level for drawn contours. If it is 0, only the specified contour is drawn. If it is 1, the function draws the contour(s) and all the nested contours. If it is 2, the function draws the contours, all the nested contours, all the nested-to-nested contours, and so on. This parameter is only taken into account when there is hierarchy available. (绘制轮廓的最高级别,这个参数只有hierarchy有效的时候才有效。maxLevel=0,绘制与输入轮廓属于同一等级的所有轮廓即输入轮廓和与其相邻的轮廓。maxLevel=1,绘制与输入轮廓同一等级的所有轮廓与其子节点。maxLevel=2,绘制与输入轮廓同一等级的所有轮廓与其子节点以及子节点的子节点。)
offset - Optional contour shift parameter. Shift all the drawn contours by the specified offset=(dx,dy).

interior [ɪnˈtɪəriə(r)]:n. 内部,里面,内景,内陆,腹地,内政,内务,本质 adj. 内部的,里面的,内位的,内陆的,腹地的,内务的,内政的,心灵的,精神的,本质的

When thickness=FILLED, the function is designed to handle connected components with holes correctly even when no hierarchy date is provided. This is done by analyzing all the outlines together using even-odd rule. This may give incorrect results if you have a joint collection of separately retrieved contours. In order to solve this problem, you need to call drawContours separately for each sub-group of contours, or iterate over the collection using contourIdx parameter.

1.2 Rotated Rectangle

Here, bounding rectangle is drawn with minimum area, so it considers the rotation also. The function used is cv2.minAreaRect(). It returns a Box2D structure which contains following detals - (the center point (mass center) (x,y), (width, height), angle of rotation). But to draw this rectangle, we need 4 corners of the rectangle. It is obtained by the function cv2.boxPoints()
在这里,边界矩形是用最小面积绘制的,因此它也考虑了旋转。使用的函数是 cv2.minAreaRect()。它返回一个 Box2D 结构,其中包含以下细节- (the center point (mass center) (x,y), (width, height), angle of rotation)。但是要绘制此矩形,我们需要矩形的 4 个角。它是通过函数 cv2.boxPoints() 获得的。

center - The rectangle mass center. (矩形质心。)
size - Width and height of the rectangle.
angle - The rotation angle in a clockwise direction. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle. (顺时针方向的旋转角度。当角度为 0、90、180、270 等时,该矩形变为直立矩形。)

rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
im = cv2.drawContours(im,[box],0,(0,0,255),2)

Both the rectangles are shown in a single image. Green rectangle shows the normal bounding rect. Red rectangle is the rotated rect.
两个矩形都显示在单个图像中。绿色矩形显示正常的边界矩形。红色矩形是旋转的矩形。

在这里插入图片描述

在这里插入图片描述

2. Example

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Chengfrom __future__ import absolute_import
from __future__ import print_function
from __future__ import divisionimport os
import syssys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))import numpy as np
# import tensorflow as tf
import cv2
import timedef inference(image_file, current_directory):img = cv2.imread(image_file, cv2.IMREAD_COLOR)# get dimensions of imagedimensions = img.shape# height, width, number of channels in imageheight = img.shape[0]width = img.shape[1]channels = img.shape[2]print('Image Dimension    : ', dimensions)print('Image Height       : ', height)print('Image Width        : ', width)print('Number of Channels : ', channels)# (the center point (mass center) (x,y), (width, height), angle of rotation)center = (800, 400)size = (300, 600)angle = 45rect = (center, size, angle)print("(the center point (mass center) (x,y), (width, height), angle of rotation):", rect)box = cv2.boxPoints(rect)print("box = cv2.boxPoints(rect):", box)box = np.int0(box)print("box = np.int0(box):", box)img = cv2.drawContours(img, [box], 0, (255, 0, 0), 2)box0 = box[0]box1 = box[1]box2 = box[2]box3 = box[3]cv2.rectangle(img, pt1=(center[0] - 6, center[1] - 6), pt2=(center[0] + 6, center[1] + 6), color=(0, 0, 255),thickness=-1)cv2.putText(img, text=" mass center: " + str(center), org=center, fontFace=0, fontScale=0.8, thickness=2,color=(0, 255, 0))cv2.putText(img, text="box0: " + str(box0), org=tuple(box0), fontFace=0, fontScale=0.8, thickness=2,color=(0, 255, 0))cv2.putText(img, text="box1: " + str(box1), org=tuple(box1), fontFace=0, fontScale=0.8, thickness=2,color=(0, 255, 0))cv2.putText(img, text="box2: " + str(box2), org=tuple(box2), fontFace=0, fontScale=0.8, thickness=2,color=(0, 255, 0))cv2.putText(img, text="box3: " + str(box3), org=tuple(box3), fontFace=0, fontScale=0.8, thickness=2,color=(0, 255, 0))tmp_directory = current_directory + "/tmp"if not os.path.exists(tmp_directory):os.makedirs(tmp_directory)cv2.namedWindow("Press ESC on keyboard to exit.", cv2.WINDOW_NORMAL)# Display the resulting framecv2.imshow("Press ESC on keyboard to exit.", img)k = cv2.waitKey(0)if k == 27:  # wait for ESC key to exitpasselif k == ord('s'):  # wait for 's' key to save and exitimage_name = "%s/%s.jpg" % (tmp_directory, "source_image")cv2.imwrite(image_name, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])# When everything done, release the capturecv2.destroyAllWindows()if __name__ == '__main__':image_file = "./tmp/000505.jpg"os.environ["CUDA_VISIBLE_DEVICES"] = '0'print("os.environ['CUDA_VISIBLE_DEVICES']:", os.environ['CUDA_VISIBLE_DEVICES'])inference(image_file, current_directory)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
os.environ['CUDA_VISIBLE_DEVICES']: 0
Image Dimension    :  (1080, 1920, 3)
Image Height       :  1080
Image Width        :  1920
Number of Channels :  3
(the center point (mass center) (x,y), (width, height), angle of rotation): ((800, 400), (300, 600), 45)
box = cv2.boxPoints(rect): [[ 481.80197  506.066  ][ 906.066     81.80195][1118.198    293.934  ][ 693.934    718.19806]]
box = np.int0(box): [[ 481  506][ 906   81][1118  293][ 693  718]]Process finished with exit code 0

在这里插入图片描述

这篇关于OpenCV 3 drawContours()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/412176

相关文章

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

opencv 滚动条

参数介绍:createTrackbar( trackbarname , "hello" , &alpha_slider ,alpha_max ,  on_trackbar )  ;在标签中显示的文字(提示滑动条的用途) TrackbarName创建的滑动条要放置窗体的名字 “hello”滑动条的取值范围从 0 到 alpha_max (最小值只能为 zero).滑动后的值存放在

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

OpenCV结构分析与形状描述符(11)椭圆拟合函数fitEllipse()的使用

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C++11 算法描述 围绕一组2D点拟合一个椭圆。 该函数计算出一个椭圆,该椭圆在最小二乘意义上最好地拟合一组2D点。它返回一个内切椭圆的旋转矩形。使用了由[90]描述的第一个算法。开发者应该注意,由于数据点靠近包含的 Mat 元素的边界,返回的椭圆/旋转矩形数据

树莓派5_opencv笔记27:Opencv录制视频(无声音)

今日继续学习树莓派5 8G:(Raspberry Pi,简称RPi或RasPi)  本人所用树莓派5 装载的系统与版本如下:  版本可用命令 (lsb_release -a) 查询: Opencv 与 python 版本如下: 今天就水一篇文章,用树莓派摄像头,Opencv录制一段视频保存在指定目录... 文章提供测试代码讲解,整体代码贴出、测试效果图 目录 阶段一:录制一段

Verybot之OpenCV应用三:色标跟踪

下面的这个应用主要完成的是Verybot跟踪色标的功能,识别部分还是居于OpenCV编写,色标跟踪一般需要将图像的颜色模式进行转换,将RGB转换为HSV,因为对HSV格式下的图像进行识别时受光线的影响比较小,但是也有采用RGB模式来进行识别的情况,这种情况一般光线条件比较固定,背景跟识别物在颜色上很容易区分出来。         下面这个程序的流程大致是这样的:

Verybot之OpenCV应用二:霍夫变换查找圆

其实我是想通过这个程序来测试一下,OpenCV在Verybot上跑得怎么样,霍夫变换的原理就不多说了,下面是程序: #include "cv.h"#include "highgui.h"#include "stdio.h"int main(int argc, char** argv){cvNamedWindow("vedio",0);CvCapture* capture;i

Verybot之OpenCV应用一:安装与图像采集测试

在Verybot上安装OpenCV是很简单的,只需要执行:         sudo apt-get update         sudo apt-get install libopencv-dev         sudo apt-get install python-opencv         下面就对安装好的OpenCV进行一下测试,编写一个通过USB摄像头采

虚拟机ubuntu配置opencv和opencv_contrib

前期准备  1.下载opencv和opencv_contrib源码 opencv-4.6.0:https://opencv.org/releases/ opencv_contrib-4.6.0:https://github.com/opencv/opencv_contrib 在ubuntu直接下载或者在window上下好传到虚拟机里都可以 自己找个地方把他们解压,个人习惯在home下新建一

Windows下使用cmake编译OpenCV

Windows下使用cmake编译OpenCV cmake下载OpenCV下载编译OpenCV cmake下载 下载地址:https://cmake.org/download/ 下载完成,点击选择路径安装即可 OpenCV下载 下载地址:https://github.com/opencv/opencv/releases/tag/4.8.1因为我们是编译OpenCV,下图选择