def __createAngularMatrix(self, matrix, kernelSide): h, w = matrix.shape[:2] angularMatrix = {} negRefVector = (2 * kernelSide + 1, 0) criterion = lambda x: 255 == x y = 0 while y < h: x = 0 while x < w: if 255 == matrix[y][x]: intercepts = self.__getIntercepts(matrix, (x, y), kernelSide, criterion) num_intercepts = len(intercepts) if 4 >= num_intercepts and num_intercepts > 0: point = (intercepts[0], intercepts[-1]) dirVector = (intercepts[0][0] - intercepts[-1][0], intercepts[0][1] - intercepts[-1][1]) angle = math.degrees(BasicFunctions.getAngleBetweenTwoVectors(negRefVector, dirVector)) if angle > 90 and 0 != dirVector[0] and 0 != dirVector[1] and dirVector[1] / dirVector[0] >= 0: angle = 180 - angle angularMatrix[(x, y)] = (angle, point) x += 1 y += 1 return angularMatrix
class CircuitPartConnector: def __init__(self, edges, completeImage, elemMark=255): self.__edges = edges self.__completeImage = np.float32(completeImage) self.__elemMark = elemMark self.__equivalenceClass = {} self.__maxDst = 1 def connect(self, iteration=1): if iteration < 1: return while iteration > 0: numComponents, self.__labeledImage = cv2.connectedComponents( self.__edges) h, w = self.__edges.shape[:2] y = 1 while y < h - 1: x = 1 while x < w - 1: if 0 != self.__edges[y][x]: self.__propagate((x, y)) x += 1 y += 1 iteration -= 1 def getMaxDst(self): return self.__maxDst def __isADeadEnd(self, (x, y)): num_zeros = 0 borders = BasicFunctions.getBorders((x, y), 1, self.__edges.shape[:2]) import copy b = copy.deepcopy(borders) f = None try: for fromPos, toPos, _, step in borders: while True: f = fromPos if 0 == self.__edges[fromPos[1]][fromPos[0]]: num_zeros += 1 if fromPos == toPos: break fromPos[0] += step[0] fromPos[1] += step[1] except IndexError: print "at (%d, %d)" % (x, y) print "borders: %s" % (str(b)) print "because of: %s" % (str(f)) raise IndexError return num_zeros >= 7
class CurveConnector: def __init__(self, matrix, lineMark=1, elemMark=150, connectionMark=100): self.__matrix = matrix self.__lineMark = lineMark self.__elemMark = elemMark self.__connectionMark = connectionMark self.__foundTheLink = False self.__h, self.__w = self.__matrix.shape[:2] self.__link = None def connect(self): h, w = self.__matrix.shape[:2] y = 1 while y < h - 1: x = 1 while x < w - 1: if self.__lineMark == self.__matrix[y][ x] and self.__isACurveEnd((x, y)): self.__foundTheLink = False self.__propagate((x, y)) cv2.line(self.__matrix, (x, y), self.__link, self.__connectionMark, 1) x += 1 y += 1 def __propagate(self, (x, y)): half_size = 1 while not self.__foundTheLink: borders = BasicFunctions.getBorders((x, y), half_size, self.__matrix.shape[:2]) for from_pos, to_pos, _, step in borders: while True: cur_pixel = self.__matrix[from_pos[1]][from_pos[0]] if self.__isACurveEnd((from_pos[0], from_pos[1])): self.__foundTheLink = True self.__link = (from_pos[0], from_pos[1]) return if to_pos == from_pos: break from_pos[0] += step[0] from_pos[1] += step[1] half_size += 1
def __getIntercepts(self, matrix, center, kernelHalfSize, criterion): h, w = matrix.shape[:2] borders = BasicFunctions.getBorders(center, kernelHalfSize, matrix.shape[:2]) intercepts = [] for from_pos, to_pos, prev_pixel_loc, step in borders: prev_pixel = matrix[prev_pixel_loc[1]][prev_pixel_loc[0]] while True: if criterion(matrix[from_pos[1]][from_pos[0]], prev_pixel): intercepts.append((from_pos[0], from_pos[1])) prev_pixel = matrix[from_pos[1]][from_pos[0]] if to_pos == from_pos: break from_pos[0] += step[0] from_pos[1] += step[1] return intercepts
if self.__isACurveEnd((from_pos[0], from_pos[1])): self.__foundTheLink = True self.__link = (from_pos[0], from_pos[1]) return if to_pos == from_pos: break from_pos[0] += step[0] from_pos[1] += step[1] half_size += 1 def __isACurveEnd(self, (x, y)): borders = BasicFunctions.getBorders((x, y), 1, self.__matrix.shape[:2]) hasElemMark = False hasConnectionMark = False for from_pos, to_pos, _, step in borders: while True: cur_pixel = self.__matrix[from_pos[1]][from_pos[0]] if self.__elemMark == cur_pixel: hasElemMark = True elif self.__connectionMark == cur_pixel: hasConnectionMark = True if to_pos == from_pos: break
import class_morphology_user_circuit_element_finder as mucef import class_region_growing_circuit_element_finder as rgcef import class_wires_remover2 as wr2 import class_wires_remover as wr # img = cv2.imread('D://Thesis//test_images//img//b_rot.jpg') img = cv2.imread('D://Thesis//old circuit lens//test_images//test_004.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) noise_remover = pcnr.PrintedCircuitNoiseRemover() clean_img = noise_remover.filter(gray) circuit_finder = cf.CircuitFinder() circuit = circuit_finder.find(clean_img) cropped_gray = bf.crop(clean_img, circuit) cropped_copy = cropped_gray.copy() # circuit_elements_finder = rgcef.RegionGrowingCircuitElementFinder() circuit_elements_finder = wr.WiresRemover() circuit_elements_finder.setKernelSide(6) # circuit_elements_finder = mucef.MorphologyUserCircuitElementFinder() # circuit_elements_finder.setLineThickness(circuit_finder.getLineThickness()) # circuit_elements_finder.setKernelSide(circuit_finder.getKernelSide()) cropped_img = bf.crop(img, circuit) circuit_elements = circuit_elements_finder.find(cropped_gray, cropped_img)
fromPos[0] += step[0] fromPos[1] += step[1] except IndexError: print "at (%d, %d)" % (x, y) print "borders: %s" % (str(b)) print "because of: %s" % (str(f)) raise IndexError return num_zeros >= 7 def __propagate(self, (x, y)): classValue = self.__labeledImage[y][x] halfSide = 1 while True: borders = BasicFunctions.getBorders((x, y), halfSide, self.__edges.shape[:2]) for fromPos, toPos, _, step in borders: while True: currentClass = self.__labeledImage[fromPos[1]][fromPos[0]] if self.__completeImage[fromPos[1]][fromPos[0]] != 0: self.__completeImage[fromPos[1]][ fromPos[0]] = classValue if 0 != currentClass and classValue != currentClass and self.__hasThisNeighbour( (fromPos[0], fromPos[1]), classValue): cv2.line(self.__edges, (x, y), (fromPos[0], fromPos[1]), self.__elemMark, 1) if self.__maxDst < halfSide: