cv2.namedWindow(name, cv2.WINDOW_NORMAL)
时间: 2024-05-25 16:09:59 浏览: 17
这个函数是 OpenCV 中创建窗口的函数,它有两个参数:窗口名称和窗口标志。其中第二个参数 cv2.WINDOW_NORMAL 表示创建一个可调整大小的窗口,可以通过拖动边缘来调整窗口的大小。如果使用默认参数 cv2.WINDOW_AUTOSIZE,则创建的窗口大小是固定的,无法调整大小。此函数只是创建一个空白的窗口,如果需要显示图像或视频,则需要使用 cv2.imshow() 函数。
相关问题
def start(): global img, pre_pts try: root = tk.Tk() root.withdraw() f_path = filedialog.askopenfilename() if not f_path: messagebox.showinfo('提示', '请选取有效图片') else: img = cv.imread(f_path) pre_pts = -1, -1 cv.namedWindow('picture', cv.WINDOW_NORMAL) cv.resizeWindow('picture', 256, 256) cv.moveWindow('picture', 600, 300) cv.imshow('picture', img) cv.setMouseCallback('picture', draw) cv.waitKey(0) cv.destroyAllWindows() root.destroy() except Exception as e: print("出现错误:", e)上述代码报错时,下列主界面会卡住并退出程序:class Faceshow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(Faceshow, self).__init__(parent) self.setupUi(self) self.main_button.clicked.connect(start) self.main_exit.clicked.connect(self.quit) def quit(self): self.close() if __name__ == '__main__': hxz = QApplication(sys.argv) ui = Faceshow() ui.show() sys.exit((hxz.exec_()))
可能是因为出现了异常,但是异常没有被捕获,导致程序直接退出。你可以在 `start()` 函数中加上异常处理,例如用 `try...except` 块包裹代码,打印出错误信息,然后使程序继续运行。这样就可以避免程序直接退出。修改后的代码如下:
```
def start():
global img, pre_pts
try:
root = tk.Tk()
root.withdraw()
f_path = filedialog.askopenfilename()
if not f_path:
messagebox.showinfo('提示', '请选取有效图片')
else:
img = cv.imread(f_path)
pre_pts = -1, -1
cv.namedWindow('picture', cv.WINDOW_NORMAL)
cv.resizeWindow('picture', 256, 256)
cv.moveWindow('picture', 600, 300)
cv.imshow('picture', img)
cv.setMouseCallback('picture', draw)
cv.waitKey(0)
cv.destroyAllWindows()
root.destroy()
except Exception as e:
print("出现错误:", e)
traceback.print_exc()
class Faceshow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Faceshow, self).__init__(parent)
self.setupUi(self)
self.main_button.clicked.connect(start)
self.main_exit.clicked.connect(self.quit)
def quit(self):
self.close()
if __name__ == '__main__':
hxz = QApplication(sys.argv)
ui = Faceshow()
ui.show()
sys.exit((hxz.exec_()))
```
这样程序就不会卡住并退出了,而是会在控制台中打印出错误信息,方便你进行调试。
cv2 python 多线程调用摄像头_Python中的多线程cv2.imshow()可以
实现多线程调用摄像头并显示的功能。具体实现方法如下:
1. 导入必要的库
```python
import cv2
import threading
```
2. 定义摄像头读取线程
```python
class CameraThread(threading.Thread):
def __init__(self, camera_id):
threading.Thread.__init__(self)
self.camera = cv2.VideoCapture(camera_id)
self.frame = None
self.running = True
def run(self):
while self.running:
ret, frame = self.camera.read()
if ret:
self.frame = frame
self.camera.release()
def stop(self):
self.running = False
```
3. 定义显示线程
```python
class DisplayThread(threading.Thread):
def __init__(self, name, frame):
threading.Thread.__init__(self)
self.name = name
self.frame = frame
self.running = True
def run(self):
cv2.namedWindow(self.name, cv2.WINDOW_NORMAL)
while self.running:
if self.frame is not None:
cv2.imshow(self.name, self.frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
self.running = False
cv2.destroyAllWindows()
def stop(self):
self.running = False
```
4. 创建摄像头读取线程和显示线程并启动
```python
camera_thread = CameraThread(0)
display_thread = DisplayThread('Camera', camera_thread.frame)
camera_thread.start()
display_thread.start()
camera_thread.join()
display_thread.stop()
display_thread.join()
```
5. 最后别忘了在程序结束时释放摄像头资源
```python
cv2.destroyAllWindows()
camera_thread.stop()
```
这样就可以实现多线程调用摄像头并显示的功能了。