def test_WrongImageDimensions(self): wrongDimensions1 = np.random.rand(1, 1, 1) wrongDimensions2 = np.random.rand(1) with self.assertRaises(Exception): EdgeDetector(wrongDimensions1) with self.assertRaises(Exception): EdgeDetector(wrongDimensions2)
def _detectEdges(self, image): edgeDetector = EdgeDetector(rgb2grayNaive(image.image)) edges = edgeDetector.getEdges( self._edgeDetectionConfig.sigma, self._edgeDetectionConfig.thresholdFactor) keepInsideStage = PipelineStage.KeepInsideRadiusStage( self._edgeDetectionConfig.radius) edges = keepInsideStage.execute(edges) trimmedEdges = self._trimEdges(edges) return _convertToMatrix(trimmedEdges)
def __init__(self): Detector.__init__(self) self.triggerDetector = EntityDetector() self.edgeDetector = EdgeDetector() self.unmergingDetector = UnmergingDetector() self.doUnmergingSelfTraining = True #False self.modifierDetector = ModifierDetector() #self.stEvaluator = Evaluators.BioNLP11GeniaTools #self.stWriteScores = False self.STATE_COMPONENT_TRAIN = "COMPONENT_TRAIN" self.tag = "event-"
def test_RealImage(self): image = rgb2grayNaive(readImage("images_unittest/1.jpg")) e = EdgeDetector(image) cloud = e.getEdges(2.2, 6.5) self.assertEqual(cloud.size(), 300) fewPointsCloud = e.getEdges(2.2, 26.0) self.assertEqual(fewPointsCloud.size(), 2) noPointsCloud = e.getEdges(2.2, 27.0) self.assertEqual(noPointsCloud.size(), 0)
def __init__(self): print(">>>>>>>>>>>>>>>TODO: Restore the latest set of options??") Ui_Dialog.__init__(self) QObject.__init__(self) self._filters = [ BlurFilter(), SimpleMotionDetection(), ActivityFilter(), HistogramFilter(), BlockNumber(), EdgeDetector() ]
def test_SimpleImage(self): simpleImage = np.zeros((12, 12)) simpleImage[1, 1] = 255 expectedPointCloud = PointCloud() expectedPointCloud.addXY(0.5, 0.0) expectedPointCloud.addXY(1.5, 2.0) expectedPointCloud.addXY(0.0, 0.5) expectedPointCloud.addXY(2.0, 1.5) e = EdgeDetector(simpleImage) pointCloud = e.getEdges(1.0, 17.0) for point, expectedPoint in zip(pointCloud, expectedPointCloud): self.assertEqual(point[0], expectedPoint[0]) self.assertEqual(point[1], expectedPoint[1])
def _drawReferenceImages(self): numPlotsPerSide = int(math.ceil(math.sqrt(len(self._referenceImages)))) print("numPlotsPerSide:", numPlotsPerSide) for ix, reference in enumerate(self._referenceImages): ax = self._referenceImagesFigure.add_subplot( numPlotsPerSide, numPlotsPerSide, ix + 1) edgeDetector = EdgeDetector(rgb2grayNaive(reference.image)) edges = edgeDetector.getEdges(self._sigma, self._thresholdFactor) keepInsideStage = PipelineStage.KeepInsideRadiusStage(self._radius) edges = keepInsideStage.execute(edges) imageToShow = reference.image.copy() addPointsToImage(imageToShow, edges, 1) ax.imshow(imageToShow) plt.show(block=False)
def _dumpPointsToFile(self): inputDirStr = _userCheckedInput("Image dir path: ") outputDirStr = _userCheckedInput("Output dir path: ") outputPostfixStr = _userCheckedInput("Output postfix str: ") imageReader = DirectoryImageReader(inputDirStr) for image in imageReader.generate(): print(image.comment) edgeDetector = EdgeDetector(rgb2grayNaive(image.image)) cloud = edgeDetector.getEdges(self._sigma, self._thresholdFactor) keepInsideStage = PipelineStage.KeepInsideRadiusStage(self._radius) cloud = keepInsideStage.execute(cloud) outFilePath = os.path.join(outputDirStr, image.comment + outputPostfixStr) outFile = open(outFilePath, "w") PointCloudHandler.savePointCloudToWriteable(cloud, outFile) outFile.close()
filePath1 = "../../images/early_tests_white/4_01.jpg" filePath2 = "../../images/early_tests_white/4_03.jpg" start = timer() img1 = imageio.imread(filePath1) img2 = imageio.imread(filePath2) end = timer() print("imread:", end - start) sigma = 2 thresholdFactor = 8.0 radius = 30 start = timer() edgeDetector1 = EdgeDetector(rgb2grayNaive(img1)) edgeDetector2 = EdgeDetector(rgb2grayNaive(img2)) edges1 = edgeDetector1.getEdges(sigma, thresholdFactor) keepInsideStage = PipelineStage.KeepInsideRadiusStage(radius) edges1 = keepInsideStage.execute(edges1) edges2 = edgeDetector2.getEdges(sigma, thresholdFactor) keepInsideStage = PipelineStage.KeepInsideRadiusStage(radius) edges2 = keepInsideStage.execute(edges2) end = timer() print("edgedetector:", end - start) points1 = np.zeros((edges1.size(), 2)) for ix, point in enumerate(edges1): points1[ix, 0] = point[0]
def execute(self, inData): print("Executing edge detector stage") edgeDetector = EdgeDetector(inData) self._executionResult = edgeDetector.getEdges(self._sigma, self._threshold) return self._executionResult
def makeDotToDot(fullFilePath, intermediateSteps=False): # Getting proper in/out names and image dimensions. fileName = os.path.split(fullFilePath)[-1] outPathJpg = 'out/jpg/' + fileName outPathPdf = 'out/pdf/' + os.path.splitext(fileName)[0] + '.pdf' imageData = Image.open(fullFilePath) width = imageData.width height = imageData.height # Canny edge detection step edgeDetector = EdgeDetector(fullFilePath) edgesNumberMatrix = timeFunction(edgeDetector.getCannyEdges) edgeMatrix = EdgeMatrix(edgesNumberMatrix) if intermediateSteps: outCanny = IntermediateImage([edgeMatrix.points], width, height) outCanny.colorWhiteSegments() outCanny.saveImage("intermediate/canny.jpg") # Trace following step edgeFollower = EdgeFollower(edgeMatrix, width, height) traces = timeFunction(edgeFollower.getTraces) if intermediateSteps: outEdges = IntermediateImage(traces, width, height) outEdges.colorAllSegments() outEdges.saveImage("intermediate/edges.jpg") # Trace to line conversion traceConverter = TraceConverter(traces) lines = timeFunction(traceConverter.getLines) if intermediateSteps: nonConnectedLinesOut = OutputNonConnectedLines(lines, width, height) nonConnectedLinesOut.saveImage("intermediate/lines.jpg") print('Lines to connect: ' + str(len(lines))) # Finding the best greedy solution lineConnector = LineConnector(lines) greedyLines = timeFunction(lineConnector.bestOfManyGreedys, GREEDY_SOLUTIONS_TO_TRY) greedyPoints = [point for sublist in greedyLines for point in sublist] if intermediateSteps: outGreedy = OutputImage(greedyPoints, width, height, True, False, "intermediate/notClean.pdf", "intermediate/notClean.jpg") # Cleaning up too close/far away dots print('Dots before clean: ' + str(len(greedyPoints))) dotCleaner = DotCleanup(greedyPoints, width, height) cleanPoints = timeFunction(dotCleaner.getCleanedDots) dotsInImage = len(cleanPoints) # Output image print('Dots in image: ' + str(dotsInImage)) out = OutputImage(cleanPoints, width, height, True, False, outPathPdf) out.saveImage(outPathJpg) return cleanPoints
from EdgeDetector import EdgeDetector from ImageUtilities import rgb2grayNaive import PipelineStage def addEdgesToImage(image, edges, colorIx): for point in edges: row = point[0] col = point[1] image[int(row), int(col), colorIx] = 255 if __name__ == "__main__": filePath = "../../images/TryEdgeDetector.jpg" img = imageio.imread(filePath) edgeDetector = EdgeDetector(rgb2grayNaive(img)) radius = 1000 sigma = 4 thresholdFactor = 0.4 edges = edgeDetector.getEdges(sigma, thresholdFactor) keepInsideStage = PipelineStage.KeepInsideRadiusStage(radius) edges = keepInsideStage.execute(edges) print("Got", edges.size(), "edge points at sigma", sigma, "and thresholdFactor", thresholdFactor) addEdgesToImage(img, edges, 0) addEdgesToImage(img, edges, 2) sigma = 8 thresholdFactor = 0.1
def test_EmptyImage(self): emptyImage = np.zeros((0, 0)) e = EdgeDetector(emptyImage) noPointsCloud = e.getEdges(2.0, 6.0) self.assertEqual(noPointsCloud.size(), 0)
def test_SimpleCreate(self): randomImage = np.random.rand(2, 2) EdgeDetector(randomImage)