opencv_day7

import cv2
import matplotlib.pylab as plt
import numpy as np
img1 = cv2.imread('D:\\gudugudu\\Pictures\\weixin\\xiaotidaya.jpg', cv2.IMREAD_ANYCOLOR)
img2 = cv2.imread('D:\\gudugudu\\Pictures\\weixin\\guoqing.jpg', cv2.IMREAD_ANYCOLOR)
img3 = cv2.imread('D:\\gudugudu\\Pictures\\weixin\\zhaoyang.jpg', cv2.IMREAD_ANYCOLOR)
img_x = cv2.resize(img1, (500, 500))
img_y = cv2.resize(img2, (500, 500))
img_z = cv2.resize(img3, (500, 500))
vc = cv2.VideoCapture("D:\\gudugudu\\Pictures\\weixin\\WeChat_20240706150425.mp4")
gray = cv2.cvtColor(img_y, cv2.COLOR_BGR2GRAY)


def drawImg(img):
    ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    draw_img = img.copy()
    res = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 3)
    cv2.imshow('gudu', res)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


#轮廓特征
def contour(img):
    ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cv2.contourArea(binary)#面积
    cv2.arcLength(binary, True)#周长


#轮廓的近似
def Contours(img):
    ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    draw_img = img.copy()
    cnt = contours[0]
    epsilon = 1 * cv2.arcLength(cnt, True)
    approx = cv2.approxPolyDP(cnt, epsilon, True)
    res = cv2.drawContours(draw_img, [approx], -1, (0, 0, 255), 2)
    cv2.imshow('gudu', res)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


#边界矩形
def juxing(img):
    ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cnt = contours[3]
    x, y, w, h = cv2.boundingRect(cnt)
    img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    area = cv2.contourArea(cnt)#计算边缘矩形的面积
    area_rect = w * h
    extent = float(area) / area_rect
    print(extent)
    cv2.imshow('gudu', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


#模板匹配
'''
    从第一个开始从左到右,从上到下对模板进行匹配;
    调用模板匹配的方法,用归一化的结果
'''


def Methods(img, img1):
    ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cnt = contours[3]
    x, y, w, h = cv2.boundingRect(cnt)
    template = img1
    methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
    for meth in methods:
        img2 = img.copy()
        method = eval(meth)
        print(method)
        res = cv2.matchTemplate(img, template, method)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
            top_left = min_loc
        else:
            top_left = max_loc
        bottom_right = (top_left[0] + w, top_left[1] + h)
        cv2.rectangle(img2, top_left, bottom_right, 255, 2)
        plt.subplot(121), plt.imshow(res, cmap='gray')
        plt.xticks([]), plt.yticks([]) #隐藏坐标轴
        plt.subplot(122), plt.imshow(img2, cmap='gray')
        plt.xticks([]), plt.yticks([])
        plt.show()


#匹配多个对象
def many(img, img1):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    template = img1
    ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cnt = contours[3]
    x, y, w, h = cv2.boundingRect(cnt)
    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    threshold = 0.8
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        bottom_right = (pt[0] + w, pt[1] + h)
        cv2.rectangle(img, pt, bottom_right, (0, 0, 255), 2)
    cv2.imshow('img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值