Python视觉识别--OpenCV圆检测\轮廓发现\对象测量\膨胀与腐蚀(八)

(二十一) 圆检测

注意: 图像的高宽一定要相等。

import cv2 as cv
import numpy as np

def detect_circles(img):
    dst = cv.pyrMeanShiftFiltering(img, 10, 100)
    cimg = cv.cvtColor(dst, cv.COLOR_BGR2GRAY)
    circles = cv.HoughCircles(cimg, cv.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)
    circles = np.uint16(np.around(circles))
    for i in circles[0, :]:
        cv.circle(img, (i[0], i[1]), i[2], (0, 0, 255), 2)
        cv.circle(img, (i[0], i[1]), 2, (255, 0, 0), 2)
    cv.imshow("c", img)


src = cv.imread('images/timg.jpg')
cv.imshow('def', src)
detect_circles(src)
cv.waitKey(0)
cv.destroyAllWindows()

圆检测

(二十二) 轮廓发现

是基于图像边缘提取的基础,寻找对象轮廓的方法,所以边缘提取的阈值选定会影响最终轮廓的发现

轮廓发现API
findContours 发现轮廓
drawContours绘制轮廓

import cv2 as cv

def contours(image):
    # 高斯模糊,消除噪声
    dst = cv.GaussianBlur(image,(9,9),15)

    # 先变灰度图像
    gray = cv.cvtColor(dst,cv.COLOR_BGR2GRAY)

    #OTSU大律法获取二值图像
    #args
    #   输入图像
    #   阈值(为0是全局自适应阈值, 参数0可改为任意数字但不起作用)
    #   与THRESH_BINARY和THRESH_BINARY_INV阈值类型一起使用设置的最大值。
    #   阈值类型
    ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)
    cv.imshow('binary image',binary)

    #寻找轮廓(直接输入二值图像)
    #args:
    #   输入的二值图像
    #   轮廓检索模式  RETR_TREE:提取所有轮廓并重新建立网状轮廓结构
    #   轮廓近似方法  CHAIN_APPROX_SIMPLE:压缩水平方向,垂直方向,对角线方向的元素,值保留该方向的重点坐标,如果一个矩形轮廓只需4个点来保存轮廓信息
    #return:
    #   传入的二值图像(ndarray)
    #   list:轮廓本身,含有轮廓上面各个点的位置信息
    #   每条轮廓对应的属性(ndarray)
    # 错误
    # ValueError: not enough values to unpack (expected 3, got 2)
    # 原因为返回值是两个,用3个接受
    # cloneimage,contours,heriachy = cv.findContours(binary,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)  #RETR_TREE包含检测内部
    contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)  # RETR_TREE包含检测内部
    # cloneImage, contours, heriachy = cv.findContours(binary, cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)  # RETR_EXTERNAL检测外部轮廓
    for i ,contour in enumerate(contours):
        cv.drawContours(image,contours,i,(0,0,255),2)      #绘制轮廓
        # cv.drawContours(image,contours,i,(0,0,255),-1)      #填充轮廓
        print(i)
    cv.imshow('detect image',image)

src = cv.imread('images/money.jpg')
cv.imshow('input image',src)
contours(src)
cv.waitKey(0)
cv.destroyAllWindows()

注意:
findContours的返回值。


原图
二值化
绘制轮廓

(二十三) 对象测量

import cv2 as cv
import numpy as np


def measure_object(img):
    gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    cv.imshow("binary img", binary)
    # outImg, contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    for i, contour in enumerate(contours):
        # 轮廓面积
        area = cv.contourArea(contour)
        # 轮廓外接矩形面积
        area = cv.contourArea(contour)
        x, y, w, h = cv.boundingRect(contour)
        # 几何矩
        mm = cv.moments(contour)
        cx = mm['m10'] / mm['m00']
        cy = mm['m01'] / mm['m00']
        #中心绿点
        cv.circle(img, (np.int(cx), np.int(cy)), 3, (0, 255, 0), -1)
        #红框
        cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
    cv.imshow("mo", img)

src = cv.imread('images/money.jpg')
cv.imshow('def', src)
measure_object(src)
cv.waitKey(0)
cv.destroyAllWindows()
对象测量

(二十四) 膨胀与腐蚀

膨胀就是求局部最大值的操作
腐蚀就是求局部最小值的操作
膨胀与腐蚀都支持任意形状的结构元素

灰度与二值图像处理中重要的手段;

import cv2 as cv

def erode(img):
    gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    # 获得结构元素
    # 第一个参数:结构元素形状,这里是矩形
    # 第二个参数:结构元素大小
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
    # 执行腐蚀
    dst = cv.erode(binary, kernel)
    cv.imshow("erode", dst)


def dilate(img):
    gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    # 获得结构元素
    # 第一个参数:结构元素形状,这里是矩形
    # 第二个参数:结构元素大小
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
    # 执行膨胀
    dst = cv.dilate(binary, kernel)
    cv.imshow("dilate", dst)


src = cv.imread('images/template.jpg')
cv.imshow('def', src)
erode(src)
dilate(src)
cv.waitKey(0)
cv.destroyAllWindows()

膨胀与腐蚀
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容