Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-旋转图像而不使用内置的旋转方法_Python_Opencv_Image Processing - Fatal编程技术网

Python-旋转图像而不使用内置的旋转方法

Python-旋转图像而不使用内置的旋转方法,python,opencv,image-processing,Python,Opencv,Image Processing,如何在OpenCV库的帮助下,通过更改图像的高度和宽度值(不使用OpenCV中的内置旋转方法),在Python中旋转图像。它必须使用两个嵌套循环来实现 img=cv2.imread('Images/Screenshot.png',cv2.IMREAD_GRAYSCALE) height, width = img.shape # for i in range(0,height): # for j in range(0,width): # img[i][j]= # sh

如何在OpenCV库的帮助下,通过更改图像的高度和宽度值(不使用OpenCV中的内置旋转方法),在Python中旋转图像。它必须使用两个嵌套循环来实现

img=cv2.imread('Images/Screenshot.png',cv2.IMREAD_GRAYSCALE)

height, width = img.shape

# for i in range(0,height):
#     for j in range(0,width):
#         img[i][j]=

# show rotated image
cv2.imshow("image",img)
谢谢你的关注

交换索引: 您可以使用
np.zeros()
创建空图像,然后将当前图像中的像素读取到空图像中。可以更改读取像素的顺序以执行一些基本旋转。下面的例子应该会有所帮助

测试图像:

向左旋转90度:

向右旋转90度:

旋转180度:
它必须是OpenCv吗?因为如果没有,您可以使用PIL轻松完成:

from PIL import Image

def rotate_img(img_path, rt_degr):
    img = Image.open(img_path)
    return img.rotate(rt_degr, expand=1)

img_rt_90 = rotate_img('Images/Screenshot.png', 90)
img_rt_90.save('img_rt_90.png')

如果您需要在OpenCV中进行简单旋转,它内置:

img=cv2.imread('Images/Screenshot.png',cv2.IMREAD_GRAYSCALE)

imgrot = cv2.rotate(img,cv2.ROTATE_90_CLOCKWISE)

其他的可能是
cv2.逆时针旋转\u 90\u
cv2.旋转\u 180
我使用PIL软件包,这样做非常简单

从PIL导入图像
path='/Users/diegodsp/sample.jpg'
img=Image.open(路径)
img=img.旋转(90)#90,-90,180。。。
img.save(path)#覆盖旧文件

您能为您目前尝试的内容发布一些代码吗?@G.Anderson我已经在post中添加了一些代码,当您运行这些代码时,另一端会出现什么,这与您期望的相比如何?不要试图卑鄙,真正好奇你的代码在做什么。你的意思是将
img[i][j]
分配给循环中的
img[j][i]
吗?拿一张纸(一个较大的正方形),画一个小网格,比如5x7,标记轴并填充一些“像素”。然后试着画同样的东西,按照你想要的方式旋转。观察您使用什么算法生成结果。(或者更好,想象一下你必须通过电话指示一位朋友生成旋转图像。你会告诉他们一步一步地做什么?)以任何角度旋转:勾住Em喇叭:-)这不是旋转,而是翻转(R)标记不应该改变位置。
empty\img=empty\img[0:h,0:w]
是完全多余和荒谬的。@ChristophRackwitz你是对的。我想,当时,我的意图是在循环之前裁剪到原始图像的高度和宽度,因为一个像素的误差。然而,正如您所提到的,在第二个嵌套循环中发生这种情况是非常愚蠢的。考虑到质询者的任意约束,整个问答都是毫无用处的。这就是答案。不知道为什么它不是最好的解决方案。这将大大降低图像质量。
h,w,c = img.shape

empty_img = np.zeros([h,w,c], dtype=np.uint8)

for i in range(h):
    for j in range(w):
        empty_img[i,j] = img[h-j-1,w-i-1]
        empty_img = empty_img[0:h,0:w]

cv2.imwrite("tester2.png", empty_img)
h,w,c = img.shape

empty_img = np.zeros([h,w,c], dtype=np.uint8)

for i in range(h):
    for j in range(w):
        empty_img[i,j] = img[h-i-1,w-j-1]
        empty_img = empty_img[0:h,0:w]

cv2.imwrite("tester3.png", empty_img)
from PIL import Image

def rotate_img(img_path, rt_degr):
    img = Image.open(img_path)
    return img.rotate(rt_degr, expand=1)

img_rt_90 = rotate_img('Images/Screenshot.png', 90)
img_rt_90.save('img_rt_90.png')
img=cv2.imread('Images/Screenshot.png',cv2.IMREAD_GRAYSCALE)

imgrot = cv2.rotate(img,cv2.ROTATE_90_CLOCKWISE)