1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| '''
Draw an image with the pHT of the original one
'''
from skimage.transform import hough, probabilistic_hough
from skimage.filter import canny
from skimage import data
from PySide import QtCore, QtGui
import numpy as np
import matplotlib.pyplot as plt
import sys
"""
Class line will have all the attribute of each line of the molecule
"""
class Line(QtCore.QObject):
def __init__(self):
QtCore.QObject.__init__(self)
self._start = QtCore.QPoint()
self._end = QtCore.QPoint()
#Get/Set for start/end of each line
def start(self):
return self._start
def setStart(self, start):
self._start = start
def end(self):
return self._end
def setEnd(self,end):
self._end = end
"""
Test of probabilistic hough and also selection of object
"""
class TestWidget(QtGui.QMainWindow):
def __init__(self):
super(TestWidget,self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 280, 270)
self.setWindowTitle('Image Skeleton ')
self._scene = QtGui.QGraphicsScene()
self._view = QtGui.QGraphicsView(self._scene)
self.setCentralWidget(self._view)
# Array with items (lines) of the scene
self._items = []
self.addLines()
def addLines(self):
fname = "molA.bmp" #cf. rar en Pièce jointe
image = data.imread(fname, True, None)
#Border detection of the image
edges = canny(image, 2, 1, 25)
"""
threshold : int
Threshold (level of intensity between 10 and 60)
line_length : int, optional (default 50)
Minimum accepted length of detected lines.
Increase the parameter to extract longer lines.
line_gap : int, optional, (default 10)
Maximum gap between pixels to still form a line.
Increase the parameter to merge broken lines more aggresively.
"""
lines = probabilistic_hough(edges, threshold=20, line_length=5, line_gap=3)
"""
lines : list
List of lines identified, lines in format ((x0, y0), (x1, y0)), indicating line start and end.
"""
pen = QtGui.QPen()
pen.setWidth(3)
for line in lines:
p0, p1 = line
"""
Each time we create a instance of Line to keep the coordinate
of each line drawn
"""
lineItem = Line()
lineItem.setStart(QtCore.QPoint(p0[0],p0[1]))
lineItem.setEnd(QtCore.QPoint(p1[0],p1[1]))
self._items.append(lineItem)
self._scene.addLine(p0[0],p0[1],p1[0],p1[1],pen)
def mousePressEvent(self, event):
x,y = event.pos().x(), event.pos().y()
#Test mouse pos
print " Pos mouse : x,y", '(',x,",",y,")" ,
if event.button() == QtCore.Qt.MouseButton.RightButton:
print "right click"
pen = QtGui.QPen()
pen.setWidth(3)
pen.setColor(QtGui.QColor(QtCore.Qt.red))
for i in range (len(self._items)):
item = self._items[i]
if item.start().x()<= x <= item.end().x():
print "x valide"
print "item start end y :" ,item.start().y(), item.end().y()
print "pose ee ej ne y :", y
"""
FIXME:
bug with y coordinnates strange results not like it should be
y value of line are not matching y of the mouse
"""
if item.start().y() <= y <= item.end().y():
print "if valide for x et y inside"
self._scene.addLine(QtCore.QLineF(item.start(),item.end()),pen)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
widget = TestWidget()
widget.resize(640, 480)
widget.show()
sys.exit(app.exec_()) |
Partager