程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python CV2 encapsulates photographing, intercepting, rotating and flipping into a function

編輯:Python

The following method implements , Get pictures from the camera , And flip or rotate according to the conditions

import cv2
# flip_x, flip_y Decide whether to proceed x The axis turns y The axis turns 
# clip = (x,y,w,h); Coordinates and width height of upper left corner Determines the square area to be cut ; If you don't cut it, set it all 0
# rotate: 0 No rotation ; 1 Turn clockwise 90 degree ; 2 rotate 180 degree ; -1 Counter clockwise rotation 90 degree 
def take_picture(flip_x=False, flip_y=False, clip=(0, 0, 100, 100), rotate=0):
clip_x, clip_y, clip_w, clip_h = clip
cap = cv2.VideoCapture(0)
# print("set pic width: ", cap.set(3, 3664))
# print("set pic height: ", cap.set(4, 2748), rotate, flip_x)
res, frame = cap.read()
cap.release()
if res:
cv2.imwrite("images/origin.png", frame)
if flip_x:
frame = cv2.flip(frame, 1)
if flip_y:
frame = cv2.flip(frame, 0)
if rotate:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE if rotate == 1 else cv2.ROTATE_90_COUNTERCLOCKWISE if rotate == -1 else cv2.ROTATE_180)
if flip_x or flip_y or rotate:
cv2.imwrite("images/res_rotate", frame)
if clip_x or clip_y or clip_w or clip_h:
img = frame[clip_y: clip_y + clip_h, clip_x: clip_x + clip_w]
cv2.imwrite("images/res_clip.png", img)
else:
cv2.imwrite("images/res_clip.png.png", frame)
return True
else:
return False

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved