Sunday, July 6, 2014

OpenCV Qt4 python Line detection code

there are 2 files. This one is 'opencvLineDetect.py' , which is the main file & gui.
 
import cv2
import numpy as np
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide import QtGui
from PySide import QtCore
from math import *
from CVLineDetection import CVLineDetection

imgPath01 = ['PlanSK_XS.jpg','dummy','dummy']
imgPath02 = 'temp.jpg'
pixelScale = 350
sliderMax = 200
sliderMin = 1
class LineDetection(QtGui.QWidget):
    
    
    def __init__(self):
        super(LineDetection, self).__init__()

 self.pixel = 1
 self.threshold=40
 self.minLineLength=5
 self.maxLineGap=30
        
        self.initUI()
        
        
    def initUI(self):
 #imgPath01 = 'PlanSK_S.jpg'
 #imgPath02 = 'temp.jpg'
 
 
        self._CVLineDetection = CVLineDetection(imgPath01[0])
        self._CVLineDetection.updateImage(imgPath01[0],self.pixel,self.threshold,self.minLineLength,self.maxLineGap)
        
        #labelSliderTxt = QtGui.QLabel('Sliders Left to Right: Pixel, Threshold, MinLineLength, maxLineGap', self)
        fbButton = QtGui.QPushButton("Choose File")
        fbButton.clicked.connect(self.pickFile)
        
        self.slider01TopTxt = QtGui.QLabel('Pixel',self)
        self.slider02TopTxt = QtGui.QLabel('Threshold',self)
        self.slider03TopTxt = QtGui.QLabel('MinLineLength',self)
        self.slider04TopTxt = QtGui.QLabel('MaxLineGap',self)
        
        self.slider01TopLcd = QtGui.QLCDNumber(self)
        self.slider01TopLcd.setSegmentStyle(QtGui.QLCDNumber.Flat)
        self.slider01TopLcd.display(self.pixel)
        self.slider02TopLcd = QtGui.QLCDNumber(self)
        self.slider02TopLcd.setSegmentStyle(QtGui.QLCDNumber.Flat)
        self.slider02TopLcd.display(self.threshold)
        self.slider03TopLcd = QtGui.QLCDNumber(self)
        self.slider03TopLcd.setSegmentStyle(QtGui.QLCDNumber.Flat)
        self.slider03TopLcd.display(self.minLineLength)
        self.slider04TopLcd = QtGui.QLCDNumber(self)
        self.slider04TopLcd.setSegmentStyle(QtGui.QLCDNumber.Flat)
        self.slider04TopLcd.display(self.maxLineGap)
        
        #change pixel
        self.slider = QtGui.QSlider(QtCore.Qt.Vertical)
        self.slider.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.slider.setTickPosition(QtGui.QSlider.TicksBothSides)
        self.slider.setTickInterval(10)
        self.slider.setSingleStep(1)
        self.slider.setValue(self.pixel)
        self.slider.setMaximum(sliderMax)
        self.slider.setMinimum(sliderMin)
        
        self.slider.valueChanged.connect(self.slider01TopLcd.display)
        self.slider.valueChanged.connect(self.refreshImage)
        
        self.slider02 = QtGui.QSlider(QtCore.Qt.Vertical)
        self.slider02.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.slider02.setTickPosition(QtGui.QSlider.TicksBothSides)
        self.slider02.setTickInterval(10)
        self.slider02.setSingleStep(1)
        self.slider02.setValue(self.threshold)
        self.slider02.setMaximum(sliderMax)
        
        self.slider02.valueChanged.connect(self.slider02TopLcd.display)
        self.slider02.valueChanged.connect(self.refreshImage)
        
        self.slider03 = QtGui.QSlider(QtCore.Qt.Vertical)
        self.slider03.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.slider03.setTickPosition(QtGui.QSlider.TicksBothSides)
        self.slider03.setTickInterval(10)
        self.slider03.setSingleStep(1)
        self.slider03.setValue(self.minLineLength)
        self.slider03.setMaximum(sliderMax)
        
        self.slider03.valueChanged.connect(self.slider03TopLcd.display)
        self.slider03.valueChanged.connect(self.refreshImage)
        
        self.slider04 = QtGui.QSlider(QtCore.Qt.Vertical)
        self.slider04.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.slider04.setTickPosition(QtGui.QSlider.TicksBothSides)
        self.slider04.setTickInterval(10)
        self.slider04.setSingleStep(1)
        self.slider04.setValue(self.maxLineGap)
        self.slider04.setMaximum(sliderMax)
        
        
        self.slider04.valueChanged.connect(self.slider04TopLcd.display)
        self.slider04.valueChanged.connect(self.refreshImage)
        
        vboxSlider01 = QtGui.QVBoxLayout()
        vboxSlider01.addStretch(0)
        vboxSlider01.addWidget(self.slider01TopTxt)
        vboxSlider01.addWidget(self.slider01TopLcd)
        vboxSlider01.addWidget(self.slider)
        
        vboxSlider02 = QtGui.QVBoxLayout()
        vboxSlider02.addStretch(0)
        vboxSlider02.addWidget(self.slider02TopTxt)
        vboxSlider02.addWidget(self.slider02TopLcd)
        vboxSlider02.addWidget(self.slider02)
        
        vboxSlider03 = QtGui.QVBoxLayout()
        vboxSlider03.addStretch(0)
        vboxSlider03.addWidget(self.slider03TopTxt)
        vboxSlider03.addWidget(self.slider03TopLcd)
        vboxSlider03.addWidget(self.slider03)
        
        vboxSlider04 = QtGui.QVBoxLayout()
        vboxSlider04.addStretch(0)
        vboxSlider04.addWidget(self.slider04TopTxt)
        vboxSlider04.addWidget(self.slider04TopLcd)
        vboxSlider04.addWidget(self.slider04)
        
        #first image
        pixmap01 = QtGui.QPixmap(imgPath01[0])
        pixmap01 = pixmap01.scaled(pixelScale, pixelScale, QtCore.Qt.KeepAspectRatio) 
        self.imageLable01 = QtGui.QLabel(self)
        self.imageLable01.setPixmap(pixmap01)
        #second image
        pixmap02 = QtGui.QPixmap(imgPath02)
        pixmap02 = pixmap02.scaled(pixelScale, pixelScale, QtCore.Qt.KeepAspectRatio) 
        self.imageLable02 = QtGui.QLabel(self)
        self.imageLable02.setPixmap(pixmap02)
        #add image lable to horizontal box layout
        hbox01 = QtGui.QHBoxLayout()
        hbox01.addStretch(1)
        hbox01.addWidget(self.imageLable01)
        hbox01.addWidget(self.imageLable02)
        hbox01.addLayout(vboxSlider01)
        hbox01.addLayout(vboxSlider02)
        hbox01.addLayout(vboxSlider03)
        hbox01.addLayout(vboxSlider04)
        
        

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch(1)
        #hbox.addWidget(labelSliderTxt)
        hbox.addWidget(fbButton)
        

        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox01)
        # add sliders
        
        vbox.addLayout(hbox)
        
        self.setLayout(vbox)    
        
        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle('OpenCV PythonSide Line Detection')    
        self.show()
    def pickFile(self):
        global imgPath01
 imgPath01 = QtGui.QFileDialog.getOpenFileName(self,'Open File',"~/","Images (*.png *.jpg)")
 print imgPath01
 
 self.refreshImage()
 pass
    def refreshImage(self):
        global imgPath01
        self.pixel = self.slider01TopLcd.intValue()
        self.threshold = self.slider02TopLcd.intValue()
        self.minLineLength = self.slider03TopLcd.intValue()
        self.maxLineGap = self.slider04TopLcd.intValue()
        
        self._CVLineDetection.updateImage(imgPath01[0],self.pixel,self.threshold,self.minLineLength,self.maxLineGap)
        pixmap01 = QtGui.QPixmap(imgPath01[0])
        pixmap01 = pixmap01.scaled(pixelScale, pixelScale, QtCore.Qt.KeepAspectRatio) 
        #self.imageLable02 = QtGui.QLabel(self)
        self.imageLable01.setPixmap(pixmap01)
        
        pixmap02 = QtGui.QPixmap(imgPath02)
        pixmap02 = pixmap02.scaled(pixelScale, pixelScale, QtCore.Qt.KeepAspectRatio) 
        #self.imageLable02 = QtGui.QLabel(self)
        self.imageLable02.setPixmap(pixmap02)
        
 pass
    def keyPressEvent(self,event):
 if event.key() == Qt.Key_Escape:
   self.close()
        
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = LineDetection()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
 
below is the second file ' CVLineDetection.py'
 

import cv2
import numpy as np

class CVLineDetection(object):
  def __init__(self, image01Path01):
    pass
    
  def updateImage(self,image01Path,threshold,minLineLength1,maxLineGap1):
    self.im = cv2.imread(image01Path)
    self.gray = cv2.cvtColor(self.im,cv2.COLOR_BGR2GRAY)
    self.edges = cv2.Canny(self.gray,150,200,apertureSize = 3)
    self.img = self.im.copy()
    
    self.lines = cv2.HoughLinesP(self.edges,1,np.pi/360,threshold, minLineLength=minLineLength1, maxLineGap=maxLineGap1)
    for x1,y1,x2,y2 in self.lines[0]:
      cv2.line(self.img,(x1,y1),(x2,y2),(0,255,0),2)
    cv2.imwrite('temp.jpg',self.img)
    

No comments:

Post a Comment