溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用python怎么實(shí)現(xiàn)一個圖像平移和旋轉(zhuǎn)操作

發(fā)布時間:2021-02-25 16:08:22 來源:億速云 閱讀:579 作者:戴恩恩 欄目:開發(fā)技術(shù)

這篇文章主要介紹了利用python怎么實(shí)現(xiàn)一個圖像平移和旋轉(zhuǎn)操作,億速云小編覺得不錯,現(xiàn)在分享給大家,也給大家做個參考,一起跟隨億速云小編來看看吧!

python是什么意思

Python是一種跨平臺的、具有解釋性、編譯性、互動性和面向?qū)ο蟮哪_本語言,其最初的設(shè)計(jì)是用于編寫自動化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨(dú)立的項(xiàng)目和大型項(xiàng)目。

如下所示:

import cv2
import math
import numpy as np
def move(img):
 height, width, channels = img.shape
 emptyImage2 = img.copy()
 x=20
 y=20
 for i in range(height):
 for j in range(width):
 if i>=x and j>=y:
  emptyImage2[i,j]=img[i-x][j-y]
 else:
  emptyImage2[i,j]=(0,0,0)
 
 
 return emptyImage2
 
 
img = cv2.imread("e:\\lena.bmp")
 
cv2.namedWindow("Image")
SaltImage=move(img)
cv2.imshow("Image",img)
cv2.imshow("ss",SaltImage)
cv2.waitKey(0)

旋轉(zhuǎn):

import cv2
import math
import numpy as np
def XRotate(image, angle):
 h, w, channels = image.shape
 anglePi = angle * math.pi / 180.0
 cosA = math.cos(anglePi)
 sinA = math.sin(anglePi)
 X1 = math.ceil(abs(0.5 * h * cosA + 0.5 * w * sinA))
 X2 = math.ceil(abs(0.5 * h * cosA - 0.5 * w * sinA))
 Y1 = math.ceil(abs(-0.5 * h * sinA + 0.5 * w * cosA))
 Y2 = math.ceil(abs(-0.5 * h * sinA - 0.5 * w * cosA))
 hh = int(2 * max(Y1, Y2))
 ww = int(2 * max(X1, X2))
 emptyImage2 = np.zeros((hh, ww, channels), np.uint8)
 for i in range(hh):
 for j in range(ww):
  x = cosA * i + sinA * j - 0.5 * ww * cosA - 0.5 * hh * sinA + 0.5 * w
  y = cosA * j- sinA * i+ 0.5 * ww * sinA - 0.5 * hh * cosA + 0.5 * h
  x = int(x)
  y = int(y)
  if x > -1 and x < h and y > -1 and y < w :
 
  emptyImage2[i, j] = image[x, y]
 
 return emptyImage2
 
 
image = cv2.imread("e:\\lena.bmp")
iXRotate12 = XRotate(image, 30)
cv2.imshow('image', image)
cv2.imshow('iXRotate12', iXRotate12)
cv2.waitKey(0)

以上就是億速云小編為大家收集整理的利用python怎么實(shí)現(xiàn)一個圖像平移和旋轉(zhuǎn)操作,如何覺得億速云網(wǎng)站的內(nèi)容還不錯,歡迎將億速云網(wǎng)站推薦給身邊好友。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI