【问题标题】:Find [x,y] rotated coordinates locations in image [OpenCV / Python]在图像中查找 [x,y] 旋转坐标位置 [OpenCV / Python]
【发布时间】:2017-02-20 21:47:24
【问题描述】:

我想按多个角度依次旋转图像。我使用cv2.getRotationMatrix2Dcv2.warpAffine 来做到这一点。有一对像素坐标 [x,y],其中 x=cols, y=rows(在本例中)我想在旋转后的图像中找到它们的新坐标。

我使用http://www.pyimagesearch.com/2017/01/02/rotate-images-correctly-with-opencv-and-python/ 提供的以下稍作更改的代码以及仿射变换 的解释来尝试映射旋转图像中的点:http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html

问题是我的映射或旋转错误,因为转换后的计算坐标错误。 (我尝试手动计算角点以进行简单验证)

代码

def rotate_bound(image, angle):
    # grab the dimensions of the image and then determine the
    # center
    (h, w) = image.shape[:2]
    (cX, cY) = ((w-1) // 2.0, (h-1)// 2.0)


# 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))
print nW, nH

# adjust the rotation matrix to take into account translation
M[0, 2] += ((nW-1) / 2.0) - cX
M[1, 2] += ((nH-1) / 2.0) - cY

# perform the actual rotation and return the image
return M, cv2.warpAffine(image, M, (nW, nH))

#function that calculates the updated locations of the coordinates
#after rotation
def rotated_coord(points,M):
    points = np.array(points)
    ones = np.ones(shape=(len(points),1))
    points_ones = np.concatenate((points,ones), axis=1)
    transformed_pts = M.dot(points_ones.T).T
    return transformed_pts

#READ IMAGE & CALL FCT
img = cv2.imread("Lenna.png")
points = np.array([[511,  511]])
#rotate by 90 angle for example
M, rotated = rotate_bound(img, 90)
#find out the new locations
transformed_pts = rotated_coord(points,M)

如果我有例如坐标[511,511],当我期望获得[0,511] 时,我将获得[-0.5, 511.50] ([col, row])。

如果我改用w // 2,则会在图像上添加一个黑色边框,并且我的旋转更新坐标将再次关闭。

问题:如何使用 Python 找到旋转图像(按一定角度)中一对像素坐标的正确位置?

【问题讨论】:

    标签: python opencv image-processing rotation


    【解决方案1】:

    对于这种图像旋转的情况,旋转后图像大小和参考点都会发生变化,必须修改变换矩阵。新的 with 和 height 可以使用以下关系计算:

    new.width = h*\sin(\theta) + w*\cos(\theta)

    new.height = h*\cos(\theta) + w*\sin(\theta)

    由于图像大小发生变化,由于您可能会看到黑色边框,因此旋转点(图像中心)的坐标也会发生变化。然后必须在转换矩阵中考虑它。

    我在我的博客image rotation bounding box opencv中解释了一个例子

    def rotate_box(bb, cx, cy, h, w):  
        new_bb = list(bb)                                                                                                                                                 
        for i,coord in enumerate(bb):
            # opencv calculates standard transformation matrix                                                                                                            
            M = cv2.getRotationMatrix2D((cx, cy), theta, 1.0)
            # Grab  the rotation components of the matrix)                                                                                                                
            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
            # Prepare the vector to be transformed 
            v = [coord[0],coord[1],1]
            # Perform the actual rotation and return the image
            calculated = np.dot(M,v)
            new_bb[i] = (calculated[0],calculated[1]) 
            return new_bb   
    
    
     ## Calculate the new bounding box coordinates
     new_bb = {}
     for i in bb1: 
     new_bb[i] = rotate_box(bb1[i], cx, cy, heigth, width)
    

    【讨论】:

    • 请帮我理解这里的 bb1 是什么? (对于 bb1 中的 i:...)我只知道 cx、cy、height、width 和 angle。
    【解决方案2】:

    上面提到的@cristianpb的Python代码对应的C++代码,如果有人和我一样在找C++代码:

     // send the original angle i.e. don't transform it in radian
            cv::Point2f rotatePointUsingTransformationMat(const cv::Point2f& inPoint, const cv::Point2f& center, const double& rotAngle)
            {
                cv::Mat rot = cv::getRotationMatrix2D(center, rotAngle, 1.0);
                float cos = rot.at<double>(0,0);
                float sin = rot.at<double>(0,1);
                int newWidth = int( ((center.y*2)*sin) +  ((center.x*2)*cos) );
                int newHeight = int( ((center.y*2)*cos) +  ((center.x*2)*sin) );
    
                rot.at<double>(0,2) += newWidth/2.0 - center.x;
                rot.at<double>(1,2) += newHeight/2.0 - center.y;
    
                int v[3] = {static_cast<int>(inPoint.x),static_cast<int>(inPoint.y),1};
                int mat3[2][1] = {{0},{0}};
    
                for(int i=0; i<rot.rows; i++)
                {
                    for(int j=0; j<= 0; j++)
                    {
                        int sum=0;
                        for(int k=0; k<3; k++)
                        {
                            sum = sum + rot.at<double>(i,k) * v[k];
                        }
                        mat3[i][j] = sum;
                    }
                }
                return Point2f(mat3[0][0],mat3[1][0]);
            }
    

    【讨论】:

      猜你喜欢
      • 2022-11-23
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 2013-09-04
      • 2012-11-26
      相关资源
      最近更新 更多