-
-
Save Benehiko/103a0f839a0bc0c78c172b36911e864f to your computer and use it in GitHub Desktop.
Avoid bring images and over-exposed images. Corrects them and finds proper shapes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
from matplotlib import pyplot as plt | |
def morph_close(img): | |
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) | |
o = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) | |
c = cv2.Canny(o,100,150) | |
#div = np.float32(img)/o | |
res = np.uint8(cv2.normalize(c, c, 0, 255,cv2.NORM_MINMAX)) | |
return c | |
img_col = cv2.imread('bright_numb.jpg') | |
img = cv2.cvtColor(img_col, cv2.COLOR_RGB2GRAY) | |
flag = True | |
if flag: | |
equ = cv2.equalizeHist(img) | |
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8)) | |
cl1 = clahe.apply(img) | |
kernel = np.ones((5,5),np.uint8) | |
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel) | |
#erosion = cv2.erode(img,kernel,iterations = 1) | |
c = cv2.Canny(cl1,100,200) | |
t = cv2.adaptiveThreshold(c,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2) | |
else: | |
t = morph_close(img) | |
#t = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2) | |
image, contours, hierarchy = cv2.findContours(t, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) | |
for cnt in contours: | |
epsilon = 0.01*cv2.arcLength(cnt, False) | |
approx = cv2.approxPolyDP(cnt, epsilon, False) | |
area = cv2.contourArea(approx) | |
rect = cv2.minAreaRect(approx) | |
box = cv2.boxPoints(rect) | |
box = np.int0(box) | |
percentage = (area * 100) / (img.shape[0] * img.shape[1]) | |
if percentage > 0.2 and percentage < 20: | |
cv2.drawContours(img_col,[box],0,(0,255,0),20) | |
cv2.imwrite('results/morph.jpg',t) | |
cv2.imwrite('results/shaped.jpg',img_col) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment