【发布时间】:2020-03-27 23:49:08
【问题描述】:
我有以下问题,我有两张图像,一张是历史图像,一张是现在的卫星图像,由于历史图像覆盖的区域较小,我想裁剪卫星图像。在这里可以看到我为此编写的代码:
import numpy as np
import cv2
import os
import imutils
import math
entries = os.listdir('../')
refImage = 0
histImages = []
def loadImage(index):
referenceImage = cv2.imread("../" + 'ref_' + str(index) + '.png')
top = int(0.5 * referenceImage.shape[0]) # shape[0] = rows
bottom = top
left = int(0.5 * referenceImage.shape[1]) # shape[1] = cols
right = left
referenceImage = cv2.copyMakeBorder(referenceImage, top, bottom, left, right, cv2.BORDER_CONSTANT, None, (0,0,0))
counter = 0
for entry in entries:
if entry.startswith("image_"+str(index)):
refImage = referenceImage.copy()
histImage = cv2.imread("../" + entry)
#histImages.append(img)
points = np.loadtxt("H2OPM/"+"CP_"+ entry[6:9] + ".txt", delimiter=",")
vector_image1 = [points[0][0] - points[1][0], points[0][1] - points[1][1]] #hist
vector_image2 = [points[0][2] - points[1][2], points[0][3] - points[1][3]] #ref
angle = angle_between(vector_image1, vector_image2)
hhist, whist, chist = histImage.shape
rotatedImage = imutils.rotate(refImage, angle)
x = int(points[0][2] - points[0][0])
y = int(points[1][2] - points[1][0])
crop_img = rotatedImage[x+left:x+left+hhist, y+top:y+top+whist]
print("NewImageWidth:", (y+top+whist)-(y+top),(x+left+hhist)-(x+left))
print(entry)
print(x,y)
counter += 1
#histImage = cv2.line(histImage, (points[0][0], ), end_point, color, thickness)
cv2.imwrite("../matchedImages/"+'image_' + str(index) + "_" + str(counter) + '.png' ,histImage)
#rotatedImage = cv2.line(rotatedImage, (), (), (0, 255, 0), 9)
cv2.imwrite("../matchedImages/"+'ref_' + str(index) + "_" + str(counter) + '.png' ,crop_img)
首先,我加载原始卫星图像并对其进行填充,这样我就不会因旋转而丢失信息,其次,我加载其中一个匹配的历史图像以及这两个图像的匹配关键点(即列表x_hist、y_hist、x_present_day、y_present_day)。第三,我计算两个图像之间的旋转角度(可行),第四,我裁剪图像(第五,我保存图像)。
问题:如上所述,旋转工作正常,但我的程序最终裁剪了图像的错误部分。
我认为,由于旋转,边界(即左、右、上、下)不再正确,我认为这是我的问题所在,但我不确定如何解决此问题。
可能有帮助的信息:
- 图像的缩放方式相同(因此一个像素 = 大约 1m)
- 每张图片我至少有 6 个关键点
【问题讨论】:
-
或许可以张贴图片,让我们看看你有什么。
标签: python-3.x opencv image-processing