写一段图片旋转与线段判别的代码
时间: 2024-05-15 22:17:14 浏览: 14
以下是一段Python代码,实现了图片旋转和线段判别的功能:
```python
import cv2
import numpy as np
def rotate_image(image, angle):
"""
旋转图片
:param image: 原始图片
:param angle: 旋转角度,逆时针为正
:return: 旋转后的图片
"""
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
def is_intersect(line1, line2):
"""
判断两条线段是否相交
:param line1: 线段1,格式为((x1, y1), (x2, y2))
:param line2: 线段2,格式为((x1, y1), (x2, y2))
:return: 是否相交,True为相交,False为不相交
"""
x1, y1 = line1[0]
x2, y2 = line1[1]
x3, y3 = line2[0]
x4, y4 = line2[1]
d = (y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)
if d == 0:
return False
ua = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / d
ub = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / d
if ua >= 0 and ua <= 1 and ub >= 0 and ub <= 1:
return True
return False
```
如果您有其他问题,欢迎继续提问!