python 图像旋转,Python-旋转图像

How can I rotate an image in Python with the help of 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 for loop.

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)

Thanks for taking your time to help me!

解决方案

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.

Test image:

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

3d069f32970e3e5a0d30f01011656d91.png

Rotate left 90 degrees:

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)

ed4a47b826be9195912790afdc6c7367.png

Rotate right 90 degrees:

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)

0f684ce68d81180f6d77398f1d7fed2f.png

Rotate 180 degrees:

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)

0f05752605bb3866b6c3352567588b9c.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值