运行我自己的旋转算法时获得错误的图片输出 [英] Getting a incorrect picture output when running my self-made rotation algorithm

查看:85
本文介绍了运行我自己的旋转算法时获得错误的图片输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了更好地了解图像处理的工作原理,我决定创建自己的图像旋转算法,而不是使用cv2.rotate().但是,我遇到了奇怪的图片裁剪和像素错位问题.

In order to better understand how image manipulation works, I've decided to create my own image rotation algorithm rather than using cv2.rotate() However, I'm encountering a weird picture cropping and pixel misplacement issue.

我认为这可能与我的填充有关,但可能还有其他错误

I think it may have something to do with my padding, but there may be other errors

import cv2

import math

import numpy as np
# Load & Show original image
img = cv2.imread('Lena.png', 0)
cv2.imshow('Original', img)

# Variable declarations
h = img.shape[0]  # Also known as rows

w = img.shape[1]  # Also known as columns

cX = h / 2 #Image Center X

cY = w / 2 #Image Center Y

theta = math.radians(100) #Change to adjust rotation angle

imgArray = np.array((img))

imgArray = np.pad(imgArray,pad_width=((100,100),(100,100)),mode='constant',constant_values=0) 
  #Add padding in an attempt to prevent image cropping

# loop pixel by pixel in image
for x in range(h + 1):

 for y in range(w + 1):

  try:
   TX = int((x-cX)*math.cos(theta)+(y-cY)*math.sin(theta)+cX) #Rotation formula 

   TY = int(-(x-cX)*math.sin(theta)+(y-cY)*math.cos(theta)+cY) #Rotation formula

   imgArray[x,y] = img[TX,TY]

  except IndexError as error:

   print(error)

cv2.imshow('Rotated', imgArray)

cv2.waitKey(0)

我认为错放的图像位置可能与缺少适当的原点有关,但是我似乎无法找到解决该问题的有效方法.

I think the misplaced image position may have something to do with lack of proper origin point, however I cannot seem to find a functioning solution to that problem.

推荐答案

尽管我没有深入研究域的数学部分,但是基于给定的信息,我认为矩阵旋转公式应该像这样工作:

Though I didn't dive in the math part of the domain, but based on the given information I think the matrix rotating formula should work like this:

更新:

正如我所承诺的,我将深入研究该领域,然后找到您可以看到的解决方案,如下所示.我还在循环中交换了源索引和目标索引的主要技巧,因此舍入永远不会有任何问题:

As I promised I dived a bit into the domain and got to the solution you can see as follows. The main trick that I've swapped the source and destination indices in the looping too, so the rounding doesn't mean any problem ever:

import cv2
import math
import numpy as np


# Load & Show original image
img = cv2.imread('/home/george/Downloads/lena.png', 0)
cv2.imshow('Original', img)


# Variable declarations
h = img.shape[0]  # Also known as rows
w = img.shape[1]  # Also known as columns

p = 120
h += 2 * p 
w += 2 * p

cX = h / 2 #Image Center X
cY = h / 2 #Image Center Y

theta = math.radians(45) #Change to adjust rotation angle

imgArray = np.zeros_like((img))  

#Add padding in an attempt to prevent image cropping
imgArray = np.pad(imgArray, pad_width=p, mode='constant', constant_values=0)   
img = np.pad(img, pad_width=p, mode='constant', constant_values=0)   

# loop pixel by pixel in image
for TX in range(h + 1):
    for TY in range(w + 1):
        try:
            x = int( +(TX - cX) * math.cos(theta) + (TY - cY) * math.sin(theta) + cX) #Rotation formula 
            y = int( -(TX - cX) * math.sin(theta) + (TY - cY) * math.cos(theta) + cY) #Rotation formula

            imgArray[TX, TY] = img[x, y]

        except IndexError as error:
           pass
#           print(error)

cv2.imshow('Rotated', imgArray)
cv2.waitKey(0)
exit()

注意:,如果您想深入了解该域,也请参见usr2564301注释.

Note: See usr2564301 comment too, if you want to dive deeper in the domain.

这篇关于运行我自己的旋转算法时获得错误的图片输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆