前提・実現したいこと
PyQt5とOpenCVを用いてボタンを押したらエッジ抽出してくれるGUIの作成
Executeボタンを押したら画面に表示された画像のエッジを抽出してくれるGUI
補足に記載されているURLを参考に進めてきました。正直Pythonを扱って一ヶ月ほどです。問題解決にあたり参考となるような資料も提示していただけると幸いです
追記
opencv_canny.py単体でしたら画像のエッジ抽出ができます。
『if name=='main':』以下のコードはそれを確認するためのコードになっています。
mainother.pyをコンパイルした際に上記のようなメッセージがでてきます。
発生している問題・エラーメッセージ
File " ~~/mainother.py",line 42, in exe_canny cv_testOpencv_test(file) NameError: name 'file' is not defined
該当のソースコード
Python3
1###mainnother.py 2from __future__ import with_statement 3 4import numpy as np 5import os 6from opencv_canny import * 7import sys 8from PyQt5 import * 9from pyqt_Opencv import * ###このファイルはUIをQtdesignerで作成し.uiを.pyに変えたものです 10 11 12class DesignerMainWindow(QtWidgets.QMainWindow,Ui_Qt_CV_MainWindow): 13 def __init__(self,parent=None): 14 super(DesignerMainWindow, self).__init__(parent) 15 self.ui = Ui_Qt_CV_MainWindow() 16 self.setupUi(self) 17 18 #シグナルとスロットの設定場所 19 self.file_button.clicked.connect(self.open_file) 20 self.exec_button.clicked.connect(self.exe_canny) 21 22 def open_file(self): 23 24 file = QtWidgets.QFileDialog.getOpenFileName()#ここでファイルからデータを読み込み 25 if file:#データが入ってたらif文にも入れる 26 self.file_edit.setText(file[0]) 27 self.scene = QtWidgets.QGraphicsScene() 28 pic_Item = QtWidgets.QGraphicsPixmapItem(QtGui.QPixmap(file[0])) 29 __width = pic_Item.boundingRect().width() 30 __height = pic_Item.boundingRect().height() 31 __x = self.pic_View.x() 32 __y = self.pic_View.y() 33 self.pic_View.setGeometry(QtCore.QRect(__x, __y, __width, __height)) 34 __main_x = int(__x + __width + 20) 35 __main_y = int(__y + __height + 50) 36 self.resize(__main_x,__main_y) 37 self.scene.addItem(pic_Item) 38 self.pic_View.setScene(self.scene) 39 return file 40 41 def exe_canny(self): 42 cv_test=Opencv_test(file) 43 pic,pic2=cv_test.open_pic(self.file[0]) 44 self.cv_img=cv_test.canny(pic2) 45 height,width,dim=self.cv_img.shape 46 bytesPerLine=dim*width 47 self.image=QtWidgets.QImage(self.cv_img.data,width,height,bytesPerLine,QtWidgets.QImage.Format_RGB888) 48 pic_Item=QtWidgets.QGraphicsPixmapItem(QtWidgets.QPixmap.fromImage(self.image)) 49 self.scene.addItem(pic_Item) 50 51if __name__ == '__main__': 52 app = QtWidgets.QApplication(sys.argv) 53 dmw = DesignerMainWindow() 54 dmw.show() 55 sys.exit(app.exec_()) 56 57######################opencv_canny.py################################# 58import numpy as np 59import cv2 60 61class Opencv_test: 62 def __init__(self): 63 self.file=file 64 65 def open_pic(self,file): 66 pic=cv2.imread(file) 67 pic_color=cv2.cvtColor(pic,cv2.COLOR_BGR2RGB) 68 return pic,pic_color 69 70 def canny(self,pic): 71 img=cv2.cvtColor(pic,cv2.COLOR_BGR2GRAY) 72 edges=cv2.Canny(img,100,200) 73 edges2=np.zeros_like(pic) 74 for i in (0,1,2): 75 edges2[:,:,i]=edges 76 add=cv2.addWeighted(pic,1,edges2,0.4,0) 77 return add 78if __name__ == '__main__': 79 #以下テスト用 80 81 file="/media/atsushi/98501FCD501FB0CC/searchImge/depth.tif" 82 a=Opencv_test() 83 b,c=a.open_pic(file) 84 d=a.canny(b) 85 cv2.imshow("",d) 86 cv2.waitKey(0) 87 cv2.destroyAllWindows() 88 89
試したこと
定義されていないといわれたからおとなしく__init__でfileを定義してみたが
class Opencv_test: def __init__(self,file): self.file=file
TypeError:init()missing 1 required positional argument: 'file'
補足情報(FW/ツールのバージョンなど)
http://tatabox.hatenablog.com/entry/2014/09/02/185727(参照ページ)
ubuntu18.04 OpenCV3.4.3 PyQt5 Python3.6.6
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/15 06:32
2018/11/15 07:11
2018/11/19 03:23