执行代码我得到了带转换后图像的opencv窗口,我明白了 开始和结束pix位置打印但矩形没有 出现在图像上。那是为什么?
import sys from PyQt4 import QtGui, uic import cv2
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
uic.loadUi('now.ui', self)
self.show()
self.open.clicked.connect(self.dialog)
self.actionOpen.triggered.connect(self.dialog)
def do_sth(self):
print('works fine')
def matp(self, name):
image = Image.open(name).convert("L")
arr = np.asarray(image)
plt.imshow(arr, cmap='Greys_r')
plt.show()
def dialog(self):
self.fileDialog = QtGui.QFileDialog(self)
file_name = self.fileDialog.getOpenFileName()
self.label.setText("File Path: " + str(file_name))
self.print_sth(file_name)
def print_sth(self, name):
mode = True
box1 = []
box2 = []
def interactive_drawing(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
if len(box1) is not 0:
box1.pop(0)
b1 = x, y
print('Start Mouse Position: ' + str(x) + ', ' + str(y))
box1.append(b1)
elif event == cv2.EVENT_LBUTTONUP:
if len(box2) is not 0:
box2.pop(0)
print('End Mouse Position: ' + str(x) + ', ' + str(y))
b2 = x, y
box2.append(b2)
if mode == True:
cv2.rectangle(img, (box1[0]), (box2[0]), (255, 255, 255), 1)
img = cv2.imread(name, 0)
cv2.imshow('image', img)
cv2.setMouseCallback('image', interactive_drawing)
cv2.waitKey(0)
cv2.destroyAllWindows()
def close_application(self):
sys.exit()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())
PS。当我将它单独放在一个新文件中时,唯一的interactive_drawing
函数就可以正常工作了。