gpt4 book ai didi

python - 图像角点度计算的脚本怎么写?

转载 作者:太空宇宙 更新时间:2023-11-03 21:42:58 25 4
gpt4 key购买 nike

我有 100 多张图片,每张图片的角度都不同。我写了一个基本的 python 代码来将每个图像从任何角度一个一个地旋转到零度(我的意思是使它们变平)。我附加的 python 代码是一个简单的代码,不幸的是它不会自动找到角度,也不会使其精确为零。任何时候对于任何图像我都应该找到角度并运行代码多次以使其为零(有时我无法使其完全平坦或我们将其设为零度)。根据我附加的图像,image1 是作为输入的示例图像之一,image_2 是我希望最后作为输出的旋转图像。我想问任何可以帮助我修改当前代码或提供新的 python 代码(我更喜欢新代码)的人,我可以使我的图像从任何角度旋转到零度。如果需要,请随时向我询问更多解释。

我的 opencv-python 代码是:

import cv2
import numpy as np
img = cv2.imread('image1.tif')
num_rows, num_cols = img.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2),69.4, 1)
img_rotation = cv2.warpAffine(img, rotation_matrix, (num_cols, num_rows))
cv2.imshow('Rotation', img_rotation)
cv2.imwrite('image_2.tif',img_rotation)
cv2.waitKey()

注意:输入和输出图像已删除。

最佳答案

这绝对不是最可靠的方法,但也许可以选择:

  1. 假设边界全是黑色
  2. 识别图像的最上角 (x0,y0)/最右角 (x1,y1)
  3. 计算旋转角度为 alpha = math.atan2(x1-x0,y1-y0)

我下载了你的图(它在 imgur 上被转换为 png)并测试了程序:

#!/usr/bin/env python
import cv2
import math

import numpy as np

img = cv2.imread('test.png')
H, W = img.shape[:2]

x0,y0 = None,None
x1,y1 = None,None

#scan all rows starting with the first
for i in range(0, H):
row = img[i].sum(axis=1)
s = np.sum(row)
if s:
#if there is at least one non-black pixel, mark
#its position
x0 = np.max(np.where(row>0))
y0 = i
break

#scan all columns starting with the right-most one
for j in range(W-1,-1,-1):
col = img[:,j,:].sum(axis=1)
s = np.sum(col)
if s:
#mark the position of the first non-black pixel
x1 = j
y1 = np.min(np.where(col>0))
break

dx = x1 - x0
dy = y1 - y0

alpha = math.atan2(dx, dy) / math.pi * 180

rotation_matrix = cv2.getRotationMatrix2D((W/2, H/2), -alpha, 1)
img_rotation = cv2.warpAffine(img, rotation_matrix, (W, H))
cv2.imwrite('image_2.tif',img_rotation)

编辑:

如果“角”像素也为黑色,则先前的方法可能会不准确,从而导致计算出的角度出现偏差。稍微更准确的方法可能如下:

  1. 确定矩形的“上”边界(即定义边缘的像素坐标)
  2. 取在x轴上投影较长的边
  3. 拟合坐标以计算定义边缘的线的斜率

实现:

#!/usr/bin/env python
import cv2
import math

import numpy as np

img = cv2.imread('test.png')
H, W = img.shape[:2]

data = []
for j in range(0, W):
col = img[:,j,:].sum(axis=1)
s = np.sum(col)
if not s:
continue

for i in range(0, H):
if col[i] > 0:
data.append((j, i))
break

y_min, min_pos = None, None
for idx, (x, y) in enumerate(data):
if y_min is None or y < y_min:
y_min = y
min_pos = idx

N = len(data)
if min_pos > N - min_pos:
data = data[:min_pos]
else:
data = data[min_pos:]

data = np.asarray(data).T
coeffs = np.polyfit(data[0], data[1], 1)
alpha = math.atan(coeffs[0]) / math.pi * 180

print(alpha)

rotation_matrix = cv2.getRotationMatrix2D((W/2, H/2), alpha, 1)
img_rotation = cv2.warpAffine(img, rotation_matrix, (W, H))
cv2.imwrite('image_2.tif',img_rotation)

关于python - 图像角点度计算的脚本怎么写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44130037/

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