def thresh_binary(img): gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) blur = cv.GaussianBlur(gray, (9, 9), 0) # OTSU's binaryzation (ret3, th3) = cv.threshold(blur, 50, 255, cv.THRESH_OTSU) kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5)) opening = cv.morphologyEx(th3, cv.MORPH_OPEN, kernel) #看看有几个非零像素 # cv.namedWindow("Image",0) # cv.imshow("Image", opening) return opening
def image_processing(imageA, img0, lang, csv_file): count = 0 img = imageA.copy() # prepare image quality for OCR img = cv2.bitwise_not(img) _, img_recog = cv2.threshold(img, 210, 255, cv2.THRESH_BINARY) _, img = cv2.threshold(img, 224, 255, cv2.THRESH_BINARY) #find text areas imgBi = cv2.bitwise_not(imageA) _, binary2 = cv2.threshold(imgBi, 0, 255, cv2.THRESH_BINARY) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 20)) eroded = cv2.erode(binary2, kernel, iterations=1) erodedBi = cv2.bitwise_not(eroded) contours2, hierarchy2 = cv2.findContours(erodedBi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # find head area for OCR text with color headArea = img_recog[104:204, 319:1493] erodedHead = cv2.erode(headArea, kernel, iterations=1) erodedHead = cv2.bitwise_not(erodedHead) contours, hierarchy = cv2.findContours(erodedHead, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for i in range(len(contours)): x, y, w, h = cv2.boundingRect(contours[i]) if w < 1000: count += 1 cv2.rectangle(img0, (x + 319, y + 104), (x + 319 + w, y + 104 + h), (0, 255, 0), 2) crop_img = headArea[y:y + h, x:x + w] cv2.imwrite('ref.png', crop_img) text = tesserocr.image_to_text(Image.open('ref.png'), lang) text = text.replace(" ", "") text = text.replace("\n", " ") csv_file.write('{}:,{},{},{},{},{}\n'.format( count, x, y, w, h, text.encode('utf-8'))) for j in range(len(contours2)): cnt2 = contours2[j] x2, y2, w2, h2 = cv2.boundingRect(cnt2) if x2 > 120 and y2 > 200 and 2 < w2 and 2 < h2 < 450: count += 1 cv2.rectangle(img0, (x2, y2), (x2 + w2, y2 + h2), (0, 255, 0), 2) crop_img = img_recog[y2:y2 + h2, x2:x2 + w2] cv2.imwrite('ref.png', crop_img) text = tesserocr.image_to_text(Image.open('ref.png'), lang) text = text.replace(" ", "") text = text.replace("\n", " ") csv_file.write('{}:,{},{},{},{},{}\n'.format( count, x2, y2, w2, h2, text.encode('utf-8'))) else: pass
def Detection(image,parameters_dict): image=cv2.resize(image,(640,480)) #cv2.imshow("normal",image) ogimg=image#store the image given as a parameter for later bitwise and operation image=cv2.cvtColor(image, cv2.COLOR_BGR2HSV) image=cv2.GaussianBlur(image, (17, 17), 2) lower=np.array([parameters_dict["hue"][0],parameters_dict["sat"][0],parameters_dict["value"][0]]) higher=np.array([parameters_dict["hue"][1],parameters_dict["sat"][1],parameters_dict["value"][1]]) mask=cv2.inRange(image,lower,higher) if parameters_dict["OR_MASK"]==True: lower_oran=np.array([175,100,100],dtype="uint8") higher_oran=np.array([179,255,255],dtype="uint8") mask1=cv2.inRange(image,lower_oran,higher_oran) mask=cv2.bitwise_or(mask,mask1) if parameters_dict["Kernel"]==True: Kernel=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) else: Kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) Thresholded_img=cv2.bitwise_and(ogimg,ogimg,mask=mask) filtered_img=cv2.morphologyEx(Thresholded_img,cv2.MORPH_OPEN,Kernel) return filtered_img
def eroded(src, size=(3, 3)): # image = cv2.resize(image_src,(600,400)) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, ksize=size) # dilated = cv2.dilate(image,kernel) # cv2.imshow('dilated',dilated) erode = cv2.erode(src, kernel) # thresholded = cv2.adaptiveThreshold(erode,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,7,2) # ret,thresholded = cv2.threshold(erode,150,255,cv2.THRESH_BINARY) # cv2.imshow('erode',erode) # cv2.imshow('src',src) # cv2.waitKey(0) return erode
def removeLines(path, imgPath): img = cv2.imread(imgPath, 0) img = 255 - img _, thres = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) cv2.imwrite(path + "removed//thres.jpg", thres) horizontal = thres vertical = thres cols = horizontal.shape[1] h_kernel = cols // 30 horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (h_kernel,1)) horizontal = cv2.erode(horizontal, horizontalStructure) horizontal = cv2.dilate(horizontal, horizontalStructure) cv2.imwrite(path + "removed//horizontal.jpg", horizontal) rows = vertical.shape[0] v_kernel = rows // 30 verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, v_kernel)) vertical = cv2.erode(vertical, verticalStructure) vertical = cv2.dilate(vertical, verticalStructure) cv2.imwrite(path + "removed//vertical.jpg", vertical) _, edges = cv2.threshold(vertical,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) cv2.imwrite(path + "removed//edges.jpg", edges) kernel = np.ones((2, 2), dtype = "uint8") dilated = cv2.dilate(edges, kernel) cv2.imwrite(path + "removed//dilated.jpg", dilated) mask = cv2.bitwise_not(vertical+horizontal) kernel = np.ones((2,2), np.uint8) mask = cv2.erode(mask, kernel, iterations=3) cv2.imwrite(path + "removed//invh.jpg", mask) masked_img = cv2.bitwise_and(img, img, mask=mask) masked_img_inv = cv2.bitwise_not(masked_img) cv2.imwrite(path + "removed_result.jpg", masked_img_inv) return masked_img_inv
def maximizeContrast(imgGrayscale): height, width = imgGrayscale.shape imgTopHat = np.zeros((height, width, 1), np.uint8) imgBlackHat = np.zeros((height, width, 1), np.uint8) structuringElement = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) imgTopHat = cv2.morphologyEx(imgGrayscale, cv2.MORPH_TOPHAT, structuringElement) imgBlackHat = cv2.morphologyEx(imgGrayscale, cv2.MORPH_BLACKHAT, structuringElement) imgGrayscalePlusTopHat = cv2.add(imgGrayscale, imgTopHat) imgGrayscalePlusTopHatMinusBlackHat = cv2.subtract(imgGrayscalePlusTopHat, imgBlackHat) return imgGrayscalePlusTopHatMinusBlackHat
def hist_extract(image, handHist): hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([hsv], [0, 1], handHist, [0, 180, 0, 256], 1) disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (21, 21)) cv2.filter2D(dst, -1, disc, dst) # dst is now a probability map # Use binary thresholding to create a map of 0s and 1s # 1 means the pixel is part of the hand and 0 means not ret, thresh = cv2.threshold(dst, 150, 255, cv2.THRESH_BINARY) kernel = np.ones((5, 5), np.uint8) thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=8) #thresh = cv2.merge((thresh, thresh, thresh)) return thresh
def recognize_text(): # # 灰度图片 # gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) # # 二值化 # binary = cv2.adaptiveThreshold( # ~gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 35, -5) # 灰度值 gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) # 自动阈值二值化 ret, binary = cv2.threshold( gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) cv2.imshow("threshold", binary) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 4)) binl = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 1)) open_out = cv2.morphologyEx(binl, cv2.MORPH_OPEN, kernel) # cv2.bitwise_not(open_out, open_out) # cv2.imshow("dstImage", open_out) textImage = Image.fromarray(binary) text = pytesseract.image_to_string(textImage) print("Result:%s" % text)
def removeLines(): gray = cv2.imread(imgPath, 0) inverted = cv2.bitwise_not(gray) _, thres = cv2.threshold(inverted, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) cv2.imwrite(path + "removeLines//thres.jpg", thres) horizontal = thres vertical = thres cols = horizontal.shape[1] h_kernel = cols // 30 horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (h_kernel, 1)) horizontal = cv2.erode(horizontal, horizontalStructure) horizontal = cv2.dilate(horizontal, horizontalStructure) cv2.imwrite(path + "removeLines//horizontal.jpg", horizontal) rows = vertical.shape[0] v_kernel = rows // 30 verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, v_kernel)) vertical = cv2.erode(vertical, verticalStructure) vertical = cv2.dilate(vertical, verticalStructure) kernel = np.ones((2, 2), dtype="uint8") dilated = cv2.dilate(vertical, kernel) cv2.imwrite(path + "removeLines//vertical.jpg", dilated) mask = cv2.bitwise_not(vertical + horizontal) kernel = np.ones((2, 2), np.uint8) mask = cv2.erode(mask, kernel, iterations=3) cv2.imwrite(path + "removeLines//frame.jpg", mask) masked_img = cv2.bitwise_and(inverted, inverted, mask=mask) removed_result = cv2.bitwise_not(masked_img) cv2.imwrite(path + "removeLines//removed_result.jpg", removed_result) return removed_result
def harris_corner_detect(image): gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) gray = cv.GaussianBlur(gray, (9, 9), 25) # sigma # cv.imshow('GS gray', gray) # dst = cv.cornerHarris(gray, 2, 3, 0.04) dst = cv.cornerHarris(gray, 9, 19, 0.06) # 膨胀腐蚀-开操作,去除小的干扰角点 kernel = cv.getStructuringElement(cv.MORPH_RECT, (9, 9)) dst = cv.morphologyEx(dst, cv.MORPH_OPEN, kernel) # 角点绘制 # image[dst > 0.01 * dst.max()] = [0, 0, 255] image[dst > 0.01 * dst.max()] = [0, 0, 255] cv.imshow('image for harris', image)
def createMask2(image): pic = image gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) edged = cv2.Canny(gray, 50, 100) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 9)) closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel) (_, cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) mask = np.ones(image.shape[:2], dtype="uint8") * 255 for c in cnts: cv2.drawContours(mask, [c], -1, 0, -1) mask_inv = cv2.bitwise_not(mask) image = cv2.bitwise_and(image, image, mask=mask) image2 = cv2.bitwise_and(pic, pic, mask=mask_inv) return whiteBackground(image2)
def edit_img(kernel_length, v_weith, v_length, h_weith, h_length, v_pixel, h_pixel, img_bin): # 선을 찾아낼 기준 조건들을 설정한다. #kernel_length = np.array(img_bin).shape[1] // 40 # 선 길이를 뜻 #kernel_length = 20 # 선 길이 20 이상만 선으로 인식한다. #print(len(cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length)))) #print(len(cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1)))) #print('dddd', kernel_length) verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length)) # 세로 선 두께 hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1)) # 가로 선 두께 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (v_pixel, h_pixel)) # 선 그릴때 픽셀 크기를 지정(세로, 가로) # 세로선을 찾는다. img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=v_weith) # 가로 인식할 선의 굵기 조정 verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=v_length) # 가로 인식된 선의 길이를 조정 #view_img(verticle_lines_img) # 가로선을 찾는다. img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=h_weith) # 세로 인식할 선의 굵기 조정 horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=h_length) # 세로 인식된 선의 길이를 조정 #view_img(horizontal_lines_img) # 찾은 선을 새 이미지에 그린다. alpha = 0.5 beta = 1.0 - alpha # This function helps to add two image with specific weight parameter to get a third image as summation of two image. img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0) #img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 3.0) img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=1) (thresh, img_final_bin) = cv2.threshold(img_final_bin, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) #img_final_bin111 = cv2.pyrDown(img_final_bin) #img_final_bin111 = cv2.pyrDown(img_final_bin111) #cv2.imshow('ori_img11', img_final_bin111) #cv2.waitKey(0) return img_final_bin
def erosion(val): erosion_size = cv2.getTrackbarPos(title_trackbar_kernel_size, title_erosion_window) erosion_type = 0 val_type = cv2.getTrackbarPos(title_trackbar_element_type, title_erosion_window) if val_type == 0: erosion_type = cv2.MORPH_RECT elif val_type == 1: erosion_type = cv2.MORPH_CROSS elif val_type == 2: erosion_type = cv2.MORPH_ELLIPSE element = cv2.getStructuringElement( erosion_type, (2 * erosion_size + 1, 2 * erosion_size + 1), (erosion_size, erosion_size)) erosion_dst = cv2.erode(src, element, iterations=iterations) cv2.imshow(title_erosion_window, erosion_dst)
def closing(img_path): """ :param img: the path to an image :type img: str """ # load in the image image = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) closing = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) output = np.hstack((image, closing)) cv2.imshow('closing', output) cv2.waitKey(0)
def morphological_gradient(img_path): """ :param img: the path to an image :type img: str """ # load in the image image = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) gradient = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel) output = np.hstack((image, gradient)) cv2.imshow('morphological gradient', output) cv2.waitKey(0)
def create_input(input): file = open("data/recognized.txt", "w+") file.write("") if '.mpeg' in input or '.wav' in input: r = sr.Recognizer() with sr.AudioFile(input) as source: audio = r.listen(source) text = r.recognize_google(audio) file.write(text) file.write('\n') file.close else: pytesseract.pytesseract.tesseract_cmd = 'C://Program Files//Tesseract-OCR//tesseract.exe' img = cv2.imread(input) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV) rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18)) dilation = cv2.dilate(thresh1, rect_kernel, iterations=1) contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) im2 = img.copy() for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) # Drawing a rectangle on copied image rect = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2) # Cropping the text block for giving input to OCR cropped = im2[y:y + h, x:x + w] # Open the file in append mode file = open("data/recognized.txt", "a") # Apply OCR on the cropped image text = pytesseract.image_to_string(cropped) # Appending the text into file file.write(text) file.write("\n") # Close the file file.close
def skeletize(img): size = np.size(img) skel = np.zeros(img.shape, np.uint8) element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3)) done = False while not done: eroded = cv2.erode(img, element) temp = cv2.dilate(eroded, element) temp = cv2.subtract(img, temp) skel = cv2.bitwise_or(skel, temp) img = eroded.copy() zeroes = size - cv2.countNonZero(img) if zeroes == size: done = True return skel
def text_recognition_from_text(image): rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Grayscale, Gaussian blur, Otsu's threshold gray_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray_image, (7, 9), 0) ret, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # Morph open to remove noise and invert image kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1) invert = 255 - opening data = pytesseract.image_to_string(invert, lang='eng', config='--psm 6') return invert, data
def dilatation(val): dilatation_size = cv2.getTrackbarPos(title_trackbar_kernel_size, title_dilatation_window) dilatation_type = 0 val_type = cv2.getTrackbarPos(title_trackbar_element_type, title_dilatation_window) if val_type == 0: dilatation_type = cv2.MORPH_RECT elif val_type == 1: dilatation_type = cv2.MORPH_CROSS elif val_type == 2: dilatation_type = cv2.MORPH_ELLIPSE element = cv2.getStructuringElement( dilatation_type, (2 * dilatation_size + 1, 2 * dilatation_size + 1), (dilatation_size, dilatation_size)) dilatation_dst = cv2.dilate(src, element, iterations=iterations) cv2.imshow(title_dilatation_window, dilatation_dst)
def __init__(self, path, viewer=None, green_screen=False, factor=0.84): self.path = path self.img = cv2.imread(self.path, cv2.IMREAD_COLOR) if green_screen: self.img = cv2.medianBlur(self.img, 5) divFactor = 1 / (self.img.shape[1] / 640) print(self.img.shape) print('Resizing with factor', divFactor) self.img = cv2.resize(self.img, (0, 0), fx=divFactor, fy=divFactor) cv2.imwrite("/tmp/resized.png", self.img) remove_background("/tmp/resized.png", factor=factor) self.img_bw = cv2.imread("/tmp/green_background_removed.png", cv2.IMREAD_GRAYSCALE) # rescale self.img and self.img_bw to 640 else: self.img_bw = cv2.imread(self.path, cv2.IMREAD_GRAYSCALE) self.viewer = viewer self.green_ = green_screen self.kernel_ = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
def GetCircle(img): #ClearBlackEdge - GaussianBlur - Binary - Erode -FindCountour time0_start = time.time() img = ClearBlackEdge(img) #去黑边 time0_end = time.time() t0 = time0_end - time0_start #print('black:',t0) time2_start = time.time() blur = cv2.GaussianBlur(img,(9,9),0) #高斯平滑 ret3, binary = cv2.threshold(blur,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) #双峰二值化,不过现在有一种情况有点问题,后续鲤鱼进一步改进 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (300, 300)) #定义一直贼大的矩形核 grad = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel) #用刚才那个贼大的核,做个闭操作 out = cv2.erode(grad, None, iterations=10) #膨胀10次 time2_end = time.time() t2 = time2_end-time2_start #print(t2) #以上骚操作,都是为了把圆盘变成个实心圆 #边缘检测得到一些外围的候选 time1_start = time.time() contours, hier = cv2.findContours(out, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) time1_end = time.time() t1 = time1_end-time1_start #print('edge:',t1) height, width = img.shape allCenter = [] allRadius = [] #这个循环是为了过滤掉一些不太外围候选,得到外围 for c in contours : (x, y), radius = cv2.minEnclosingCircle(c) if width/3 < x < width/3*2 and 0.1*width < radius < 0.8*width: allCenter.append((int(x), int(y))) allRadius.append(int(radius)+100) else: pass # for i in range(len(allCenter)): # img = cv2.circle(img, allCenter[i], allRadius[i], (0, 0, 255), 10) return img, height, width, allCenter, allRadius #返回值是去了黑边的大图及其长宽,外围中心与半径
def img_threadhold(frame): #轉灰階 gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #平滑化 gray_img = cv2.GaussianBlur(gray_img, (3, 3), 0) #依照區域亮度做二值化 gray_img = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, blockSize=321, C=14) #膨脹 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1)) gray_img = cv2.erode(gray_img, kernel) return gray_img
def dilate(img_path): """ :param img: the path to an image :type img: str """ # load in the image image = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) dilation = cv2.dilate(image, kernel, iterations=1) output = np.hstack((image, dilation)) cv2.imshow('dilated', output) cv2.waitKey(0) return dilation
def getShadowImage(points): if(sys.platform == 'win32'): Debug = True ''' 得到投影图 ''' origin = np.amin(points, 0) span = np.ptp(points, 0) height = np.int(span[0]) + 1 width = np.int(span[1]) + 1 shadow = np.zeros((height, width), np.uint8) relative_coor = points - origin relative_coor = relative_coor.astype(np.int) for p in relative_coor: shadow[p[0]][p[1]] = 255 if(Debug): cv2.imshow('origin_shadow', shadow) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) #shadow = cv2.morphologyEx(shadow, cv2.MORPH_CLOSE, kernel) shadow = cv2.dilate(shadow, kernel) if(Debug): cv2.imshow('shadow1', shadow) #shadow = cv2.medianBlur(shadow, 7) shadow = cv2.GaussianBlur(shadow, (3,3), 1) if(Debug): cv2.imshow('shadow2', shadow) if(cv2.__version__[0] == '4'): contours, _ = cv2.findContours(shadow, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) else: _, contours, _ = cv2.findContours(shadow, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for con in contours: if(cv2.contourArea(con) > 500): cv2.drawContours(shadow, [con], 0, 255, -1) else: cv2.drawContours(shadow, [con], 0, 0, -1) if(Debug): cv2.imshow('final_shadow', shadow) return(shadow)
def crop(filename): img = image.load_img(filename) img = np.array(img) img = img[:, :, ::-1].copy() ## (1) Convert to gray, and threshold gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) th, threshed = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV) ## (2) Morph-op to remove noise kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11)) morphed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, kernel) ## (3) Find the max-area contour cnts = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] cnt = sorted(cnts, key=cv2.contourArea)[-1] ## (4) Crop and save it x,y,w,h = cv2.boundingRect(cnt) dst = img[y:y+h, x:x+w] cv2.imwrite(filename, dst)
def classify(file_path): res_text=[0] res_img=[0] img = cv2.imread(file_path) img2 = cv2.GaussianBlur(img, (3,3), 0) img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) img2 = cv2.Sobel(img2,cv2.CV_8U,1,0,ksize=3) _,img2 = cv2.threshold(img2,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) element = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(17, 3)) morph_img_threshold = img2.copy() cv2.morphologyEx(src=img2, op=cv2.MORPH_CLOSE, kernel=element, dst=morph_img_threshold) num_contours, hierarchy= cv2.findContours(morph_img_threshold,mode=cv2.RETR_EXTERNAL,method=cv2.CHAIN_APPROX_NONE) cv2.drawContours(img2, num_contours, -1, (0,255,0), 1) for i,cnt in enumerate(num_contours): min_rect = cv2.minAreaRect(cnt) if ratio_and_rotation(min_rect): x,y,w,h = cv2.boundingRect(cnt) plate_img = img[y:y+h,x:x+w] print("Number identified number plate...") res_img[0]=plate_img cv2.imwrite("result.png",plate_img) if(isMaxWhite(plate_img)): clean_plate, rect = clean2_plate(plate_img) if rect: fg=0 x1,y1,w1,h1 = rect x,y,w,h = x+x1,y+y1,w1,h1 plate_im = Image.fromarray(clean_plate) text = tess.image_to_string(plate_im, lang='eng') res_text[0]=text if text: break label.configure(foreground='#011638', text=res_text[0]) uploaded=Image.open("result.png") im=ImageTk.PhotoImage(uploaded) plate_image.configure(image=im) plate_image.image=im plate_image.pack() plate_image.place(x=560,y=320)
def cleanImage(image, stage=0): V = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) # applying topHat/blackHat operations topHat = cv2.morphologyEx(V, cv2.MORPH_TOPHAT, kernel) blackHat = cv2.morphologyEx(V, cv2.MORPH_BLACKHAT, kernel) # add and subtract between morphological operations add = cv2.add(V, topHat) subtract = cv2.subtract(add, blackHat) if (stage == 1): return subtract T = threshold_local(subtract, 29, offset=35, method="gaussian", mode="mirror") thresh = (subtract > T).astype("uint8") * 255 if (stage == 2): return thresh # invert image thresh = cv2.bitwise_not(thresh) return thresh
def text_recognition_from_video(): cap = cv2.VideoCapture(0) while (True): # Capture frame-by-frame _, frame = cap.read() rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Grayscale, Gaussian blur, Otsu's threshold gray_frame = cv2.cvtColor(rgb_frame, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray_frame, (7, 9), 0) ret, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # Morph open to remove noise and invert image kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1) invert = 255 - opening data = pytesseract.image_to_string(invert, lang='eng', config='--psm 6') cv2.imshow('frame', frame) cv2.imshow('gray_frame', gray_frame) cv2.imshow('blur', blur) cv2.imshow('thresh', thresh) cv2.imshow('opening', opening) cv2.imshow('invert', invert) if data == 'CHANAKYA\nNEETI': return data if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() return
def thinning(img, img_domain, is_gt): img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) height, width = img.shape scale_percent_h = (64 / height) * 100 scale_percent_w = (64 / width) * 100 if not is_gt: cv2.imwrite("current_fake_result_%s.png" % img_domain, img) average = np.average(img) if not img_domain == 'A' or not is_gt: if average >= 255 / 2: ret, img = cv2.threshold(img, average - (0.5 * (255 - average)), 255, cv2.THRESH_BINARY) img = 255 - img else: ret, img = cv2.threshold(img, average + (0.5 * (255 - average)), 255, cv2.THRESH_BINARY) if not is_gt: cv2.imwrite("current_fake_result_1_%s_threshold.png" % img_domain, img) # if is_gt: # img = 255 - img if is_gt and img_domain == 'A': img = 255 - img percent_h = (256 / height) * 100 percent_w = (256 / width) * 100 img = cv2.resize(img, (256, 256), fx=percent_w, fy=percent_h) # kernel = np.ones(5, dtype='uint8') kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) # rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) # img = cv2.dilate(img, rect_kernel, iterations=2) img = cv2.erode(img, kernel, iterations=2) img = cv2.resize(img, (64, 64), fx=scale_percent_w, fy=scale_percent_h) if not is_gt: cv2.imwrite("current_fake_result_2_%s_resize.png" % img_domain, img) # img = 255 - img # img = cv2.ximgproc.thinning(img) return img
def extract_contours_from_image(image_path, write_path, hsv_lower, hsv_upper): image = cv2.imread(image_path) original = image.copy() hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) hsv_lower = np.array(hsv_lower) hsv_upper = np.array(hsv_upper) mask = cv2.inRange(hsv, hsv_lower, hsv_upper) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1) close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=1) cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if len(cnts) == 2 else cnts[1] offset = 20 global ROI_number global cnt_add for c in cnts: x, y, w, h = cv2.boundingRect(c) cv2.rectangle(image, (x - offset, y - offset), (x + w + offset, y + h + offset), (36, 255, 12), 2) ROI = original[y - offset:y + h + offset, x - offset:x + w + offset] try: cv2.imwrite(write_path + 'contour_{}.png'.format(ROI_number), ROI) except: ROI_number += 1 # print(ROI_number) continue ROI_number += 1 for i in range(len(cnts)): cnt = cnts[i] x, y, w, h = cv2.boundingRect(cnt) img_final_rect = cv2.rectangle(original, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(original, "Error", (int(x + w / 2), int(y + h / 2)), cv2.FONT_HERSHEY_SIMPLEX, 0.25, (0, 255, 0)) cv2.imwrite( path + "display_comb_cntrs/" + id + '/' + "contour_add_" + str(cnt_add) + ".png", original) cnt_add = cnt_add + 1