opencv仿射变换和查找轮廓


import cv2 as cv
import numpy as np
from pynput.mouse import Button, Controller  
print("start")
mouse = Controller()
count = 0
matsrc = np.float32([[226, 14], [193, 135], [439, 34], [432, 158]])
matdst = np.float32([[0, 0], [0, 1080], [1920, 0], [1920, 1080]])

kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (6, 6))


def callback(object):
    pass


imgi = np.zeros((50, 500), np.uint8)
cv.namedWindow("tj")
cv.imshow("tj", imgi)


cv.createTrackbar('erode', 'tj', 10, 50, callback)
cv.createTrackbar('LH', 'tj', 0, 179, callback)
cv.createTrackbar('LS', 'tj', 220, 255, callback)
cv.createTrackbar('LV', 'tj', 222, 255, callback)
cv.createTrackbar('HH', 'tj', 169, 179, callback)
cv.createTrackbar('HS', 'tj', 255, 255, callback)
cv.createTrackbar('HV', 'tj', 255, 255, callback)


def filter_hsv(src_frame):
    if src_frame is not None:
        hsv = cv.cvtColor(src_frame, cv.COLOR_BGR2HSV)
        LH = cv.getTrackbarPos("LH", 'tj')
        LS = cv.getTrackbarPos("LS", 'tj')
        LV = cv.getTrackbarPos("LV", 'tj')
        HH = cv.getTrackbarPos("HH", 'tj')
        HS = cv.getTrackbarPos("HS", 'tj')
        HV = cv.getTrackbarPos("HV", 'tj')
        lower_red = np.array([LH, LS, LV])
        upper_red = np.array([HH, HS, HV])

        mask = cv.inRange(hsv, lower_red, upper_red)
        return mask


def xy(event, x, y, flags, param):  
    global count
    global matsrc
    if event == cv.EVENT_LBUTTONUP:
        print(count)
        matsrc[count] = [x, y]
        print(x,y)
        print(matsrc)
        print("____________________")
        print(count)
        if count >= 0 and count < 3:
            count += 1
        else:
            count = 0


cv.namedWindow('img')
cv.setMouseCallback('img', xy)


def vedio_demo():
    global xiaodou
    capture = cv.VideoCapture(0)
    mataffine = cv.getPerspectiveTransform(matsrc, matdst)
    while(True):
        ref, frame = capture.read()
        dst = cv.warpPerspective(frame, mataffine, (1920, 1080))
        erode_v = cv.getTrackbarPos('erode', 'tj')
        mask = filter_hsv(dst)
        #img_dilate = cv.dilate(mask, kernel, iterations=6)
        #x, y, w, h = cv.boundingRect(img_dilate)
        closeMat = cv.morphologyEx(mask, cv.MORPH_CLOSE, np.ones((3, 3), np.uint8))
        xxx,contours, hierarchy = cv.findContours(closeMat, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
        maxArea = 0
        maxIndex = 0
        for i in range(len(contours)):
            x, y, w, h = cv.boundingRect(contours[i])
            if w*h < 30:
                continue
            xiaodou = np.append(xiaodou[1:], [(1 if x > 0 else 0)])
            # print(type(xiaodou))
            if np.sum(xiaodou) >= 1:
                mouse.click(Button.left, 1)
                xiaodou[xiaodou != 0] = 0 
            if x:
                cv.rectangle(dst, (x, y), (x+w+150, y+h+100), (0, 255, 0), 2)
                # set pointer postion
                mouse.position = (x+w+150, y+h+100)
                # double click
        cv.namedWindow("img", cv.WINDOW_NORMAL)
        cv.imshow("img", frame)
        cv.namedWindow("dst", cv.WINDOW_NORMAL)
        cv.imshow("dst", dst)
        # cv.imshow("img_erode", img_erode)
        cv.namedWindow("mask", cv.WINDOW_NORMAL)
        cv.imshow("mask", mask)
        #cv.imshow("img_dilate", img_dilate)
        cv.namedWindow("closeMat", cv.WINDOW_NORMAL)
        cv.imshow("closeMat", closeMat)

        c = cv.waitKey(30) & 0xff
        if c == 27:
            capture.release()
            break


xiaodou = np.zeros(100)
vedio_demo()
cv.waitKey(0)
cv.destroyAllWindows()

优化点

theRect = cv.minAreaRect(contours[i]) center = theRect[0]


FR:徐海涛(hunk xu)
QQ技术交流群:386476712

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值