|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +''' |
| 4 | +This example illustrates how to use cv.ximgproc.EdgeDrawing class. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + ed.py [<image_name>] |
| 8 | + image argument defaults to board.jpg |
| 9 | +''' |
| 10 | + |
| 11 | +# Python 2/3 compatibility |
| 12 | +from __future__ import print_function |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import cv2 as cv |
| 16 | +import random as rng |
| 17 | +import sys |
| 18 | + |
| 19 | +rng.seed(12345) |
| 20 | + |
| 21 | +def main(): |
| 22 | + try: |
| 23 | + fn = sys.argv[1] |
| 24 | + except IndexError: |
| 25 | + fn = 'board.jpg' |
| 26 | + |
| 27 | + src = cv.imread(cv.samples.findFile(fn)) |
| 28 | + gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) |
| 29 | + cv.imshow("source", src) |
| 30 | + |
| 31 | + ssrc = src.copy()*0 |
| 32 | + lsrc = src.copy() |
| 33 | + esrc = src.copy() |
| 34 | + |
| 35 | + ed = cv.ximgproc.createEdgeDrawing() |
| 36 | + |
| 37 | + # you can change parameters (refer the documentation to see all parameters) |
| 38 | + EDParams = cv.ximgproc_EdgeDrawing_Params() |
| 39 | + EDParams.MinPathLength = 50 # try changing this value between 5 to 1000 |
| 40 | + EDParams.PFmode = False # defaut value try to swich it to True |
| 41 | + EDParams.MinLineLength = 10 # try changing this value between 5 to 100 |
| 42 | + EDParams.NFAValidation = True # defaut value try to swich it to False |
| 43 | + |
| 44 | + ed.setParams(EDParams) |
| 45 | + |
| 46 | + # Detect edges |
| 47 | + # you should call this before detectLines() and detectEllipses() |
| 48 | + ed.detectEdges(gray) |
| 49 | + |
| 50 | + segments = ed.getSegments() |
| 51 | + lines = ed.detectLines() |
| 52 | + ellipses = ed.detectEllipses() |
| 53 | + |
| 54 | + #Draw detected edge segments |
| 55 | + for i in range(len(segments)): |
| 56 | + color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)) |
| 57 | + cv.polylines(ssrc, [segments[i]], False, color, 1, cv.LINE_8) |
| 58 | + |
| 59 | + cv.imshow("detected edge segments", ssrc) |
| 60 | + |
| 61 | + #Draw detected lines |
| 62 | + if lines is not None: # Check if the lines have been found and only then iterate over these and add them to the image |
| 63 | + lines = np.uint16(np.around(lines)) |
| 64 | + for i in range(len(lines)): |
| 65 | + cv.line(lsrc, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 1, cv.LINE_AA) |
| 66 | + |
| 67 | + cv.imshow("detected lines", lsrc) |
| 68 | + |
| 69 | + #Draw detected circles and ellipses |
| 70 | + if ellipses is not None: # Check if circles and ellipses have been found and only then iterate over these and add them to the image |
| 71 | + for i in range(len(ellipses)): |
| 72 | + center = (int(ellipses[i][0][0]), int(ellipses[i][0][1])) |
| 73 | + axes = (int(ellipses[i][0][2])+int(ellipses[i][0][3]),int(ellipses[i][0][2])+int(ellipses[i][0][4])) |
| 74 | + angle = ellipses[i][0][5] |
| 75 | + color = (0, 0, 255) |
| 76 | + if ellipses[i][0][2] == 0: |
| 77 | + color = (0, 255, 0) |
| 78 | + cv.ellipse(esrc, center, axes, angle,0, 360, color, 2, cv.LINE_AA) |
| 79 | + |
| 80 | + cv.imshow("detected circles and ellipses", esrc) |
| 81 | + cv.waitKey(0) |
| 82 | + print('Done') |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + print(__doc__) |
| 87 | + main() |
| 88 | + cv.destroyAllWindows() |
0 commit comments