python 圖像平移和旋轉(zhuǎn)的實例
更新時間:2019年01月10日 15:00:23 作者:cc0587
今天小編就為大家分享一篇python 圖像平移和旋轉(zhuǎn)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
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 圖像平移和旋轉(zhuǎn)的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中3種內(nèi)建數(shù)據(jù)結(jié)構(gòu):列表、元組和字典
這篇文章主要介紹了Python中3種內(nèi)建數(shù)據(jù)結(jié)構(gòu):列表、元組和字典,需要的朋友可以參考下2014-11-11Python scipy的二維圖像卷積運算與圖像模糊處理操作示例
這篇文章主要介紹了Python scipy的二維圖像卷積運算與圖像模糊處理操作,涉及Python數(shù)學(xué)運算與圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2019-09-09Python3.9.1中使用split()的處理方法(推薦)
這篇文章主要介紹了Python3.9.1中使用split()的處理方法(推薦),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02Python使用PyGreSQL操作PostgreSQL數(shù)據(jù)庫教程
這篇文章主要介紹了Python使用PyGreSQL操作PostgreSQL數(shù)據(jù)庫,需要的朋友可以參考下2014-07-07python實現(xiàn)爬蟲統(tǒng)計學(xué)校BBS男女比例(一)
這篇文章主要介紹了python實現(xiàn)爬蟲統(tǒng)計學(xué)校BBS男女比例,,需要的朋友可以參考下2015-12-12Python使用Phantomjs截屏網(wǎng)頁的方法
今天小編就為大家分享一篇Python使用Phantomjs截屏網(wǎng)頁的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05動態(tài)設(shè)置django的model field的默認(rèn)值操作步驟
這篇文章主要介紹了動態(tài)設(shè)置django的model field的默認(rèn)值操作步驟,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03