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 _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 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()
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] points1[ix, 1] = point[1] print(points1.shape)
def execute(self, inData): print("Executing edge detector stage") edgeDetector = EdgeDetector(inData) self._executionResult = edgeDetector.getEdges(self._sigma, self._threshold) return self._executionResult
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 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)
def test_EmptyImage(self): emptyImage = np.zeros((0, 0)) e = EdgeDetector(emptyImage) noPointsCloud = e.getEdges(2.0, 6.0) self.assertEqual(noPointsCloud.size(), 0)