盡管我選擇了相同的相機框架大小和小部件大小,但視窗周圍會出現空白。
我怎樣才能去除這些白人?
我一直不明白為什么會出現這些白色部分。
盡管我選擇了相同的相機框架大小和小部件大小,但視窗周圍會出現空白。
我怎樣才能去除這些白人?
我一直不明白為什么會出現這些白色部分。

'''
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_())
'''
uj5u.com熱心網友回復:
用于vbox.setContentsMargins(0, 0, 0, 0);去除白邊。
此外,您的代碼中還有更多錯誤。例如,您不應該調整影像標簽的大小self.image_label.resize(self.disply_width, self.display_height)(我猜這應該會崩潰,因為您還沒有創建標簽……但我猜您沒有顯示所有代碼)。您應該調整整個視窗的大小,即self.resize(self.disply_width, self.display_height). 好吧,您似乎不了解布局的作業原理。但是這個我的回答不是為了教你,我只是想回答你的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/463642.html
