gpt4 book ai didi

python-3.x - 我们如何在python中以圆周运动旋转图像

转载 作者:行者123 更新时间:2023-12-02 17:36:55 25 4
gpt4 key购买 nike

我们如何以圆周运动旋转图像,例如在python中向任意方向自由旋转。可能吗?如果是,那我该怎么办?谢谢!

我尝试过,我使用opencv库在单击鼠标时将图像旋转1度。
但这还没有解决。它旋转缓慢,每旋转1度需要点击一下。

这是该代码

 import cv2 as cv
#import numpy as np

DEF_ANGLE = 0

def click_to_rotate(event,x,y,flags,param):
global DEF_ANGLE,DEF_ANGLE1
if event == cv.EVENT_FLAG_LBUTTON:
DEF_ANGLE += 1


elif event == cv.EVENT_FLAG_RBUTTON:
DEF_ANGLE -= 1


cv.namedWindow('window')
img = cv.imread('4.2.04.tiff') #put any image here with path
cv.setMouseCallback('window',click_to_rotate)



while(True):
num_rows, num_cols = img.shape[:2]
rotation_matrix =
cv.getRotationMatrix2D((num_cols/2,num_rows/2),DEF_ANGLE,1)
img_rotation = cv.warpAffine(img, rotation_matrix, (num_cols, num_rows))
cv.imshow('window',img_rotation)
k = cv.waitKey(1) & 0xFF
if k == 27:
break

cv.destroyAllWindows()

最佳答案

我有一个您期望的工作演示:

import cv2 as cv

DEF_ANGLE = 0
pressed_left = False
pressed_right = False

def click_to_rotate(event, x, y, flags, param):
global DEF_ANGLE, DEF_ANGLE1, pressed_left, pressed_right

if event == cv.EVENT_LBUTTONDOWN:
pressed_left = True
DEF_ANGLE += 1

elif event == cv.EVENT_MOUSEMOVE:
#print('x,y',x,y)
if(pressed_left):
DEF_ANGLE += 1

if event == cv.EVENT_RBUTTONDOWN:
pressed_right = True
DEF_ANGLE -= 1

elif event == cv.EVENT_MOUSEMOVE:
#print('x,y',x,y)
if(pressed_right):
DEF_ANGLE -= 1

elif event == cv.EVENT_LBUTTONUP:
pressed_left = False

elif event == cv.EVENT_RBUTTONUP:
pressed_right = False

cv.namedWindow('window')
path = 'C:/Users/selwyn77/Desktop/'
img = cv.imread(path + 'ch.jpg')
img = cv2.resize(img, (0, 0), fx = 0.1, fy = 0.1)
cv.setMouseCallback('window', click_to_rotate)

while(True):
num_rows, num_cols = img.shape[:2]
rotation_matrix = cv.getRotationMatrix2D((num_cols/2, num_rows/2), DEF_ANGLE, 1)
img_rotation = cv.warpAffine(img, rotation_matrix, (num_cols, num_rows))
cv.imshow('window', img_rotation)
k = cv.waitKey(1) & 0xFF
if k == 27:
break

cv.destroyAllWindows()

我分别使用两个全局变量进行左右旋转。

关于python-3.x - 我们如何在python中以圆周运动旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50285492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com