import sys
import cv2
import numpy as np
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QThread, pyqtSignal
class Worker(QThread):
update_image = pyqtSignal(np.ndarray)
def run(self):
# 模拟实时图像数据
cap = cv2.VideoCapture(0) # 从摄像头捕获图像
while True:
ret, frame = cap.read() # 读取一帧图像
if not ret:
break
# 发送图像数据到主线程以更新UI
self.update_image.emit(frame)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 设置窗口大小
self.setGeometry(100, 100, 800, 600)
# 创建画布
self.canvas = Canvas(self)
self.setCentralWidget(self.canvas)
# 创建右侧静态区按钮
self.capture_button = QPushButton('Capture', self)
self.capture_button.clicked.connect(self.on_capture_button_clicked)
# 布局
layout = QVBoxLayout()
layout.addWidget(self.canvas)
layout.addWidget(self.capture_button)
widget = QWidget()
widget.setLayout(layout)
self.showMaximized()
def on_capture_button_clicked(self):
# 获取左侧图像并存储到右侧
left_image = self.canvas.left_image.copy()
self.canvas.right_image = left_image
class Canvas(QWidget):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.initUI()
def initUI(self):
self.setGeometry(0, 0, 800, 600)
self.left_image = None
self.right_image = None
# 显示左侧图像
self.left_label = QLabel(self)
self.left_label.setGeometry(0, 0, 400, 600)
self.update_left_image()
# 显示右侧图像
self.right_label = QLabel(self)
self.right_label.setGeometry(400, 0, 400, 600)
self.update_right_image()
def update_left_image(self):
if self.left_image is not None:
qImg = QImage(self.left_image.data,
self.left_image.shape[1], self.left_image.shape[0],
QImage.Format_RGB888)
self.left_label.setPixmap(QPixmap.fromImage(qImg))
def update_right_image(self):
if self.right_image is not None:
qImg = QImage(self.right_image.data,
self.right_image.shape[1], self.right_image.shape[0],
QImage.Format_RGB888)
self.right_label.setPixmap(QPixmap.fromImage(qImg))
def draw_features(self, img, features):
# 绘制特征点
for feature in features:
x, y = feature
cv2.circle(img, (x, y), 5, (0, 255, 0), -1)
def mousePressEvent(self, event):
# 记录鼠标按下的位置
self.mouse_start_position = (event.x(), event.y())
def mouseMoveEvent(self, event):
# 如果没有开始拖动,则不做处理
if not hasattr(self, 'mouse_start_position'):
return
# 计算拖动矩形框的尺寸
x1, y1 = self.mouse_start_position
x2, y2 = (event.x(), event.y())
width = max(x1, x2) - min(x1, x2)
height = max(y1, y2) - min(y1, y2)
# 在右侧图像上绘制可拖动的矩形框
self.update_right_image()
self.draw_features(self.right_image, [(x1, y1), (x2, y2)])
self.update_right_image()
def mouseReleaseEvent(self, event):
# 清理之前绘制的矩形框
self.update_right_image()
# 定义特征提取函数(示例)
def extract_features(image):
# 这里可以根据您的需求实现特征提取算法
# 例如,可以使用边缘检测、颜色空间转换等技术提取特征
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
features = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
return features
# 定义相似度计算函数(示例)
def calculate_similarity(template, image):
# 这里可以根据您的需求实现相似度计算算法
# 例如,可以使用模板匹配(Template Matching)计算相似度
res = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
return max_val
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
‘’‘
使用模板匹配来计算图像之间的相似度,
并在左侧动态区实时捕捉与右侧相似特征图像
在右侧静态区使用鼠标进行目标特征图像框选的功能
’‘’