首页 > 解决方案 > 如何在pyqt5窗口中插入带有cv2的图像裁剪模块

问题描述

我用 OpenCV 创建了一个图像裁剪应用程序。它打开一个窗口,裁剪后,它会在另一个窗口中显示裁剪后的图片。如何在 pyqt5 程序中而不是在 OpenCV 本身中执行这些操作?

import cv2
import os, sys


#Foto
yol = "./photos/"
liste_yol = os.listdir( yol )
l = [yol+nesne for nesne in liste_yol if ".jpg" or ".png" in nesne]
im_nu = 0
t = 0
kesme = False
x_basl, y_basl, x_son, y_son = 0, 0, 0, 0
 
resim = cv2.imread(l[im_nu])
oriresim = resim.copy()
 

def crop_node(olay, x, y, flags, param):
    global x_basl, y_basl, x_son, y_son, kesme, t
 
    if olay == cv2.EVENT_LBUTTONDOWN:
        x_basl, y_basl, x_son, y_son = x, y, x, y
        kesme = True
 
    elif olay == cv2.EVENT_MOUSEMOVE:
        if kesme == True:
            x_son, y_son = x, y
 
    elif olay == cv2.EVENT_LBUTTONUP:
        x_son, y_son = x, y
        kesme = False 
 
        refNokta = [(x_basl, y_basl), (x_son, y_son)]
 
        if len(refNokta) == 2: 
            t += 1
            alan = oriresim[refNokta[0][1]:refNokta[1][1], refNokta[0][0]:refNokta[1][0]]
            cv2.imshow("Kesilen Resim", alan)
            cv2.imwrite("cropped-images/new-image"+str(t)+".jpg", alan)

 
cv2.namedWindow("resim", cv2.WINDOW_NORMAL)
cv2.setMouseCallback("resim", crop_node)
 
while True:
    key = cv2.waitKey(1) & 0xFF
    i = resim.copy()
 
    if not kesme:
        cv2.imshow("resim", resim)
 
    elif kesme:
        cv2.rectangle(i, (x_basl, y_basl), (x_son, y_son), (255, 0, 0), 2)
        cv2.imshow("resim", i)    
 
    cv2.waitKey(1)
    if key == ord(" "):
        im_nu += 1
        resim = cv2.imread(l[im_nu])
        oriresim = resim.copy()
        cv2.imshow("resim", resim)
        cv2.setMouseCallback("resim", crop_node)
 
cv2.destroyAllWindows()

标签: pythonopencvpyqtpyqt5crop

解决方案


推荐阅读