[英]Correct orientation of an image based on 4 Qpoints
我正在嘗試根據從用戶那里獲取的四個 Qpoints 來校正圖像的方向(旋轉)。 我找到了一個類似的代碼,我正在處理它,並將其作為解決方案發布在此鏈接中。
編碼:
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
current_dir = os.path.dirname(os.path.realpath(__file__))
point_filename = os.path.join(current_dir, "Lastout.png")
class GraphicsView(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super().__init__(QtWidgets.QGraphicsScene(), parent)
self.pixmap_item = self.scene().addPixmap(QtGui.QPixmap())
self.pixmap_item.setShapeMode(QtWidgets.QGraphicsPixmapItem.BoundingRectShape)
self.setAlignment(QtCore.Qt.AlignCenter)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
def set_image(self, pixmap):
self.pixmap_item.setPixmap(pixmap)
#The pixmap is scaled to a rectangle as small as possible outside size, preserving the aspect ratio.
self.fitInView(self.pixmap_item, QtCore.Qt.KeepAspectRatio)
class CropView(GraphicsView):
Changed_view = QtCore.pyqtSignal(QtGui.QPixmap)
def __init__(self, parent=None):
super().__init__(parent)
self.point_items = []
def mousePressEvent(self, event):
if not self.pixmap_item.pixmap().isNull():
sp = self.mapToScene(event.pos())
#print("Event position = " +str(sp))
lp = self.pixmap_item.mapFromScene(sp)
#print("Event position FromScene = " +str(lp))
if self.pixmap_item.contains(lp):
size = QtCore.QSize(30, 30)
height = (
self.mapToScene(QtCore.QRect(QtCore.QPoint(), size))
.boundingRect()
.size()
.height()
)
pixmap = QtGui.QPixmap(point_filename)
point_item = QtWidgets.QGraphicsPixmapItem(pixmap, self.pixmap_item)
point_item.setOffset(
-QtCore.QRect(QtCore.QPoint(), pixmap.size()).center()
)
point_item.setPos(lp)
scale = height / point_item.boundingRect().size().height()
# print ("Scale: "+str(scale))
point_item.setScale(scale)
self.point_items.append(point_item)
if len(self.point_items) == 4:
points = []
for it in self.point_items:
points.append(it.pos().toPoint())
print ("points: " + str (it.pos().toPoint()))
print (" x " + str(it.x()) +" y "+ str( it.y()) )
self.crop(points)
elif len(self.point_items) == 5:
for it in self.point_items[:-1]:
self.scene().removeItem(it)
self.point_items = [self.point_items[-1]]
else:
print("outside")
super().mousePressEvent(event)
def crop(self, points):
# https://stackoverflow.com/a/55714969/6622587
polygon = QtGui.QPolygonF(points)
path = QtGui.QPainterPath()
path.addPolygon(polygon)
source = self.pixmap_item.pixmap()
r = path.boundingRect().toRect().intersected(source.rect())
print (str(r))
#t = QtGui.QTransform() #added
pixmap = QtGui.QPixmap(source.size())
#t.translate (pixmap._center.x() -pixmap.width() / 2, pixmap._center.y() -pixmap.height() / 2)
#t.translate(pixmap.width() / 2, pixmap.height() / 2)
# t.rotate(45.0)
#t.translate(-pixmap.width() / 2, -pixmap.height() / 2)
pixmap.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(pixmap)
painter.setClipPath(path)
painter.drawPixmap(QtCore.QPoint(), source, source.rect())
painter.end()
result = pixmap.copy(r)
self.Changed_view.emit(result)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(1200, 700)
self.left_view = CropView()
self.rigth_view = GraphicsView()
self.left_view.Changed_view.connect(self.rigth_view.set_image)
button = QtWidgets.QPushButton(self.tr("Select Image"))
button.setStyleSheet("background-color: rgb(0, 100, 100);")
button.setFixedSize(230, 60)
font = QtGui.QFont()
font.setFamily("Microsoft YaHei UI")
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
button.setFont(font)
button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
button.clicked.connect(self.load_image)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QGridLayout(central_widget)
lay.addWidget(self.left_view, 0, 0)
lay.addWidget(self.rigth_view, 0, 1)
lay.addWidget(button, 1, 0, 1, 2, alignment=QtCore.Qt.AlignHCenter)
@QtCore.pyqtSlot()
def load_image(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
None, "Select Image", "", "Image Files (*.png *.jpg *jpeg *.bmp)"
)
if fileName:
pixmap = QtGui.QPixmap(fileName)
self.left_view.set_image(pixmap)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
電流輸出:
預期輸出:裁剪后用戶輸入圖像的校正方向
誰能指導我如何做到這一點?
謝謝你。
選擇四個任意點不會給你一個矩形,而是一個四邊形,它可能不是所有的角都是 90° 角。 你將如何決定哪條線作為旋轉的參考?
此外,簡單的旋轉不會補償透視失真。
與其簡單地旋轉矩形,不如使用變換。
我冒昧地更改了創建點背后的邏輯(使其更簡單):這樣它們就不是像素圖項的子項,而是場景的子項; 它們也可以移動,立即顯示結果圖像。
在下圖中,透視失真得到了更好的解釋:我正在使用具有可見透視的源,並且通過轉換,我能夠將四邊形變成矩形。
在這個例子中,我假設點的順序總是左上、右上、右下、左下。
如果用戶遵循另一個順序,結果顯然是錯誤的,因此您可能需要找到一種方法來更好地檢查點的位置。
class CropView(GraphicsView):
Changed_view = QtCore.pyqtSignal(QtGui.QPixmap)
def __init__(self, parent=None):
super().__init__(parent)
self.point_items = []
self.crosshair = QtGui.QPixmap(point_filename)
def mousePressEvent(self, event):
if not self.pixmap_item.pixmap().isNull():
if not self.itemAt(event.pos()) in self.point_items:
scenePos = self.mapToScene(event.pos())
if len(self.point_items) == 4:
while self.point_items:
self.scene().removeItem(self.point_items.pop())
if self.pixmap_item.sceneBoundingRect().contains(scenePos):
point_item = self.scene().addPixmap(self.crosshair)
point_item.setPos(scenePos)
point_item.setFlag(point_item.ItemIgnoresTransformations)
point_item.setFlag(point_item.ItemIsMovable)
point_item.setOffset(-self.crosshair.rect().center())
self.point_items.append(point_item)
if len(self.point_items) == 4:
self.crop()
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
if len(self.point_items) == 4 and self.itemAt(event.pos()) in self.point_items:
# update the rectangle if the points have been moved
self.crop()
def crop(self):
points = []
for point_item in self.point_items:
points.append(self.pixmap_item.mapFromScene(point_item.pos()))
# get the width and height based on the 4 points:
# I'm assuming that the points are always in this order:
# top-left, top-right, bottom-right, bottom-left
# so we get the width from the longest two top and bottom lines
# and the height from the longest left and right lines
width = max(QtCore.QLineF(points[0], points[1]).length(), QtCore.QLineF(points[2], points[3]).length())
height = max(QtCore.QLineF(points[1], points[2]).length(), QtCore.QLineF(points[3], points[0]).length())
sourcePolygon = QtGui.QPolygonF(points)
source = self.pixmap_item.pixmap()
pixmap = QtGui.QPixmap(width, height)
transform = QtGui.QTransform()
rect = pixmap.rect()
# this is the target used for the transformation
targetPolygon = QtGui.QPolygonF([rect.topLeft(), rect.topRight(), rect.bottomRight(), rect.bottomLeft()])
# quadToQuad is a static that sets the matrix of a transform based on two
# four-sided polygons
QtGui.QTransform.quadToQuad(sourcePolygon, targetPolygon, transform)
painter = QtGui.QPainter(pixmap)
# smooth pixmap transform is required for better results
painter.setRenderHints(painter.SmoothPixmapTransform)
painter.setTransform(transform)
painter.drawPixmap(QtCore.QPoint(), source)
painter.end()
self.Changed_view.emit(pixmap)
請注意,我還在set_image
函數中添加了一行:
self.setSceneRect(self.scene().sceneRect())
這確保了視圖的sceneRect 總是適應實際的場景矩形。
此外,您應該記住在加載新圖像后立即刪除所有點項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.