【问题标题】:Crop satellite image image based on a historical image with OpenCV in Python在 Python 中使用 OpenCV 基于历史图像裁剪卫星图像图像
【发布时间】: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


【解决方案1】:

我还没有查看您的代码,但这是因为您混淆了 x 和 y 吗?检查 OpenCV 文档以确保您导入的变量顺序正确。 在我有限的时间和使用 opencv 的经验中,这很奇怪,因为有时它会要求例如 BGR 而不是 RGB 值。 (在我的程序中,不是你的)

另外,您似乎有一堆列表,请确保列表[x][y]没有与列表[y][x]混淆

【讨论】:

    【解决方案2】:

    所以我在计算中发现了错误。剪切区域的边界框被错误地转换为现在的图像。
    所以这个:

    x = int(points[0][2] - points[0][0])
    y = int(points[1][2] - points[1][0])
    

    被换成了这个:

    v = [pointBefore[0],pointBefore[1],1]
    # Perform the actual rotation and return the image
    calculated = np.dot(m,v)
    newPoint = (int(calculated[0]- points[0][0]),int(calculated[1]- points[0][1]))
    

    其中 m(=M) 来自转换:

    def rotate_bound(image, angle):
        # grab the dimensions of the image and then determine the
        # center
        (h, w) = image.shape[:2]
        (cX, cY) = (w // 2, h // 2)
    
        # grab the rotation matrix (applying the negative of the
        # angle to rotate clockwise), then grab the sine and cosine
        # (i.e., the rotation components of the matrix)
        M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
        cos = np.abs(M[0, 0])
        sin = np.abs(M[0, 1])
    
        # compute the new bounding dimensions of the image
        nW = int((h * sin) + (w * cos))
        nH = int((h * cos) + (w * sin))
    
        # adjust the rotation matrix to take into account translation
        M[0, 2] += (nW / 2) - cX
        M[1, 2] += (nH / 2) - cY
    
        # perform the actual rotation and return the image
        return cv2.warpAffine(image, M, (nW, nH)), M
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 2020-10-18
      • 1970-01-01
      • 2013-03-13
      相关资源
      最近更新 更多