opencv_img=cv2.imread("111.png", 1)
if opencv_img is None:
print ('None')
else:
size = (int(self.label.width()),int(self.label.height()))
shrink = cv2.resize(opencv_img, size, interpolation=cv2.INTER_AREA)
#cv2.imshow('img', shrink)
shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB)
self.QtImg = QtGui.QImage(shrink.data,
shrink.shape[1],
shrink.shape[0],
QtGui.QImage.Format_RGB888)
self.label.setPixmap(QtGui.QPixmap.fromImage(self.QtImg))
这是运行后的效果,右边img的小框是cv自带函数imshow的效果,放到Qlabel里,发生扭曲,考虑到很可能是numpy数组和Qlmage的每行对齐有问题。
查阅到Qlmage的参数(data, width, height, bytesPerLine, format ) 其实bytesPerline我并没有用到,字面意思也能看出这是指每行字节数,那一定和width有关,其次因为我显示的是彩色图片,具有三个通道,所以这一个参数应该写width*3。
self.QtImg = QtGui.QImage(shrink.data,
shrink.shape[1],
shrink.shape[0],
shrink.shape[1]*3,
QtGui.QImage.Format_RGB888)