python - 如何在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()
解决方案
推荐阅读
- javascript - 如何使画布文本可点击/可悬停?
- function - zsh - 嵌套函数中的变量扩展失败
- sql - 具有等效限制的 BigQuery Select
- sql - 将视图从 SQL Server(本地)复制到 Azure SQL Server
- reactjs - 无法获取未定义或空引用的属性“绑定”
- php - 动态渲染视图 symfony5
- python - 如何使用 Anaconda / pip 组织我的 Python 库
- c - 将字符串从 const char *const 数组复制到字符串(在 C 中)
- javascript - 仅当我更新数据两次时,反应材料表才用新数据重新渲染表
- rust - const fn 与内联属性