001 摄像头拍照+旋转+截取部分+计算棋盘的四个角点坐标+四点定位拉伸

001 摄像头拍照+旋转+截取部分+计算棋盘的四个角点坐标+四点定位拉伸

这样,顶上的摄像头即便稍有一些移位或歪斜,也不影响了。

最终得到的是棋盘的规范图,为后续识别做好了准备。

# -*- coding: utf-8 -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image


def imgCapture(ImageName):
    print("拍照")
    #video="http://admin:admin@192.168.1.109:8081/" 
    #cap=cv2.VideoCapture(video)
    #测试过用手机当ip摄像头了,别的都还好,就是拍照分辨率不能调整,所以还是用usb有线摄像头
    cap=cv2.VideoCapture(0)    
    cap.set(3,1920)
    cap.set(4,1080)
    #cap.set(10,1)  #亮度参数 感觉其实没什么用
    i=10
    while i>0:
        i=i-1
        ret, frame = cap.read() 
    cap.release()
    #time.sleep(3) # 有些时候 USB摄像头不知道为什么拍的一片漆黑 猜测需要略休眠 实测无效
    
    frame = rotate_bound(frame, 180) #旋转角度180度
    cv2.imwrite(ImageName,frame, [int( cv2.IMWRITE_JPEG_QUALITY), 100]) # 默认95

#旋转图像的函数
def rotate_bound(image, angle):
    print("旋转图片")
    # grab the dimensions of the image and then determine the
    # center
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)
    # grab the rotation matrix (applying the negative of the
    # angle to rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])
    # compute the new bounding dimensions of the image
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))
    # adjust the rotation matrix to take into account translation
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY
    # perform the actual rotation and return the image
    #return cv2.warpAffine(image, M, (nW, nH))
    return cv2.warpAffine(image, M, (nW, nH),borderValue=(255,255,255))


# 切出矩形棋盘的一系列函数 开始
# 参考 https://blog.youkuaiyun.com/sinat_36458870/article/details/78825571

def get_image(path):
    #获取图片
    img=cv2.imread(pat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值