【问题标题】:Overlayed contours not aligning after image+contours rotation w/warpAffine from OpenCV来自OpenCV的带有warpAffine的图像+轮廓旋转后重叠轮廓未对齐
【发布时间】:2016-10-14 02:21:25
【问题描述】:

为了 100% 可重现,整个代码(和使用的图像)如下。

以下是代码中的步骤。

  1. 我将轮廓 ([[386, 330], [398, 320], [382, 300], [370, 310], [386, 330]]) 叠加在图像上。效果很好。

  2. 使用warpAffine将图像旋转θ(= 90)

  3. 在大小和旋转功能完全相同的虚拟图像中旋转轮廓中的每个点。

  4. 找到旋转点的坐标。

  5. 在旋转后的图像上叠加旋转点。

我预计他们会排队,但他们没有

可能是什么原因?

#! /usr/bin/env python                                                                                             
import numpy as np
import cv2
from shapely.geometry.polygon import LinearRing

theta = 90
image_path = 'image.tiff'
orig_image = cv2.imread(image_path)

wnd_str = 'unrotated'
cv2.namedWindow(wnd_str,cv2.WINDOW_NORMAL)
cv2.moveWindow(wnd_str, 0, 0)

rows,cols, channels = orig_image.shape
print 'Loaded image shape {}'.format(orig_image.shape)

blank_image = np.zeros((rows, cols, 3), np.uint8)
print 'Blank image shape {}'.format(blank_image.shape)

M = cv2.getRotationMatrix2D((rows/2, cols/2), theta, 1.0)
rot_image = cv2.warpAffine(orig_image, M, (rows, cols), flags=cv2.INTER_CUBIC+cv2.BORDER_CONSTANT)
print 'Rotated image shape {}'.format(rot_image.shape)

white = (255, 255, 255)

#contours overlayed on unrotated image                                                                             
pts = [[386, 330], [398, 320], [382, 300], [370, 310], [386, 330]]
poly_pts = []
for p in pts: poly_pts.append(p)
poly_pts = np.array(poly_pts[0:-1], np.int32)
poly_pts = poly_pts.reshape((-1,1,2))

cv2.polylines(orig_image, [poly_pts], True, white, 1)
cv2.imshow(wnd_str, orig_image)
cv2.waitKey(0)

#generate contours for the rotated image                                                                           
rot_poly_pts = []
for p in pts:
    x, y = p

    blank_image = np.zeros((rows, cols, 3), np.uint8)

    blank_image[x,y] = (255, 255, 255)
    blank_image_affine = cv2.warpAffine(blank_image, M, (rows, cols), flags=cv2.INTER_CUBIC+cv2.BORDER_CONSTANT)

    rotated_y, rotated_x, rotated_z = np.unravel_index(blank_image_affine.argmax(), blank_image_affine.shape)

    rot_poly_pts.append([rotated_x, rotated_y])

rot_poly_pts = np.array(rot_poly_pts[0:-1], np.int32)
rot_poly_pts = rot_poly_pts.reshape((-1,1,2))
cv2.polylines(rot_image, [rot_poly_pts], True, white, 1)

wnd_str = 'rotated {}'.format(theta)
cv2.namedWindow(wnd_str,cv2.WINDOW_NORMAL)
cv2.moveWindow(wnd_str, 0, 0)
cv2.imshow(wnd_str, rot_image)

cv2.waitKey(0)
cv2.destroyAllWindows()


  [1]: https://i.stack.imgur.com/pyxmq.png

这是具有对齐轮廓的原始图像

这是旋转后的图像和叠加。

【问题讨论】:

    标签: opencv image-processing image-rotation affinetransform satellite-image


    【解决方案1】:

    wrapAffine 生成的图像两边都有裁剪像素。此过程的一种有效替代方法是转置然后翻转。

    dst = cv2.flip(img.transpose(1,0,2),0)
    
    dst  = destination image;
    img  = source image;
    

    img.transpose(col,row,channel) = 1,0,2 对应这个顺序,因为我们只想转置 rows 和 col 并保持通道不变。

    cv2.flip = 用于抵消转置操作期间引起的镜像效果。根据您的源图像(横向或纵向),将标志更改为 01

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2011-12-13
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多