Python-旋转图像而不使用内置方法进行旋转 [英] Python - Rotate Image without using the built-in methods for rotation

查看:50
本文介绍了Python-旋转图像而不使用内置方法进行旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I rotate an image in Python with the help of the OpenCV library, and by changing the value of height and width of the image (without using the built-in methods for rotation in OpenCV). It has to implement with two nested loops.

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()创建空白图像,然后将像素从当前图像读取到空白图像.您可以更改读取像素的顺序以执行一些基本旋转.以下示例将有所帮助.

Swap the indexes:

You can create an empty image with np.zeros() and then read the pixels from your current image to the empty image. You can change the order that you read the pixels to perform some basic rotations. The following examples should help.

img = cv2.imread('texas_longhorns_log.png')

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[j-1,i-1]
        empty_img = empty_img[0:h,0:w]

cv2.imwrite("tester1.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-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)

这篇关于Python-旋转图像而不使用内置方法进行旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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