窗口的透視變換效果

當(dāng)我們點(diǎn)擊Win10的UWP應(yīng)用中的小部件時(shí),會發(fā)現(xiàn)小部件會朝著鼠標(biāo)點(diǎn)擊位置凹陷下去,而且不同的點(diǎn)擊位置對應(yīng)著不同的凹陷情況,看起來就好像小部件在屏幕上不只有x軸和y軸,甚至還有一個(gè)z軸。要做到這一點(diǎn),其實(shí)只要對窗口進(jìn)行透視變換即可。下面是對Qt的窗口和按鈕進(jìn)行透視變換的效果:

具體代碼
1.下面先定義一個(gè)類,它的作用是將傳入的 QPixmap 轉(zhuǎn)換為numpy 數(shù)組,然后用 opencv 的 warpPerspective 對數(shù)組進(jìn)行透視變換,最后再將 numpy 數(shù)組轉(zhuǎn)為 QPixmap 并返回;
# coding:utf-8
import cv2 as cv
import numpy
from PyQt5.QtGui import QImage, QPixmap
class PixmapPerspectiveTransform:
""" 透視變換基類 """
def __init__(self, pixmap=None):
""" 實(shí)例化透視變換對象\n
Parameter
---------
src : numpy數(shù)組 """
self.pixmap = pixmap
def setPixmap(self, pixmap: QPixmap):
""" 設(shè)置被變換的QPixmap """
self.pixmap = QPixmap
self.src=self.transQPixmapToNdarray(pixmap)
self.height, self.width = self.src.shape[:2]
# 變換前后的邊角坐標(biāo)
self.srcPoints = numpy.float32(
[[0, 0], [self.width - 1, 0], [0, self.height - 1],
[self.width - 1, self.height - 1]])
def setDstPoints(self, leftTop: list, rightTop, leftBottom, rightBottom):
""" 設(shè)置變換后的邊角坐標(biāo) """
self.dstPoints = numpy.float32(
[leftTop, rightTop, leftBottom, rightBottom])
def getPerspectiveTransform(self, imWidth, imHeight, borderMode=cv.BORDER_CONSTANT, borderValue=[255, 255, 255, 0]) -> QPixmap:
""" 透視變換圖像,返回QPixmap\n
Parameters
----------
imWidth : 變換后的圖像寬度\n
imHeight : 變換后的圖像高度\n
borderMode : 邊框插值方式\n
borderValue : 邊框顏色
"""
# 如果是jpg需要加上一個(gè)透明通道
if self.src.shape[-1] == 3:
self.src = cv.cvtColor(self.src, cv.COLOR_BGR2BGRA)
# 透視變換矩陣
perspectiveMatrix = cv.getPerspectiveTransform(
self.srcPoints, self.dstPoints)
# 執(zhí)行變換
self.dst = cv.warpPerspective(self.src, perspectiveMatrix, (
imWidth, imHeight), borderMode=borderMode, borderValue=borderValue)
# 將ndarray轉(zhuǎn)換為QPixmap
return self.transNdarrayToQPixmap(self.dst)
def transQPixmapToNdarray(self, pixmap: QPixmap):
""" 將QPixmap轉(zhuǎn)換為numpy數(shù)組 """
width, height = pixmap.width(), pixmap.height()
channels_count = 4
image = pixmap.toImage() # type:QImage
s = image.bits().asstring(height * width * channels_count)
# 得到BGRA格式數(shù)組
array = numpy.fromstring(s, numpy.uint8).reshape(
(height, width, channels_count))
return array
def transNdarrayToQPixmap(self, array):
""" 將numpy數(shù)組轉(zhuǎn)換為QPixmap """
height, width, bytesPerComponent = array.shape
bytesPerLine = 4 * width
# 默認(rèn)數(shù)組維度為 m*n*4
dst = cv.cvtColor(array, cv.COLOR_BGRA2RGBA)
pix = QPixmap.fromImage(
QImage(dst.data, width, height, bytesPerLine, QImage.Format_RGBA8888))
return pix
標(biāo)題名稱:詳解如何在pyqt中通過OpenCV實(shí)現(xiàn)對窗口的透視變換-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://jinyejixie.com/article32/dcjopc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、外貿(mào)建站、網(wǎng)站營銷、企業(yè)建站、網(wǎng)站導(dǎo)航、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容