|
| 1 | +''' |
| 2 | +output : word detection on document (Simple OCR type of application) |
| 3 | +''' |
| 4 | + |
| 5 | +import cv2 |
| 6 | +import numpy as np |
| 7 | +import imutils |
| 8 | + |
| 9 | +# frame read |
| 10 | +frame = cv2.imread('test.jpeg') |
| 11 | + |
| 12 | +# resize |
| 13 | +frame = cv2.resize(frame , (600,600)) |
| 14 | + |
| 15 | +# grayscale |
| 16 | +gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) |
| 17 | + |
| 18 | +# remove noise |
| 19 | +blur = cv2.GaussianBlur(gray,(5,5),0) |
| 20 | + |
| 21 | +# otsu thresh (bimodel thresold) |
| 22 | +thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] |
| 23 | + |
| 24 | +# get structuring element |
| 25 | + |
| 26 | +horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,1)) |
| 27 | +vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,25)) |
| 28 | +print('horizontal kernel : {}'.format(horizontal_kernel)) |
| 29 | +print('vertical kernel : {}'.format(vertical_kernel)) |
| 30 | + |
| 31 | +# opening (erosion followed by dilation) |
| 32 | + |
| 33 | +horizontal_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2) |
| 34 | +vertical_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2) |
| 35 | + |
| 36 | +# contours apply on detected lines |
| 37 | +# First one is source image, second is contour retrieval mode, third is contour approximation method |
| 38 | + |
| 39 | +cnts = cv2.findContours(horizontal_lines ,cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_SIMPLE) |
| 40 | +cntsv = cv2.findContours(vertical_lines ,cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_SIMPLE) |
| 41 | + |
| 42 | +# find contours |
| 43 | +cnts = cnts[0] if len(cnts) == 2 else cnts[1] |
| 44 | +cntsv = cntsv[0] if len(cntsv) == 2 else cntsv[1] |
| 45 | + |
| 46 | +for c in cnts: |
| 47 | + cv2.drawContours(frame, [c], -1, (255,255,255), 2) |
| 48 | +for c in cntsv: |
| 49 | + cv2.drawContours(frame, [c], -1, (255,255,255), 2) |
| 50 | + |
| 51 | + |
| 52 | +# imshow |
| 53 | +cv2.imshow('thresh', thresh) |
| 54 | +cv2.imshow('horizontal_lines', horizontal_lines) |
| 55 | +cv2.imshow('vertical_lines', vertical_lines) |
| 56 | +cv2.imshow('frame', frame) |
| 57 | + |
| 58 | +# grayscale |
| 59 | + |
| 60 | +gray1 = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) |
| 61 | +thresh1 = cv2.adaptiveThreshold(gray1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 23, 30) |
| 62 | +canny = imutils.auto_canny(thresh1) |
| 63 | + |
| 64 | +output = cv2.bitwise_not(canny) |
| 65 | +kernel = np.ones((5,5),np.uint8) |
| 66 | + |
| 67 | +opening = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel) |
| 68 | + |
| 69 | +dilation = cv2.dilate(canny,kernel,iterations = 1) |
| 70 | + |
| 71 | + |
| 72 | +contour,hierachy=cv2.findContours(dilation,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) |
| 73 | + |
| 74 | +for i in contour: |
| 75 | + area= cv2.contourArea(i) |
| 76 | + if area>20: |
| 77 | + x,y,w,h = cv2.boundingRect(i) |
| 78 | + cv2.rectangle(frame,(x,y),(x+w,y+h),(0,120,255),2) |
| 79 | + |
| 80 | + |
| 81 | +cv2.imshow('output' ,output) |
| 82 | +cv2.imshow('dilate' ,dilation) |
| 83 | +cv2.imshow('opening' ,opening) |
| 84 | +cv2.imshow('original_frame' ,frame) |
| 85 | +cv2.imshow('canny' ,canny) |
| 86 | +cv2.imshow('thresh1' ,thresh1) |
| 87 | + |
| 88 | +# Saving output image |
| 89 | +cv2.imwrite('output.jpg', frame) |
| 90 | + |
| 91 | +# destroy all window |
| 92 | +cv2.waitKey(0) |
| 93 | +cv2.destroyAllWindows() |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
0 commit comments