# 图像轮廓
# 图像轮廓是具有相同颜色或者灰度的连续点的曲线
# 轮廓的作用
# 用于图像分析
# 物体的识别和检测
# 注意点:
# 为了检测准确性,需要先对图像进行二值化或者canny边缘检测
# 画轮廓时会修改输入法人图像,如果之后想继续使用原图像,应该将原始图像存储到其他变量之中。
# 查找轮廓
# findContours(image,mode,method)
# mode:查找轮廓的模式
# RETR_EXTERNAL = 0 表示只查找外围轮廓
# method轮廓近似方法也叫ApproximationMdode
# CHAIN_APPROX_NONE:保存所有轮廓上的点
# CHAIN_APPROX_SIMPLE:只保存角点
# 返回两个值,一个contours,一个hierarchy即轮廓和层级
import cv2 as cv
import numpy as np
img = cv.imread("./contours.jpg")
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
kernel = cv.getStructuringElement(cv.MORPH_RECT,(5,5))
dst = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY_INV,3,0)
contours,hierarchy = cv.findContours(dst,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)
cv.imshow('img', np.hstack(( gray,dst)))
cv.waitKey(0)
cv.destroyAllWindows()
# 绘制轮廓
# drawContours(image,contours,contouldx,color,thickness)
# image:要绘制的轮廓图像
# contours:轮廓点
# contourldx:要绘制的轮廓的编号,-1表示绘制所有轮廓
# thickness:-1表示全部填充
import cv2 as cv
import numpy as np
img = cv.imread("./contours.jpg")
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,3))
dst = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,3,0)
# 1-查找轮廓
contours,hierarchy = cv.findContours(dst,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)
# 2-画出轮廓
cv.drawContours(img,contours,0,(0,255,0),2)
cv.imshow('img',img)
cv.waitKey(0)
cv.destroyAllWindows()
# 计算轮廓的周长和面积
# 轮廓面积是指每个轮廓中所有像素点围成的区域面积,单位为像素
# 在查找轮廓后,可能会出现很多细小的轮廓,可以通过轮廓的面积进行过滤
# contourArea(contour)
# arcLength(curve,closed)
# curve就是轮廓
# closed是否闭合的轮廓
import cv2 as cv
import numpy as np
img = cv.imread("./contours.jpg")
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,3))
dst = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,3,0)
contours,hierachy = cv.findContours(dst,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)
cv.drawContours(img,contours,-1,(0,0,255),2)
while True:
for i in range(len(contours)):
# 计算面积
area = cv.contourArea(contours[i])
print(area)
# 计算周长
length = cv.arcLength(contours[i],closed = True)
print(length)
cv.imshow('img',img)
cv.waitKey(0)
cv.destroyAllWindows()
# 多边形逼近与凸包
# 查找轮廓后的轮廓信息过于复杂不平滑,可以用多边形逼近函数对多边形曲线做适当近似,这就是多边形逼近
# 核心就是不断找多边形最远的点加入形成新的多边形,知道最短距离小于指定的精度。
# approxPolyDP(curve,epsilon,closed)
# curve:要近似逼近的轮廓
# epsilon:即DP算法使用的阈值
import cv2 as cv
import numpy as np
img = cv.imread("./hand.png")
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
thresh,dst = cv.threshold(gray,127,255,cv.THRESH_BINARY)
contours,hierarchy = cv.findContours(dst,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(img,contours,0,(0,0,255),2)
approx = cv.approxPolyDP(contours[0],20,True)
cv.drawContours(img,[approx],-1,(0,255,255),2)
# 凸包指的是完全包含原有轮廓,并且仅由轮廓上的点所构成的多边形
# 凸包的每一处都是凸的,即在凸包内连接任意两点的直线都在凸包的内部,在凸包内,任意连续三个点的内角小于180°
hull = cv.convexHull(contours[0])
cv.drawContours(img,[hull],-1,(100,100,255),2)
cv.imshow('img',img)
cv.waitKey(0)
cv.destroyAllWindows()
# 外接矩形
# 外接矩形分为最小外接矩形和最大外接矩形
# 最小外接矩形
# minAreaRect(points)
# points:就是轮廓 返回元组,内容是一个旋转矩形的参数(起始坐标x,y,宽高,旋转角度)
# 最大外接矩形
# boudingRect(points)
import cv2 as cv
import numpy as np
# 1-导入图片
hand = cv.imread("./hello.png")
# 2-转化为灰度图
gray = cv.cvtColor(hand,cv.COLOR_BGR2GRAY)
# 3-二值化
thresh,dst = cv.threshold(gray,127,255,cv.THRESH_BINARY)
# 4-查找轮廓
contours, hierarachy = cv.findContours(dst,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
# 5-绘制轮廓
# cv.drawContours(hand,contours,0,(0,255,0),2)
# 6-绘制最小外接旋转矩形
rect = cv.minAreaRect(contours[1])
# rect是一个旋转矩形,矩形的起始坐标(x,y)和,矩形的长宽,矩形的旋转角度
# print(rect)
# 7-绘制旋转矩形
points = cv.boxPoints(rect)
# 其实就是帮我们把旋转矩阵的4个坐标点计算出来了
# print(points)
# 注意坐标必须是整数,所以这里的坐标需要转化一下
# int0直接取整数,不做四舍五入
# points = np.int0(points)
# 采用四舍五入
# points = np.int0(np.round(points))
# 或者
# points = np.round(points).astype("int64")
# cv.drawContours(hand,[points],0,(255,0,255),2)
# 8-绘制最大外接矩形,返回值是最大外接矩形的 x,y,w,h
x,y,w,h = cv.boundingRect(contours[1])
cv.rectangle(hand,(x,y),(x+w,y+h),(0,255,255),2)
cv.imshow("img",hand)
cv.waitKey(0)
cv.destroyAllWindows()