def default_freset(op, noise, boxes, img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #debug cv2.imshow('ori', img) cv2.waitKey() pcs_obj = Preprocessing(img) if op: pcs_img = pcs_obj.natural_img_processing(noise) else: pcs_img = pcs_obj.digital_img_processing() #debug cv2.imshow('pre', pcs_img) cv2.waitKey() skew_obj = Deskew(pcs_img) skew_img = skew_obj.run() #debug cv2.imshow('skew', skew_img) cv2.waitKey() tdt_obj = TextDetection(skew_img, img) text_img = tdt_obj.detection() #debug flag = -1 for t_img, y, x in text_img: #cv2.imshow('', t_img) #cv2.waitKey() t_img = cv2.copyMakeBorder(t_img, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=[255,255,255]) if flag <= y+1 and flag >= y-1: tesseract_ocr(t_img, True) else: tesseract_ocr(t_img) if boxes: b_img = tesseract_boxes(t_img) h, w = t_img.shape[:2] gray[y*2:y*2+h, x*2:x*2+w] = b_img flag = y #debug if boxes: cv2.imshow('origin', tesseract_boxes(img)) cv2.imshow('result', gray) cv2.waitKey()
def do_skew_correction(self,widget): global imgloc global skewcorrected if imgloc == 0 : errorwindow = builder.get_object("error_message_box") errorwindow.set_transient_for(window) errorwindow.set_markup("<b>No Image loaded to the application</b>") errorwindow.format_secondary_markup("Add or Scan image to start skew correction") errorwindow.show() elif skewcorrected == 0 : loc = imgloc out=os.path.join(os.path.split(loc)[0],'skewcorrected'+os.path.splitext(os.path.split(loc)[1])[0]+os.path.splitext(os.path.split(loc)[1])[1]) d = Deskew( input_file=loc, display_image=None, output_file=out, r_angle=0) d.run() imgloc = out img = builder.get_object("previmage") img.set_from_file(imgloc) skewcorrected = 1 successwindow = builder.get_object("error_message_box") successwindow.set_transient_for(window) successwindow.set_markup("<b>Skewness of the Image Corrected Successfully</b>") successwindow.format_secondary_markup("You can now proceed Converting to text") successwindow.show() global zoomout zoomout =1 else: errorwindow = builder.get_object("error_message_box") errorwindow.set_transient_for(window) errorwindow.set_markup("<b>Image already Skew Corrected</b>") errorwindow.format_secondary_markup("Further correction may increase the skewness") errorwindow.show()
def process_image(img_path, output_path): img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) print('Skew correcting image: ', img_path) processed1 = process1(img) cv2.imwrite(os.path.join(output_path, 'processed1.png'), processed1) try: angle = compute_skew(img) print("deskew angle: {:.3f}".format(angle)) out = deskew_image(img, angle) cv2.imwrite(os.path.join(output_path, 'processed2.png'), out) except: print('Failed') d = Deskew(input_file=img_path, display_image=False, output_file=os.path.join(output_path, 'corrected.png'), r_angle=0) d.run()
def process_image(self): #convert to grayscale and find canny edges gray = cv2.imread(self.path) edges = cv2.Canny(gray, 100, 150, apertureSize=3) #get Hough Lines Probabilistic minLineLength = 100 lines = cv2.HoughLinesP(image=edges, rho=1, theta=np.pi / 180, threshold=120, lines=np.array([]), minLineLength=minLineLength, maxLineGap=100) if lines == []: return a, _, _ = lines.shape x_diff = 0 y_diff = 0 for i in range(a): x_diff += abs(lines[i][0][0] - lines[i][0][2]) y_diff += abs(lines[i][0][1] - lines[i][0][3]) x_diff = x_diff / len(lines) y_diff = y_diff / len(lines) print('x_diff: ', x_diff, 'y_diff: ', y_diff) if x_diff > y_diff: ratio = x_diff / y_diff print('Image is Horizontal, ratio is:', x_diff / y_diff) else: ratio = y_diff / x_diff print('Image is Vertical, ratio is:', y_diff / x_diff) return #self.rotate_image() if ratio < 7.0: print('we need to deskew this image') deskew_obj = Deskew(self.path) deskew_obj.deskew()
from time import sleep from cv2.cv2 import * import numpy as np from matplotlib import pyplot as plt from deskew import Deskew path = "C:\\Users\\OmarMarridi\\Desktop\\Semester\\Digtal Image Processing\\ImageProject\\Preprocessing DataSet\\" deskew_obj = Deskew(path+'Doc14.tif', 'Yes', 0) image=deskew_obj.run() image = threshold(image,130,255,THRESH_BINARY) imwrite(path+'testborder.tif',image[1])
from skew_detect import SkewDetect from deskew import Deskew from text_cleaner import clean_image img_path = './images/12.png' clean_image(img_path, './') img_path = 'cleaned.png' #sd = SkewDetect(input_file=img_path, output_file='./output.txt', display_output='Yes') #sd.run() d = Deskew(input_file=img_path, display_image=False, output_file='output_img.png', r_angle=0) d.run()
from detect_blur import * from deskew import Deskew path = "C:\\Users\\OmarMarridi\\Desktop\\Semester\\Digtal Image Processing\\ImageProject\\Preprocessing DataSet\\" files = [ "Doc07.tif", "Doc08.tif", "Doc09.tif", "Doc011.tif", "Doc11.tif", "Doc12.tif", "Doc13.tif", "Doc14.tif", "Doc15.tif", "Doc077.tif", "Fax001.tif", "Fax003.tif", "wordlist12.tif", "wordlist23.tif", "Document001.tif", "Doc066.tif" ] for i in range(0, files.__len__() - 1, 1): print("Processing Image \"" + files[i] + "\"") orig = imread(path + files[i], IMREAD_GRAYSCALE) img = orig deskew_obj = Deskew(img, 'Yes', 0) image = deskew_obj.run() _, image = threshold(image, 130, 255, THRESH_BINARY) _, thresh = threshold(image, 1, 255, THRESH_BINARY) contours = findContours(image, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE) cnt = contours[0] x, y, w, h = boundingRect(cnt) image = image[y:y + h, x:x + w] image = equalizeHist(image) #if(detectBlur(img, 100)): #print("Blurred Image") blur = medianBlur(image, 3) for k in range(1, 5, 1): blur = medianBlur(blur, 3)
flags=cv2.CASCADE_SCALE_IMAGE) # if a face find in this case we consider this face and image and continue our job if (len(faces) != 0): M = cv2.getRotationMatrix2D(center, 180, 1.0) docImage = cv2.warpAffine(docImage, M, (width, height)) print("Found {0} faces!".format(len(faces))) return docImage, faces ################################################################# ################################################################# inputAddress = 'patent3.jpg' outputAddress = 'patent3Ro.jpg' des = Deskew(inputAddress, False, outputAddress, 0) des.run() image = Image.open(outputAddress) box = getbox(image) result = image.crop(box) result.save('sampleCut.jpg') # Read the image image = cv2.imread('sampleCut.jpg') image, faces = detectFace(image) # we keep only the biggest face. because sometime some noise fool the algorithm faceArea = 0 faceX = 0 faceY = 0