|
| 1 | +# ex2tron's blog: |
| 2 | +# http://ex2tron.wang |
| 3 | + |
| 4 | +importsys |
| 5 | +importcv2 |
| 6 | + |
| 7 | +fromPyQt5importQtCore,QtGui,QtWidgets |
| 8 | +fromPyQt5.QtCoreimport* |
| 9 | +fromPyQt5.QtGuiimport* |
| 10 | +fromPyQt5.QtWidgetsimportQFileDialog,QMainWindow |
| 11 | + |
| 12 | +frommainFormimportUi_MainWindow |
| 13 | + |
| 14 | + |
| 15 | +classPyQtMainEntry(QMainWindow,Ui_MainWindow): |
| 16 | +def__init__(self): |
| 17 | +super().__init__() |
| 18 | +self.setupUi(self) |
| 19 | + |
| 20 | +self.camera=cv2.VideoCapture(0) |
| 21 | +self.is_camera_opened=False# 摄像头有没有打开标记 |
| 22 | + |
| 23 | +# 定时器:30ms捕获一帧 |
| 24 | +self._timer=QtCore.QTimer(self) |
| 25 | +self._timer.timeout.connect(self._queryFrame) |
| 26 | +self._timer.setInterval(30) |
| 27 | + |
| 28 | +defbtnOpenCamera_Clicked(self): |
| 29 | +''' |
| 30 | + 打开和关闭摄像头 |
| 31 | + ''' |
| 32 | +self.is_camera_opened=~self.is_camera_opened |
| 33 | +ifself.is_camera_opened: |
| 34 | +self.btnOpenCamera.setText("关闭摄像头") |
| 35 | +self._timer.start() |
| 36 | +else: |
| 37 | +self.btnOpenCamera.setText("打开摄像头") |
| 38 | +self._timer.stop() |
| 39 | + |
| 40 | +defbtnCapture_Clicked(self): |
| 41 | +''' |
| 42 | + 捕获图片 |
| 43 | + ''' |
| 44 | +# 摄像头未打开,不执行任何操作 |
| 45 | +ifnotself.is_camera_opened: |
| 46 | +return |
| 47 | + |
| 48 | +self.captured=self.frame |
| 49 | + |
| 50 | +# 后面这几行代码几乎都一样,可以尝试封装成一个函数 |
| 51 | +rows,cols,channels=self.captured.shape |
| 52 | +bytesPerLine=channels*cols |
| 53 | +# Qt显示图片时,需要先转换成QImgage类型 |
| 54 | +QImg=QImage(self.captured.data,cols,rows, |
| 55 | +bytesPerLine,QImage.Format_RGB888) |
| 56 | +self.labelCapture.setPixmap(QPixmap.fromImage(QImg).scaled( |
| 57 | +self.labelCapture.size(),Qt.KeepAspectRatio,Qt.SmoothTransformation)) |
| 58 | + |
| 59 | +defbtnReadImage_Clicked(self): |
| 60 | +''' |
| 61 | + 从本地读取图片 |
| 62 | + ''' |
| 63 | +# 打开文件选取对话框 |
| 64 | +filename,_=QFileDialog.getOpenFileName(self,'打开图片') |
| 65 | +iffilename: |
| 66 | +self.captured=cv2.imread(str(filename)) |
| 67 | +# OpenCV图像以BGR通道存储,显示时需要从BGR转到RGB |
| 68 | +self.captured=cv2.cvtColor(self.captured,cv2.COLOR_BGR2RGB) |
| 69 | + |
| 70 | +rows,cols,channels=self.captured.shape |
| 71 | +bytesPerLine=channels*cols |
| 72 | +QImg=QImage(self.captured.data,cols,rows, |
| 73 | +bytesPerLine,QImage.Format_RGB888) |
| 74 | +self.labelCapture.setPixmap(QPixmap.fromImage(QImg).scaled( |
| 75 | +self.labelCapture.size(),Qt.KeepAspectRatio,Qt.SmoothTransformation)) |
| 76 | + |
| 77 | +defbtnGray_Clicked(self): |
| 78 | +''' |
| 79 | + 灰度化 |
| 80 | + ''' |
| 81 | +# 如果没有捕获图片,则不执行操作 |
| 82 | +ifnothasattr(self,"captured"): |
| 83 | +return |
| 84 | + |
| 85 | +self.cpatured=cv2.cvtColor(self.captured,cv2.COLOR_RGB2GRAY) |
| 86 | + |
| 87 | +rows,columns=self.cpatured.shape |
| 88 | +bytesPerLine=columns |
| 89 | +# 灰度图是单通道,所以需要用Format_Indexed8 |
| 90 | +QImg=QImage(self.cpatured.data,columns,rows, |
| 91 | +bytesPerLine,QImage.Format_Indexed8) |
| 92 | +self.labelResult.setPixmap(QPixmap.fromImage(QImg).scaled( |
| 93 | +self.labelResult.size(),Qt.KeepAspectRatio,Qt.SmoothTransformation)) |
| 94 | + |
| 95 | +defbtnThreshold_Clicked(self): |
| 96 | +''' |
| 97 | + Otsu自动阈值分割 |
| 98 | + ''' |
| 99 | +ifnothasattr(self,"captured"): |
| 100 | +return |
| 101 | + |
| 102 | +_,self.cpatured=cv2.threshold( |
| 103 | +self.cpatured,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) |
| 104 | + |
| 105 | +rows,columns=self.cpatured.shape |
| 106 | +bytesPerLine=columns |
| 107 | +# 阈值分割图也是单通道,也需要用Format_Indexed8 |
| 108 | +QImg=QImage(self.cpatured.data,columns,rows, |
| 109 | +bytesPerLine,QImage.Format_Indexed8) |
| 110 | +self.labelResult.setPixmap(QPixmap.fromImage(QImg).scaled( |
| 111 | +self.labelResult.size(),Qt.KeepAspectRatio,Qt.SmoothTransformation)) |
| 112 | + |
| 113 | +@QtCore.pyqtSlot() |
| 114 | +def_queryFrame(self): |
| 115 | +''' |
| 116 | + 循环捕获图片 |
| 117 | + ''' |
| 118 | +ret,self.frame=self.camera.read() |
| 119 | + |
| 120 | +img_rows,img_cols,channels=self.frame.shape |
| 121 | +bytesPerLine=channels*img_cols |
| 122 | + |
| 123 | +cv2.cvtColor(self.frame,cv2.COLOR_BGR2RGB,self.frame) |
| 124 | +QImg=QImage(self.frame.data,img_cols,img_rows, |
| 125 | +bytesPerLine,QImage.Format_RGB888) |
| 126 | +self.labelCamera.setPixmap(QPixmap.fromImage(QImg).scaled( |
| 127 | +self.labelCamera.size(),Qt.KeepAspectRatio,Qt.SmoothTransformation)) |
| 128 | + |
| 129 | + |
| 130 | +if__name__=="__main__": |
| 131 | +app=QtWidgets.QApplication(sys.argv) |
| 132 | +window=PyQtMainEntry() |
| 133 | +window.show() |
| 134 | +sys.exit(app.exec_()) |