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

最低0.47元/天 解锁文章
466

被折叠的 条评论
为什么被折叠?



