76 lines
2.6 KiB
Python
76 lines
2.6 KiB
Python
import cv2
|
|
import sys, time, os
|
|
import numpy as np
|
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
|
from test_pyqt5_0712_ui import Ui_MainWindow
|
|
from PyQt5.QtCore import QDate,QTime
|
|
from PyQt5.QtWidgets import QApplication, QFileDialog, QLabel,QMainWindow, QWidget, QPushButton
|
|
from PyQt5.QtCore import QThread, pyqtSignal
|
|
|
|
|
|
|
|
|
|
|
|
# 轉Qlabel的格式
|
|
def label_to_view(img): # 原圖
|
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # QT顏色顯示轉換
|
|
Ny, Nx, _ = img.shape
|
|
img = QtGui.QImage(img.data, Nx, Ny, Nx * 3, QtGui.QImage.Format_RGB888) # 須改格式
|
|
return img
|
|
|
|
|
|
|
|
|
|
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
|
|
def __init__(self, parent=None): #按鍵設定
|
|
super(MainWindow, self).__init__(parent)
|
|
self.setupUi(self)
|
|
|
|
self.view_1.setScaledContents(True); #設定view_1 自適應大小
|
|
|
|
self.open_file_bt.clicked.connect(self.open_file_clicked) # 按鍵open
|
|
self.horizontalSlider.valueChanged.connect(self.sliderValue) # 滑桿調整
|
|
|
|
self.img_check=False # 確認有無圖片
|
|
|
|
# 選擇圖片
|
|
def open_file_clicked(self):
|
|
file_filter = "Image Files (*.jpg *.jpeg *.png *.bmp *.mp4 *.avi);" # 規定格式
|
|
file_path, _ = QFileDialog.getOpenFileName(self, 'Open Image','',file_filter)
|
|
# 若沒圖 直接return 不執行下面程式
|
|
if file_path =="":
|
|
return
|
|
if file_path:
|
|
self.img_check = True
|
|
self.img = cv2.imread(file_path)
|
|
img = self.img.copy()
|
|
img = label_to_view(img)
|
|
self.view_1.setPixmap(QtGui.QPixmap.fromImage(img))
|
|
# 滑桿控制
|
|
def sliderValue(self):
|
|
# 若沒圖 直接return 不執行下面程式
|
|
if self.img_check==False:
|
|
return
|
|
|
|
binary_value = int(self.horizontalSlider.value()) # 二值化參數
|
|
self.horizontalSlider_text.setText(str(binary_value)) # 顯示數值
|
|
img = self.img.copy()
|
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
if binary_value < 2:
|
|
return
|
|
if (binary_value % 2 == 1):
|
|
img1 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, binary_value, 2) # 動態二值
|
|
else:
|
|
img1 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, binary_value + 1, 2) # 動態二值
|
|
img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
|
|
img = label_to_view(img1)
|
|
self.view_1.setPixmap(QtGui.QPixmap.fromImage(img))
|
|
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
app = QtWidgets.QApplication(sys.argv)
|
|
win = MainWindow()
|
|
win.show()
|
|
sys.exit(app.exec_()) |