Python/OpenCV:事件.pos()与正确的单击值不对应

2024-05-29 03:10:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我们使用的是python3.6和OpenCV。你知道吗

我们试图从一幅图像中的两点得到坐标(x,y)。你知道吗

我们有setFrame函数来显示图像,如下所示:

def setFrame(self):

        global video

        num = self.frame_scrollBar.value()
        self.video_frame.setScaledContents(True); #QLabel will resize itself to the size of pixmap/image and scroll-bar will get visible.
        cvRGBImg = cv2.cvtColor(video[num-1], cv2.COLOR_BGR2RGB) #Convert opencv RGB image to QImage
        qimg = QtGui.QImage(cvRGBImg.data,cvRGBImg.shape[1], cvRGBImg.shape[0], QtGui.QImage.Format_RGB888)
        qpm = QtGui.QPixmap.fromImage(qimg)
        self.video_frame.setPixmap(qpm)

此外,我们还有getPos函数来获得(x,y)坐标:

def getPos(self, event):

        global setPoint
        global coords
        global video

        if setPoint == True:
            x = event.pos().x()
            y = event.pos().y()

            coords.append((x,y))
            cv2.circle(video[int(self.frame_3.text())-1],(x,y),3,(0,0,255),-1)
            self.setFrame()
            if len(coords)==2:
                setPoint = False
                print (coords[0])
                print (coords[1])
                self.message()

问题是,当我们点击图像上的一个点时,坐标是不正确的。例如,我们点击一个点a,我们看到该点上方的圆,(x,y)与我们点击的点不对应。你知道吗

我们还尝试使用pygame获取坐标,但仍然没有成功。你知道吗

我们必须进行某种转变吗?如果我们不使用self.video_frame.setScaledContents(True),那么呈现的图像在QPixmap中显示为移位。 你能帮我们吗?你知道吗


Tags: 函数图像selfeventtruevideocoordscv2
1条回答
网友
1楼 · 发布于 2024-05-29 03:10:54

我们找到了解决办法。你知道吗

问题是QLabel大小与显示的图像大小不对应。你知道吗

QLabel大小为491x371,图像大小为512x512。所以我们必须转换点击的坐标,比如:

x = event.pos().x()
x = int(x*(512/491)) # coordinates/pixels must be integers
y = event.pos().y()
y = int(y*(512/371))

所以现在命令cv2.circle(video[int(self.frame_3.text())-1],(x,y),3,(0,0,255),-1)将圆设计在正确的位置。你知道吗

相关问题 更多 >

    热门问题