python opencv检测高光点及findContours计算连通域重心

在对二值化图像的连通域进行分析时,重心,对称轴等参数是重要特征。现对连通域重心计算的python代码实现,归纳总结如下,欢迎大家批评指正。重心是距离对重量的加权平均。用数学语言描述即为:
在这里插入图片描述
离散化后为:
在这里插入图片描述
对于二值化图像中的连通域,属于均匀物体,每一点像素值相等,所以重心公式如下:
在这里插入图片描述
x_i , y_i分别为连通域中像素点的坐标。当然更方便的方法是利用现有的函数接口,代码如下:

import cv2
import numpy as np
from skimage import data,segmentation,measure,morphology,color
import glob
import matplotlib.pyplot as plt
# images = glob.glob("./*.jpg")
images = glob.glob("./inputdata/highlight/*.bmp")
num_img = len(images)
Coord_highlight = np.zeros((4,2,num_img))
CoordInWorld = np.zeros((4,2,num_img))
num_img = 0
for fname in images:
    img = cv2.imread(fname)
    img_copy = img.copy()
    plt.figure()
    plt.subplot(221)
    plt.imshow(img[:,:,[2,1,0]])   # plt的图像通道和opencv图像通道相反,前者为rgb,后者则为bgr。
    plt.title('ori_img')
    plt.subplot(222)
    plt.imshow(img_copy[:,:,[2,1,0]])
    plt.title('ori_img_copy')

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    gray=cv2.GaussianBlur(gray, (9, 9), 0)

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(8,8))#圆结构元素
    gray = cv2.erode(gray,kernel,iterations = 1)
    gray = cv2.dilate(gray,kernel,iterations = 1)

    retval, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)#阈值的选择
    # method1   利用函数接口
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for index,c in enumerate(contours):
        # calculate moments for each contour
        M = cv2.moments(c)
        # calculate x,y coordinate of center
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])
        cv2.circle(img, (cX, cY), 10, (0, 0, 255), -1)
        print(index,cX,cY)

    # method2   利用公式计算
    label_image = measure.label(thresh)  #连通区域标记
    print('##########################')
    for index,region in enumerate(measure.regionprops(label_image)):
        points = region.coords  #得到的是按列 行
        cX = int(np.mean(points[:,1]))   # 求均值即可
        cY = int(np.mean(points[:,0]))
        cv2.circle(img_copy, (cX, cY), 10, (255, 0, 255), -1)
        print(index,cX,cY)

    plt.subplot(223)
    plt.imshow(img[:,:,[2,1,0]])
    plt.title('post_img')

    plt.subplot(224)
    plt.imshow(img_copy)
    plt.title('post_img_copy')
    plt.show()

    cv2.namedWindow('img',0)
    cv2.imshow('img',img)
    cv2.namedWindow('img_copy',0)
    cv2.imshow('img_copy',img_copy)
    cv2.waitKey(0)


在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`cv2.findContours()` 是 OpenCV 中用于查找图像轮廓的函数,它可以返回一个包含所有轮廓的列表。该函数需要输入一个二值化图像(一般为灰度图像或者二值图像),可以通过调节阈值或者其他图像处理算法获得。在函数的输出中,每个轮廓都是一个点集,可以使用 `cv2.drawContours()` 函数将其绘制出来。 在 OpenCV 4.7.0 中,`cv2.findContours()` 函数的参数列表为: ``` python contours, hierarchy = cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) ``` 其中: - `image`:输入的二值化图像。 - `mode`:轮廓检索模式,决定如何检索轮廓。可以选择 `cv2.RETR_EXTERNAL`,表示只检测最外层轮廓;`cv2.RETR_LIST`,表示检测所有轮廓并将它们存储在列表中;`cv2.RETR_CCOMP`,表示检测所有轮廓并将它们组织为两级层次结构;`cv2.RETR_TREE`,表示检测所有轮廓并将它们组织为树形结构。默认值为 `cv2.RETR_EXTERNAL`。 - `method`:轮廓逼近方法,决定如何逼近轮廓。可以选择 `cv2.CHAIN_APPROX_NONE`,表示不逼近,将轮廓上的所有点存储下来;`cv2.CHAIN_APPROX_SIMPLE`,表示逼近,只存储轮廓的端点;`cv2.CHAIN_APPROX_TC89_L1` 和 `cv2.CHAIN_APPROX_TC89_KCOS`,表示使用 Teh-Chin 链逼近算法。默认值为 `cv2.CHAIN_APPROX_NONE`。 - `contours`:输出的轮廓列表,如果不传入,则返回所有轮廓。 - `hierarchy`:输出的轮廓层次结构,如果不传入,则不输出。 - `offset`:可选参数,表示轮廓点集的偏移量。 示例代码: ``` python import cv2 img = cv2.imread('example.jpg') 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, 0, 255), 2) cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这段代码读入一张图片,将其转换为灰度图像并进行二值化处理,然后使用 `cv2.findContours()` 函数查找轮廓,并将轮廓绘制在原图像上。最后显示原图像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值