如何去除框架周围的白条? PyQt5
How can I remove the white bars around the frame? PyQt5
虽然我选择了相同的相机框架尺寸和小部件尺寸,但window周围出现了空白。
如何去除这些白色?
我一直不明白为什么会出现这些白色部分。
虽然我选择了相同的相机框架尺寸和小部件尺寸,但window周围出现了空白。
如何去除这些白色?
我一直不明白为什么会出现这些白色部分。
'''
class VideoThread(QThread):
change_pixmap_signal = pyqtSignal(np.ndarray)
def __init__(self):
super().__init__()
self._run_flag = True
def run(self):
# capture from analog camera
cap = cv2.VideoCapture(0)
cap.set(3,720)
cap.set(4,576)
while self._run_flag:
now1=time.time()
ret, cv_img = cap.read()
if ret:
cv_img = cv2.resize(cv_img, (1024, 768))
self.change_pixmap_signal.emit(cv_img)
now2=time.time()
cap.release()
def stop(self):
#stop capture
self._run_flag = False
self.wait()
class App(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("window")
self.disply_width = 1024
self.display_height = 768
# create the label that holds the image
self.image_label.resize(self.disply_width, self.display_height)
# create a vertical box layout and add the two labels
vbox = QVBoxLayout()
vbox.addWidget(self.image_label)
#vbox.addWidget(self.textLabel)
# set the vbox layout as the widgets layout
self.setLayout(vbox)
# create the video capture thread
self.thread = VideoThread()
# connect its signal to the update_image slot
self.thread.change_pixmap_signal.connect(self.update_image)
# start the thread
self.thread.start()
def closeEvent(self, event):
self.thread.stop()
event.accept()
@pyqtSlot(np.ndarray)
def update_image(self, cv_img):
"""Updates the image_label with a new opencv image"""
qt_img = self.convert_cv_qt(cv_img)
self.image_label.setPixmap(qt_img)
def convert_cv_qt(self, cv_img):
"""Convert from an opencv image to QPixmap"""
rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w
convert_to_Qt_format = QtGui.QImage(rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)
p = convert_to_Qt_format.scaled(self.disply_width, self.display_height, Qt.KeepAspectRatio)
return QPixmap.fromImage(p)
if __name__ == "__main__":
app = QApplication(sys.argv)
a = App()
a.show()
#a.showFullScreen()
sys.exit(app.exec_())
'''
使用vbox.setContentsMargins(0, 0, 0, 0);
去除白边。
此外,您的代码中还有更多错误。例如,你不应该调整图像标签 self.image_label.resize(self.disply_width, self.display_height)
的大小(我想这应该会崩溃,因为你还没有创建标签......但我猜你没有显示所有代码)。您应该改为调整整个 window 的大小,即 self.resize(self.disply_width, self.display_height)
。好吧,看来您不了解布局的工作原理。但是我的回答并不是为了教你,我只是想回答你的问题。
虽然我选择了相同的相机框架尺寸和小部件尺寸,但window周围出现了空白。
如何去除这些白色?
我一直不明白为什么会出现这些白色部分。
虽然我选择了相同的相机框架尺寸和小部件尺寸,但window周围出现了空白。
如何去除这些白色?
我一直不明白为什么会出现这些白色部分。
'''
class VideoThread(QThread):
change_pixmap_signal = pyqtSignal(np.ndarray)
def __init__(self):
super().__init__()
self._run_flag = True
def run(self):
# capture from analog camera
cap = cv2.VideoCapture(0)
cap.set(3,720)
cap.set(4,576)
while self._run_flag:
now1=time.time()
ret, cv_img = cap.read()
if ret:
cv_img = cv2.resize(cv_img, (1024, 768))
self.change_pixmap_signal.emit(cv_img)
now2=time.time()
cap.release()
def stop(self):
#stop capture
self._run_flag = False
self.wait()
class App(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("window")
self.disply_width = 1024
self.display_height = 768
# create the label that holds the image
self.image_label.resize(self.disply_width, self.display_height)
# create a vertical box layout and add the two labels
vbox = QVBoxLayout()
vbox.addWidget(self.image_label)
#vbox.addWidget(self.textLabel)
# set the vbox layout as the widgets layout
self.setLayout(vbox)
# create the video capture thread
self.thread = VideoThread()
# connect its signal to the update_image slot
self.thread.change_pixmap_signal.connect(self.update_image)
# start the thread
self.thread.start()
def closeEvent(self, event):
self.thread.stop()
event.accept()
@pyqtSlot(np.ndarray)
def update_image(self, cv_img):
"""Updates the image_label with a new opencv image"""
qt_img = self.convert_cv_qt(cv_img)
self.image_label.setPixmap(qt_img)
def convert_cv_qt(self, cv_img):
"""Convert from an opencv image to QPixmap"""
rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w
convert_to_Qt_format = QtGui.QImage(rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)
p = convert_to_Qt_format.scaled(self.disply_width, self.display_height, Qt.KeepAspectRatio)
return QPixmap.fromImage(p)
if __name__ == "__main__":
app = QApplication(sys.argv)
a = App()
a.show()
#a.showFullScreen()
sys.exit(app.exec_())
'''
使用vbox.setContentsMargins(0, 0, 0, 0);
去除白边。
此外,您的代码中还有更多错误。例如,你不应该调整图像标签 self.image_label.resize(self.disply_width, self.display_height)
的大小(我想这应该会崩溃,因为你还没有创建标签......但我猜你没有显示所有代码)。您应该改为调整整个 window 的大小,即 self.resize(self.disply_width, self.display_height)
。好吧,看来您不了解布局的工作原理。但是我的回答并不是为了教你,我只是想回答你的问题。