首页 > 编程语言 >Python OpenCV4趣味应用系列(三)---内切圆/外接圆

Python OpenCV4趣味应用系列(三)---内切圆/外接圆

时间:2022-10-18 22:32:52浏览次数:55  
标签:src vert int cv 外接圆 result OpenCV4 np 内切圆


为了方便讲解,我们先来创建一个多边形做演示

第一步:创建图像,并绘制一个六边形。代码和生成图像如下:


# Create an image
r = 100
src = np.zeros((4*r, 4*r), dtype=np.uint8)

# Create a sequence of points to make a contour
vert = [None]*6
vert[0] = (3*r//2, int(1.34*r))
vert[1] = (1*r, 2*r)
vert[2] = (3*r//2, int(2.866*r))
vert[3] = (5*r//2, int(2.866*r))
vert[4] = (3*r, 2*r)
vert[5] = (5*r//2, int(1.34*r))
# Draw it in src
for i in range(6):
cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3)

cv.imshow("src", src)

Python OpenCV4趣味应用系列(三)---内切圆/外接圆_调用函数

第二步:查找轮廓,计算图像上的点到轮廓的距离


# Get the contours
_, contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

# Calculate the distances to the contour
raw_dist = np.empty(src.shape, dtype=np.float32)
for i in range(src.shape[0]):
for j in range(src.shape[1]):
raw_dist[i,j] = cv.pointPolygonTest(contours[1], (j,i), True)


注意cv.RETR_TREE查找轮廓后轮廓是从外到内的排列顺序,那么contours[1]就是六边形的内边轮廓

第三步:获取轮廓内部距离轮廓最远的点(作为内切圆圆心)和最小距离(作为内切圆半径),绘制内切圆


# 获取最大值即内接圆半径,中心点坐标
minVal, maxVal, _, maxDistPt = cv.minMaxLoc(raw_dist)
minVal = abs(minVal)
maxVal = abs(maxVal)

result = cv.cvtColor(src,cv.COLOR_GRAY2BGR)

cv.circle(result,maxDistPt, np.int(maxVal),(0,255,0), 2, cv.LINE_8, 0)
cv.imshow('result', result) 

最终效果:

Python OpenCV4趣味应用系列(三)---内切圆/外接圆_vim_02

当然,如果你还想获取外接圆,直接调用函数cv2.minEnclosingCircle(即可),下面是代码演示和效果:

center, radius = cv.minEnclosingCircle(contours[0])
cv.circle(result,(int(center[0]),int(center[1])),int(radius),(0,0,255),2)

Python OpenCV4趣味应用系列(三)---内切圆/外接圆_vim_03

完整代码:


import cv2 as cv
import numpy as np

# Create an image
r = 100
src = np.zeros((4*r, 4*r), dtype=np.uint8)

# Create a sequence of points to make a contour
vert = [None]*6
vert[0] = (3*r//2, int(1.34*r))
vert[1] = (1*r, 2*r)
vert[2] = (3*r//2, int(2.866*r))
vert[3] = (5*r//2, int(2.866*r))
vert[4] = (3*r, 2*r)
vert[5] = (5*r//2, int(1.34*r))
# Draw it in src
for i in range(6):
cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3)

cv.imshow("src", src)

# Get the contours
_, contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

# Calculate the distances to the contour
raw_dist = np.empty(src.shape, dtype=np.float32)
for i in range(src.shape[0]):
for j in range(src.shape[1]):
raw_dist[i,j] = cv.pointPolygonTest(contours[1], (j,i), True)

# 获取最大值即内接圆半径,中心点坐标
minVal, maxVal, _, maxDistPt = cv.minMaxLoc(raw_dist)
minVal = abs(minVal)
maxVal = abs(maxVal)

result = cv.cvtColor(src,cv.COLOR_GRAY2BGR)

center,radius = cv.minEnclosingCircle(contours[0])
cv.circle(result,(int(center[0]),int(center[1])),int(radius),(0,0,255),2)

cv.circle(result,maxDistPt, np.int(maxVal),(0,255,0), 2, cv.LINE_8, 0)
cv.imshow('result', result)

cv.waitKey(0)
cv.destroyAllWindows()

Python OpenCV4趣味应用系列(三)---内切圆/外接圆_vim_04


标签:src,vert,int,cv,外接圆,result,OpenCV4,np,内切圆
From: https://blog.51cto.com/stq054188/5768297

相关文章

  • Python OpenCV4趣味应用系列(四)---颜色物体实时检测
    今天,我们来实现一个视频实时检测颜色物体的小实例,视频中主要有三个颜色物体,我们只检测红色和绿色的球状物体,如下图所示:第一步需要打开视频(或者摄像头):cap=cv2.VideoCaptur......
  • OpenCV4.2使用viz模块显示3D图像
        在OpenCV3D视觉中如果需要显示三维数据或图像就需要用到viz模块,viz是OpenCV的3D显示模块,OpenCV官方release版本不包含此模块,需要我们自己cmake编译。  Cmake步......
  • 19、Opencv4.4的仿射矩阵处理
    基本思想:深入学习一下仿射矩阵的使用和分解环境window10+Mingw32+Opencv4.4.0+Eigen这里仅说明一下Eigen库的导入方法,首先去Eigen官网下载Eigen源码,解压导入Clion工程中,修......
  • 14、OpenCV4.4进行区域的相交比计算
    基本思想:进行两个区域的相交比计算  #include<opencv2/opencv.hpp>#include<opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<iostream>using......
  • 13、OpenCV4.4 仿射变换
    Parameters:src–Coordinatesoftriangleverticesinthesourceimage.dst–Coordinatesofthecorrespondingtriangleverticesinthedestinationimage.Thefunc......
  • 9、OpenCV4.4 色彩抽出
    色彩的空间变换#-*-coding:utf-8-*-importcv2importnumpyasnporiginal=cv2.imread('1.jpg',1)img=original.copy()#副本dst=np.zeros(img.shape,np.uint8)#......
  • 代码笔记24 windows+opencv4.5.5安装中出现的IPPICV: Download: ippicv_2020_win_inte
    1环境介绍:windows10,visualstudio2019,CMake,OPENCV4.5.5出现问题IPPICV:Download:ippicv_2020_win_intel64_20191018_general.zipTry1failedCMakeWarninga......
  • 20220830-hi3516交叉编译opencv4.6.0
    1. 下载opencv4.6.0并解压https://opencv.org/releases/unzipopencv-4.6.0.zipcdopencv-4.6.0 2. 安装基础工具sudoapt-getinstallbuild-essentialsudoapt......
  • opencv4.x 中的plot函数绘制二维Mat
     发现一个好玩的二维图像绘制函数,与大家共同欣赏:)参考网址:OpenCV4入门061:使用plot2d绘制折线图-食铁兽(feater.top) 头文件:#include<opencv2/plot.hpp>动态库:-l......