OpenCV旋转图像 要用到这两个函数:
M = cv2.getRotationMatrix2D((cx, cy), angle, 1) # 旋转中心,角度degree,放大比例
image = cv2.warpAffine(image, M, die_image.shape)
示例程序
K = 10
resized = cv2.resize(copied, (rows // K, cols // K))
domain = connected_domain(resized, 20) # 缩小图像,以便加速图像分割
for label in domain:
if (area := len(domain[label])) < 1440000 // (K * K):
continue # 舍弃掉面积小的连通域
# 找 连通域的左上角和右下角
x1, y1, x3, y3 = find_corners(domain[label])
row = int(x1 / (1330.0 / K)) # 1330 为die 重复间距
col = int(y1 / (1330.0 / K))
index = 3 * row + col
#print(f"row={row}, col={col}")
die_bin = MX_MY_BIN_list[index][2]
if die_bin == "GOOD": # only analyze good die
# print(f"row={row}, col={col}")
# print(MX_MY_BIN_list[index], x1, y1)
die_image = img[x1 * K:x3 * K, y1 * K:y3 * K]
# plt.imshow(die_image, cmap="gray")
# plt.title("-".join(MX_MY_BIN_list[index]))
# plt.show()
row1, col1, row2, col2 = find_left_dummy_pads(die_image)
# 计算旋转角度
theta = 0
if (delta_y := col2 - col1) != 0: # 象鼻表达式的优先级很低,所有这里需要括号
theta = atan((row2 - row1) / delta_y) / pi * 180
#print(f" die 的旋转角度 theta = {theta} 度")
angle = (theta - 90) if theta > 0 else (theta + 90)
M = cv2.getRotationMatrix2D((0, 0), angle, 1) # 旋转中心,角度degree,放大比例
die_image = cv2.warpAffine(die_image, M, die_image.shape)
# plt.imshow(die_image, cmap="gray")
# plt.title("rotated die image")
# plt.show()
本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!