Пример #1
0
 def scan_image_different_threshs(self, cv_image):
     '''Scan image using multiple thresholds if first try fails. Return list of data found in image.'''
     scan_try = 0
     qr_data = []
     while True:
         if scan_try == 0:
             image_to_scan = cv_image # use original image
         elif scan_try == 1:
             cv_gray_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
             cv_thresh_image = cv2.adaptiveThreshold(cv_gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 101, 2)
             image_to_scan = cv2.cvtColor(cv_thresh_image, cv2.COLOR_GRAY2BGR)
         elif scan_try == 2:
             cv_gray_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
             cv_thresh_image = cv2.adaptiveThreshold(cv_gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 39, 2)
             image_to_scan = cv2.cvtColor(cv_thresh_image, cv2.COLOR_GRAY2BGR)
         elif scan_try == 3:
             cv_gray_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
             _, cv_thresh_image = cv2.threshold(cv_gray_image, 150, 255, 0)
             image_to_scan = cv2.cvtColor(cv_thresh_image, cv2.COLOR_GRAY2BGR)
         else:
             break # nothing else to try.
         
         qr_data = self.scan_image(image_to_scan)
         
         if len(qr_data) > 0:
             break # found code data in image so don't need to keep trying
         
         scan_try += 1
         
     # Notify if had to use a backup thresholding and had success.
     if scan_try > 0 and len(qr_data) > 0:
         print 'success on scan try {0}'.format(scan_try)
         
     return qr_data
Пример #2
0
def fneighbourdhood_area(x):
    global na, thres, windowTitle, tipo
    if x % 2 ==0:
        na = x+1
    else:
        na = x
    if na == 0 or na == 1:
        na = 3
    if tipo == 0:
        thres = img
    elif tipo == 1:
        thres = cv2.adaptiveThreshold(img, maxValue,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, na, cons)
    elif tipo == 2:
        thres = cv2.adaptiveThreshold(img, maxValue, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,na,cons)
    blobImg = Image(thres)
    invImg = blobImg.invert()
    blobImg = blobImg.rotate90()
    invImg = blobImg.invert()
    blobs = invImg.findBlobs()
    for blob in blobs:
        #print blob.coordinates()
        invImg.dl().circle(blob.coordinates(), 3, Color.RED, filled = True)
    blobImg.addDrawingLayer(invImg.dl())
    blobs.show(color=Color.GREEN,width=1)
    
    cv2.imshow(windowTitle, thres)
Пример #3
0
def find_boxes(im, size):
    # Does some preprocessing
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)    
    thresh = cv2.adaptiveThreshold(gray, 255, 1, 0, 127, 0)
    contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    thresh2 = cv2.adaptiveThreshold(gray, 255, 1, 1, 127, 0)
    contours2, _ = cv2.findContours(thresh2.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # Checks if image needs to be inverted
    if len(contours2) > len(contours):
        thresh = thresh2
        contours = contours2

    boxes = []
    splits = []
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        if w > size * 2 or h > size * 2:  # Too big
            continue

        if w >= size and h >= size: # Perfect size
            boxes.append((x, y, x + w, y + h))

        elif w >= size or h >= size: # half box
            # Tries to find other half
            i = find_near(splits, x + w / 2, y + h / 2, size)
            if i > -1: # If it finds it, connects them
                other = splits.pop(i)
                x1, y1, x2, y2 = join_rect((x, y, w, h), other)
                boxes.append((x1, y1, x2, y2))
            else: # Else, stores for later
                splits.append((x, y, w, h))
                
    # returns all the boxes and the processed image
    return boxes, thresh
Пример #4
0
def onBlockSize (pos):
   if (pos%2 != 0):
       thres = cv2.getTrackbarPos("threshold","image")
       if thres == 0:
           img2 = cv2.adaptiveThreshold(img,cv2.getTrackbarPos("maxValue","image"),cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,pos,cv2.getTrackbarPos("cte","image"))
       else:  img2 = cv2.adaptiveThreshold(img,cv2.getTrackbarPos("maxValue","image"),cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,pos,cv2.getTrackbarPos("cte","image"))
       cv2.imshow("image",img2)
Пример #5
0
def main():
    const = 2
    block_size = 51
    imagePath = "../data/sudoku.png"

    image = cv2.imread(imagePath, 0)

    cv2.imshow("Orignal Image", image)

    mean_thresh = \
        cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, block_size, const)
    gaussian_thresh = \
        cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, block_size, const)

    images = [image, mean_thresh, gaussian_thresh]
    titles = ["Orignals", "Mean", "Gaussian"]

    cv2.imshow("Mean Image", mean_thresh)
    cv2.imshow("Gaussian Image", gaussian_thresh)

    for i in range(3):
        plt.subplot(3, 1, i + 1)
        plt.imshow(images[i], cmap='gray')
        plt.title(titles[i])

    plt.show()
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Пример #6
0
    def get(self):
        r1 = cv2.getTrackbarPos('r1', self.windowname)
        r2 = cv2.getTrackbarPos('r2', self.windowname)
        r3 = cv2.getTrackbarPos('r3', self.windowname)
        l1 = cv2.getTrackbarPos('l1', self.windowname)
        l2 = cv2.getTrackbarPos('l2', self.windowname)
        l3 = cv2.getTrackbarPos('l3', self.windowname)


        # Remove light     
        h, s, v = cv2.split(self.orig_hsv)
        kernel = np.ones((9*2+1, 9*2+1), np.uint8)
        v_dilated = cv2.dilate(v, kernel, iterations = 1)
        v_out = cv2.subtract(v_dilated, v)

        #ret, v_t = cv2.threshold(v, l3, r3, cv2.THRESH_TRUNC)
        
        # Binarization
        #ret, ots = cv2.threshold(v_out, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

        #et, ots2 = cv2.threshold(v, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)


        #self.hist(v_out)
        #for i in xrange(l1):
        #    ret, mask = cv2.threshold(v_out, l2, 255, cv2.THRESH_TOZERO)
        #    v_out = cv2.bitwise_and(v_out, mask)
        #    v_out = cv2.add(v_out, (v_out/l3))

        v_out = cv2.bitwise_not(v_out)

        th3 = cv2.adaptiveThreshold(v, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,l1*2+1,l2)
        th4 = cv2.adaptiveThreshold(v_out, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,l1*2+1,l2)

        return [v_out, th3, th4]
Пример #7
0
    def find_yellow_contours(self, split_image):
        lab_bthreshed_board = cv2.adaptiveThreshold(split_image.lab[2], 255, 
            cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 
            self.options["board_blocksize"], self.options["board_C_b"])
        yuv_uthreshed_board = cv2.adaptiveThreshold(split_image.yuv[2], 255, 
            cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 
            self.options["board_blocksize"], self.options["board_C_u"])
        yuv_uthreshed_board = cv2.bitwise_not(yuv_uthreshed_board)
        finalThreshed = lab_bthreshed_board & yuv_uthreshed_board

        # Erode and dilate thresholded images
        morph_size = self.options["board_morph_size"]
        erode_element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (morph_size * 2 + 1, morph_size * 2 + 1),
                                                  (morph_size, morph_size))
        dilate_element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (morph_size * 2 + 1, morph_size * 2 + 1),
                                                   (morph_size, morph_size))
        eroded = cv2.erode(finalThreshed,erode_element, iterations = self.options["board_morph_iter"])
        finalThreshed = cv2.dilate(eroded, dilate_element, iterations = self.options["board_morph_iter"])
        finalThreshed = cv2.dilate(finalThreshed, dilate_element, iterations = self.options["board_morph_iter"])
        finalThreshed = cv2.erode(finalThreshed, erode_element, iterations = self.options["board_morph_iter"])

        self.post_if_enabled('yellow_lab_bthreshed', lab_bthreshed_board)
        self.post_if_enabled('yellow_yuv_uthreshed', yuv_uthreshed_board)
        self.post_if_enabled('yellow_binary_image', finalThreshed)

        _, contours, hierarchy = cv2.findContours(np.copy(finalThreshed), 
            cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        return contours, hierarchy
Пример #8
0
def adaptative_thresholding(x):
    global thres, na, cons, maxValue, tipo, img, windoTitle
    if x == 0:
        thres = img
        maxValue = 255
        na = 11
        cons = 2;
        cv2.createTrackbar('Neighbourhood area (odds)', windowTitle, na, maxValue, fneighbourdhood_area)
        cv2.createTrackbar('Constant', windowTitle, -maxValue, maxValue, fConstant)
        cv2.createTrackbar('MaxValue', windowTitle, maxValue, maxValue, fMaxValue)
    elif x == 1:
        thres = cv2.adaptiveThreshold(img, maxValue,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, na, cons)
    elif x == 2:
        thres = cv2.adaptiveThreshold(img, maxValue, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,na,cons)
    tipo = x
    blobImg = Image(thres)
    invImg = blobImg.invert()
    blobImg = blobImg.rotate90()
    invImg = blobImg.invert()
    blobs = invImg.findBlobs()
    for blob in blobs:
        #print blob.coordinates()
        invImg.dl().circle(blob.coordinates(), 3, Color.RED, filled = True)
    blobImg.addDrawingLayer(invImg.dl())
    blobs.show(color=Color.GREEN,width=1)
    cv2.imshow(windowTitle, thres)
Пример #9
0
def Hough_transform(img, imgc):


    th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
    th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)

    kernel = np.ones((3,3),np.uint8)

    edges = cv2.Canny(th2,50,150,apertureSize = 3)

    maxo = 0
    ie = 0
    aux = []

    for i in range (140, 250):
        lines = cv2.HoughLines(edges,1,np.pi/180,i)
        aux.extend(lines)


    for l in aux:
        for rho,theta in l:
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a*rho
            y0 = b*rho
            x1 = int(x0 + 1000*(-b))
            y1 = int(y0 + 1000*(a))
            x2 = int(x0 - 1000*(-b))
            y2 = int(y0 - 1000*(a))

            cv2.line(imgc,(x1,y1),(x2,y2),(0,0,0),1)

    cv2.imshow("Hough transform", imgc)
def testImgThreshod():
	#orig = cv2.imread('./images/lock.jpg',0)
	img = cv2.imread('./images/free.jpg',0) #comp.jpg eme.jpg building.jpg lock.jpg food.jpg
	#img = cv2.medianBlur(img,5)

	# ret,gth1 = cv2.threshold(img,5,255,cv2.THRESH_BINARY)
	# ret,gth2 = cv2.threshold(img,50,255,cv2.THRESH_BINARY)
	# ret,gth3 = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
	# ret,gth4 = cv2.threshold(img,150,255,cv2.THRESH_BINARY)
	# ret,gth5 = cv2.threshold(img,200,255,cv2.THRESH_BINARY)

	ret,gth1 = cv2.threshold(img,5,255,cv2.THRESH_TRUNC)
	ret,gth2 = cv2.threshold(img,5,255,cv2.THRESH_BINARY)
	ret,gth3 = cv2.threshold(img,100,255,cv2.THRESH_TRUNC)
	ret,gth4 = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
	ret,gth5 = cv2.threshold(img,200,255,cv2.THRESH_TRUNC)
	ret,gth6 = cv2.threshold(img,200,255,cv2.THRESH_BINARY)
	
	th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)

	th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)

	titles = ['Modified Image' , 'Global Thresholding (v = 5, trunc)',
	            'Global Thresholding (v = 5)', 'Global Thresholding (v = 100, trunc)', 'Global Thresholding (v = 100)', 'Global Thresholding (v = 200, trunc)',  'Global Thresholding (v = 200)', 'ADAPTIVE_THRESH_GAUSSIAN']
	images = [img, gth1, gth2, gth3, gth4, gth5, gth6, th3]

	for i in xrange(8):
	    plt.subplot(3,3,i+1),plt.imshow(images[i],'gray')
	    plt.title(titles[i])
	    plt.xticks([]),plt.yticks([])
	plt.show()
Пример #11
0
def convertData(filename, flag):
    if os.path.exists(filename) == False:
        raise ValueError
        return None

    with open(filename,'rb+') as handle:
        reader = csv.reader(handle)
        index = 0
        data = []
        labels = []
        for line in reader:
            index = index + 1
            if index == 1:
                continue
            if flag == "train":
                res = map(np.uint8,line[:28*28+1])
                labels.append(res[0])
                info_arr = np.array(res[1:]).reshape([28,28,1])
                info_img = cv.adaptiveThreshold(info_arr, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)
                data.append(255 - info_img.astype(np.float).reshape([28,28,1]))
                #cv.imwrite("train/S%05d_%d.jpg" % (index-1,res[0]),info_arr)
            if flag == "test":
                info_arr = np.array( map( np.uint8, line[:28*28] ) ).reshape([28,28,1])
                info_img = cv.adaptiveThreshold(info_arr, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)
                data.append(255 - info_img.astype(np.float).reshape([28,28,1]))
                #cv.imwrite("test/S%05d.jpg" % (index-1),info_arr)
        if flag == "train":
            labels_handle = open("labels.pkl","wb")
            pickle.dump(np.array(labels),labels_handle)
            labels_handle.close()

        saveHandle = open(filename.split(".")[0] + ".pkl",'wb')
        pickle.dump(np.array(data),saveHandle)
        saveHandle.close()
Пример #12
0
def get_threshold(init_img):
    gray = cv2.cvtColor(init_img, cv2.COLOR_BGR2GRAY)
    img = cv2.medianBlur(gray, 5)

    ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
    th3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
Пример #13
0
    def process(self, annotator, iohelper):
        img = cv2.imread(iohelper.thumbnail())
        image = img.copy()

        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        gray_blur = cv2.GaussianBlur(gray, (15, 15), 0)
        thresh = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 1)

        kernel = np.ones((3, 3), np.uint8)
        closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=3)

        methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
            'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']

        template = cv2.imread(iohelper.template(), 0)
        template_gray_blur = cv2.GaussianBlur(template, (15, 15), 0)
        template_thresh = cv2.adaptiveThreshold(template_gray_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 1)

        w, h = template.shape[::-1]

        res = cv2.matchTemplate(closing, template_thresh, eval(methods[1]))

        threshold = 0.3
        loc = np.where( res >= threshold)

        for pt in zip(*loc[::-1]):
            annotator.add_bug(pt[0], pt[1], w, h)
            cv2.rectangle(image, pt, (pt[0] + w, pt[1] + h), (0,0,255), 1)


        # cv2.imshow('Image', closing)
        # cv2.waitKey()

        return image
Пример #14
0
def get_contours(image, total, question):
    #Otsu's thresholding
    #cv2.threshold(image,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU,image)
    w, h = image.shape[::-1]
    block_size = w
    if block_size%2==0: block_size+=1
    cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,block_size,doc_parameters["adaptative_threshold_size"],image)

    contours, hierarchy = cv2.findContours(image.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    contours.reverse()
    if cv2.__version__=="2.4.3": #a bug in opencv 2.4.3, the fix is to add .astype("int") in the contour element
      contours = [get_contour_data(c.astype('int'), image) for c in contours]
    else:
      contours = [get_contour_data(c, image) for c in contours]
    contours = [c for c in contours if not c["empty"]]
    for i in range(len(contours)): contours[i]["index"]=i

    if len(contours) < total:
        return False,[]

    #if there are more contours than expected try merge them
    if len(contours)>total:
        contours = merge_contours_kmeans(contours, total, image)

    if len(contours)>1:
        if not same_size(contours, image):
            report.errors.append(QuestionError(question,"The circles don't have the same size or are too big"))
        else:
            mean_rad = 0
            for c in contours:
                mean_rad+=c["radius"]
            mean_rad=mean_rad/float(len(contours))
            for c in contours:
                c["radius"] = mean_rad
                recalculate_intensity(c,image)

    if len(contours)!= total:
        report.errors.append(QuestionError(question,"The number of circles do not match"))
    if not doc_parameters["squares"] and not are_circular(contours):
        report.errors.append(QuestionError(question,"All the circles don't have the correct shape"))

    if len(contours)>1:
        if not same_distance(contours,doc_parameters["distance_threshold"]):
            report.errors.append(QuestionError(question,"All the circles are not within the same distance"))

        if not are_aligned(contours, doc_parameters["circle_aligment_percent"]):
            report.errors.append(QuestionError(question,"All the boxes are not aligned"))

    if doc_parameters["debug"]:
        debug_contour_detection(contours, image)

    if doc_parameters["debug"]:
        errors = [e for e in report.errors if e.question == question]
        if len(errors)==0: print "-----OK-----"
        else:
            for e in errors:
                print e.message
            print "------------"
        cv2.waitKey()
Пример #15
0
def threshold(img, method = 'binary', thres = 127., max_value = 255.):
    if method == 'binary':
        ret,th = cv.threshold(img, thres, max_value, cv.THRESH_BINARY)
    elif method == 'adaptative_mean':
        th = cv.adaptiveThreshold(img, max_value, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 2)
    else:
        th = cv.adaptiveThreshold(img, max_value,cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY,11,2)
    return th
Пример #16
0
def binarize_cv(image_gray):
    BLOCK_SIZE = 29
    C = 20
    image_gray = cv2.dilate(image_gray, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)))
    image_bin_mean = cv2.adaptiveThreshold(image_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, BLOCK_SIZE, C)
    image_bin_gaussian = cv2.adaptiveThreshold(image_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, BLOCK_SIZE, C)
    cv2.imwrite('bin_mean.jpg', image_bin_mean)
    cv2.imwrite('bin_gaussian.jpg', image_bin_gaussian)
Пример #17
0
def get_text_roi(frame, show_window=True):
    kernel_sharpen = np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]])
    kernel_sharpen_3 = np.array([[-1,-1,-1,-1,-1],
                             [-1,2,2,2,-1],
                             [-1,2,8,2,-1],
                             [-1,2,2,2,-1],
                             [-1,-1,-1,-1,-1]]) / 8.0

    chars = []
    bound = 5
    kernel = np.ones((2,2),np.uint8)

    frame_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    frame_gray = cv2.filter2D(frame_gray.copy(),-1,kernel_sharpen_3)
    # frame_gray = cv2.GaussianBlur(frame_gray, (5,5),0) # Gaussian blur to remove noise

    # Adaptive threshold to find numbers on paper
    thresh = cv2.adaptiveThreshold(frame_gray,255,
        cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,255,7)
    thresh = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel,iterations=2)
    # cv2.imshow('thresh',thresh)

    contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,
                                            cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(frame,contours,-1,(255,0,0),2)
    # Build the list of number contours and number locations

    if len(contours) < 35:
        for ind,contour in enumerate(contours):
            [x,y,w,h] = cv2.boundingRect(contour)
            x_bound = w * .1;
            y_bound = h * .1;
            if  bound < x < (frame_gray.shape[1] - bound) and bound < y < (frame_gray.shape[0] - bound) and (x+w) <= (frame_gray.shape[1] - bound) and (y+h) <= (frame_gray.shape[0] - bound):
                roi = frame_gray[y-bound:y+h+bound,x-bound:x+w+bound]

                if len(roi) > 0: # Gets rid of weird zero-size contours
                    new_roi = cv2.adaptiveThreshold(roi,255,
                        cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,35,7)
                    new_roi = cv2.morphologyEx(new_roi,cv2.MORPH_OPEN,kernel,iterations=2) # Embiggen numbers
                    new_roi = cv2.resize(new_roi, (20,20), interpolation=cv2.INTER_AREA) # standardize contours
                    deskewed = deskew(new_roi)
                    # Record contour and contour location, and filter out internal contours
                    if hierarchy[0][ind][3] == -1:
                        cont = Character(deskewed,(x,y,w,h), hog(deskewed))
                        chars.append(cont)

    if show_window:
        # Build an image to show all number contours
        num_len = len(chars)
        # print '# of contours: ', num_len
        if num_len < 35 and num_len > 0:
            new_img = np.ones((20,20*num_len),np.uint8)
            y = 0
            for x in chars:
                new_img[:,y:y+20] = x.img
                y += 20
            cv2.imshow('image3',new_img)
    return chars
Пример #18
0
def onThreshold (pos):
    size = cv2.getTrackbarPos("blockSize","image")
    if size%2 == 0:
        size = size+1

    if pos == 0:
        img2 = cv2.adaptiveThreshold(img,cv2.getTrackbarPos("maxValue","image"),cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,size,cv2.getTrackbarPos("cte","image"))
    else: img2 = cv2.adaptiveThreshold(img,cv2.getTrackbarPos("maxValue","image"),cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,size,cv2.getTrackbarPos("cte","image"))
    cv2.imshow("image",img2)
 def findOpening(self, image, slowmode=False, MAX_SIZE=74, MIN_SIZE=63, startp1=119, startp2=142, startp3=2.7, imgshow=0):
     result = []
     detect = 0
     if slowmode == False:
         image = cv2.resize(image, (1280, 960))
         output = image.copy()
         gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
         thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
         while detect == 0:      # decrease sensitivity until at least one circle is found
             circles = cv2.HoughCircles(thresh,cv2.cv.CV_HOUGH_GRADIENT,startp3,50, param1=startp1,param2=startp2,minRadius=MIN_SIZE,maxRadius=MAX_SIZE)
              ## change param 1 and 2 for more-less circles
             if  circles is not None:
                 # convert the (x, y) coordinates and radius of the circles to integers
                 circles = np.round(circles[0, :]).astype("int")
                 # loop over the (x, y) coordinates and radius of the circles
                 for i in range(0,len(circles)):
                     # draw the circle in the output image, then draw a rectangle
                     # corresponding to the center of the circle
                     cv2.circle(output, (circles[i,0], circles[i,1]), circles[i,2], (0, 255, 0), 4)
                     cv2.rectangle(output, (circles[i,0] - 5, circles[i,1] - 5), (circles[i,0] + 5, circles[i,1] + 5), (0, 128, 255), -1)
                 if len(circles) == 1:
                     detect = 1
                 elif startp2 > 100 and len(circles) > 1:     #probably not starting to find only one circle
                     startp2 = startp2 - 3
                 elif startp2 <= 100 or len(circles) > 1:
                     detect = 1      # to get out if too many circles get found repeatedly -- unlikely to result in wrong angle as it needs validation
             else:
                 startp2 = startp2 - 3       # get less sensitive if no circles were found
         if imgshow == 1:
             cv2.imshow("thresh", thresh)
             cv2.imshow("output", output)
             cv2.waitKey(0)
     elif slowmode == True:      # Improves findOpening on the off-chance that something wrong in the image processing
         oldcircles = np.zeros((1,3), dtype=np.int)
         detect = 0
         while detect == 0:      # decrease sensitivity until at least one circle is found
             image3 = cv2.resize(image, (1280, 960))
             output = image3.copy()
             gray = cv2.cvtColor(image3, cv2.COLOR_BGR2GRAY)
             thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
             circles = cv2.HoughCircles(thresh,cv2.cv.CV_HOUGH_GRADIENT,startp3,50, param1=startp1,param2=startp2,minRadius=MIN_SIZE,maxRadius=MAX_SIZE)
             if circles is not None:
                 circles = np.round(circles[0, :]).astype("int")
                 detect = 1
                 # show the output image
                 for i in range(0,len(circles)):
                  # draw the circle in the output image, then draw a rectangle
                  # corresponding to the center of the circle
                     if imgshow == 1:
                         cv2.circle(output, (circles[i,0], circles[i,1]), circles[i,2], (0, 255, 0), 4)
                         cv2.rectangle(output, (circles[i,0] - 5, circles[i,1] - 5), (circles[i,0] + 5, circles[i,1] + 5), (0, 128, 255), -1)
                         cv2.imshow("thresh", thresh)
                         cv2.imshow("output", output)
                         cv2.waitKey(0)
             else:
                 startp2 = startp2 - 3       # get less sensitive if no circles were found
     return circles
Пример #20
0
def threshold_image(im):
    # There are many options here.
    ret,th1 = cv2.threshold(im,2,255,cv2.THRESH_BINARY)
    th2 = cv2.adaptiveThreshold(im,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
                cv2.THRESH_BINARY,11,2)
    th3 = cv2.adaptiveThreshold(im,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
                cv2.THRESH_BINARY,5,2)
    #ret,thresh = cv2.threshold(im,127,255,0)
    return th1
Пример #21
0
def fMaxValue(x):
    global maxValue, windowTitle, thres, img, na, cons
    maxValue = x
    if tipo == 0:
        thres = img
    elif tipo == 1:
        thres = cv2.adaptiveThreshold(img, maxValue,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, na, cons)
    elif tipo == 2:
        thres = cv2.adaptiveThreshold(img, maxValue, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,na,cons)
    cv2.imshow(windowTitle, thres)
Пример #22
0
def fMaxValue(x):
    global maxValueAdaptative, windowTitle, thresAdaptative, img, naAdaptative, consAdaptative, filtro
    maxValueAdaptative = x
    if tipoAdaptative == 0:
        thresAdaptative = img
    elif tipoAdaptative == 1:
        thresAdaptative = cv2.adaptiveThreshold(filtro, maxValueAdaptative,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, naAdaptative, consAdaptative)
    elif tipoAdaptative == 2:
        thresAdaptative = cv2.adaptiveThreshold(filtro, maxValueAdaptative, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,naAdaptative,consAdaptative)
    cv2.imshow(windowTitle, thresAdaptative)
Пример #23
0
def fConstant(x):
    global cons, thres, windowTitle, tipo, maxValue, na, img
    # const positive to white, otherwise, to black
    cons = x
    if tipo == 0:
        thres = img
    elif tipo == 1:
        thres = cv2.adaptiveThreshold(img, maxValue,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, na, cons)
    elif tipo == 2:
        thres = cv2.adaptiveThreshold(img, maxValue, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,na,cons)
    cv2.imshow(windowTitle, thres)
def Split_Blur_Thresh(frame):
	
	b,g,r = cv2.split(frame)
	b = cv2.medianBlur(b,7)
	g = cv2.medianBlur(g,7)
	r = cv2.medianBlur(r,7)
	b = cv2.adaptiveThreshold(b,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
	g = cv2.adaptiveThreshold(g,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
	r = cv2.adaptiveThreshold(r,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
	
	return cv2.merge((b,g,r))
Пример #25
0
def fConstant(x):
    global consAdaptative, thresAdaptative, windowTitle, tipoAdaptative, maxValueAdaptative, naAdaptative, img, filtro
    # consAdaptativet positive to white, otherwise, to black
    consAdaptative = x
    if tipoAdaptative == 0:
        thresAdaptative = img
    elif tipoAdaptative == 1:
        thresAdaptative = cv2.adaptiveThreshold(filtro, maxValueAdaptative,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, naAdaptative, consAdaptative)
    elif tipoAdaptative == 2:
        thresAdaptative = cv2.adaptiveThreshold(filtro, maxValueAdaptative, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,naAdaptative,consAdaptative)
    cv2.imshow(windowTitle, thresAdaptative)
Пример #26
0
def getrack(im):
    ##############TEMPORARY
    #globalim = bridge.imgmsg_to_cv2(data, "bgr8")
    #cv2.imshow("R", im)
    #cv2.waitKey(0)
    ################
    

    hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

    lower_blue = np.array([0/2,(0)*2.55,(0)*2.55])
    upper_blue = np.array([(360)/2,(16)*2.55,(15)*2.55])

    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)

    kernel = np.ones((2,2),np.uint8)
    morph2 = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel,iterations = 2)
    # kernel = np.ones((1,2),np.uint8)
    # morph = cv2.morphologyEx(morph2, cv2.MORPH_OPEN, kernel,iterations = 1)
    # kernel = np.ones((2,2),np.uint8)
    # morph2 = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel,iterations = 2)
    # kernel = np.ones((2,2),np.uint8)
    # morph = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel,iterations = 1)
    
    # cropim = im[140:380,100:550].copy()
    imw = rotateImage(morph2,3)
    print imw.shape
    imw= imw[219:270,34:546].copy()
    timw = cv2.adaptiveThreshold(imw,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
    print imw.shape
    # cv2.imshow("Result", imw)
    # cv2.waitKey(0)
    #print imw.shape
    rack_array = []
    classified = []
    start = time.time()
    for cut in range(0,8):
        i = imw[:,cut*63:(1+cut)*63].copy()
        # cv2.imshow("cut",i)
        # cv2.waitKey(0)
        #print i
        backtorgb = cv2.cvtColor(i,cv2.COLOR_GRAY2RGB)
        rack_array.append(backtorgb)
        scipy.misc.imsave("9.jpg",backtorgb)
        #classified.append(dl_classify.classify([backtorgb]))

    #classified.append(dl_classify([None]))
    
    elap = time.time() - start
    print elap
Пример #27
0
def find_blur(img):
    # make img grey
    grey_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # try_1 = cv2.Sobel(grey_img, cv2.CV_8U, 0, 1, ksize=5)
    # se_1=cv2.getStructuringElement(cv2.MORPH_RECT,(23,5))
    # # try_1 = cv2.adaptiveThreshold(try_1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 4241, -6)
    # try_2=cv2.morphologyEx(try_1, cv2.MORPH_CLOSE, se_1)
    # try_2 = cv2.adaptiveThreshold(try_2, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 2241, -6)
    # minus_1 = minus_background_white(grey_img, try_2)
    # # minus_1_morf=cv2.morphologyEx(minus_1, cv2.MORPH_CLOSE, se_1)
    # # minus_1_morf_my = minus_1.copy()
    # try_3 = cv2.Sobel(grey_img, cv2.CV_8U, 1, 0, ksize=5)
    # try_4=cv2.morphologyEx(try_3, cv2.MORPH_CLOSE, se_1)
    # try_4 = cv2.adaptiveThreshold(try_4, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 2241, -6)
    # minus_2 = minus_background_white(minus_1, try_4)
    # minus_2_my = minus_2.copy()
    # other
    # delete sky and light objects
    blur_1=cv2.GaussianBlur(grey_img,(5,5),22)
    adaptive_1 = cv2.adaptiveThreshold(blur_1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 4241, -21)
    se_1=cv2.getStructuringElement(cv2.MORPH_RECT,(23,5))
    closing_1=cv2.morphologyEx(adaptive_1, cv2.MORPH_CLOSE, se_1)
    minus_1 = minus_background_white(grey_img, closing_1)
    # delete black background from grey img
    adaptive_2 = cv2.adaptiveThreshold(blur_1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 1111, 22)
    minus_2 = minus_background_black(minus_1, adaptive_2)
    # minus_2_my = minus_2.copy()
    # prepera new img for detection
    adaptive_search = cv2.adaptiveThreshold(minus_2, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, -5 ) # 5
    my_addaptive = adaptive_search.copy()
    # detect counturs
    image, contours, hierarchy = cv2.findContours(adaptive_search, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    pass_contours = []
    rejected_conturs = []
    for contour in contours:
        box, is_blury = detect_blur(contour, grey_img)
        if is_blury:
            pass_contours.append(box)
        else:
            rejected_conturs.append(box)
    return pass_contours, rejected_conturs , \
           {
               # '1': closing_1,
               # '3': adaptive_2,
               # # 'try_1': try_1,
               # '2': minus_1,
               # # 'try_2': try_2,
               # # 'try_3': try_3,
               # # 'try_4': try_4,
               # '4': minus_2_my,
               # # 'try_5': try_5_my,
               #  '5': my_addaptive,
           }
Пример #28
0
    def __read(self):
        self.img = cv2.medianBlur(self.img, 25)

        ret, th11 = cv2.threshold(self.img, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
        ret, th121 = cv2.threshold(self.img, 127, 255, cv2.THRESH_TRUNC | cv2.THRESH_OTSU)  #
        ret, th122 = cv2.threshold(self.img, 150, 255, cv2.THRESH_TRUNC)  #
        ret, th131 = cv2.threshold(self.img, 127, 255, cv2.THRESH_TOZERO_INV | cv2.THRESH_OTSU)
        ret, th132 = cv2.threshold(self.img, 90, 100, cv2.THRESH_TOZERO_INV)
        ret, th14 = cv2.threshold(self.img, 127, 255, cv2.THRESH_OTSU)
        # adaptive: border detection issue
        th2 = cv2.adaptiveThreshold(self.img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 551, 10)
        th3 = cv2.adaptiveThreshold(self.img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 151, 10)
        return th132
Пример #29
0
 def skew_correct(self, plate_detail, chars=None):
     (xmin, ymin, xmax, ymax) = plate_detail["size"]
     if True:
         # rotate our roiblobs
         plateregion = self.image[ymin - self.config["h_max"]:ymax + self.config["h_max"], xmin - 2*self.config["w_max"]:xmax + 2*self.config["w_max"]].copy()
         (h, w) = plateregion.shape[:2]
         (cX, cY) = (w / 2, h / 2)
         degrees = math.atan(plate_detail["average_angle"]) * 180 / math.pi
         #print degrees
         #rotate_deg = - atan(((*glob_charlys_lys_it).m1+(*glob_charlys_lys_it).m2)/2.0) * 180.0 / 3.14159265;
         #degrees = 0 # som om die gemiddelde M te kry en dan skakel ons dit om na degree
         M = cv2.getRotationMatrix2D((cX, cY), degrees, 1.0)
         rotated = cv2.warpAffine(plateregion, M, (w, h))
         #cv2.imshow("Rotated by xx Degrees", rotated)
         #cv2.waitKey(0)
         plate_detail["warped"] = rotated
         warped2 = cv2.adaptiveThreshold(rotated, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, self.config["thesh_window"], self.config["thesh_offset"])
         plate_detail["warped2"] = warped2
     #TODO: Rotate eerder as correct
     else:
         plate_detail["plate"] = ""
         plateregion = self.image[ymin - self.config["h_max"]:ymax + self.config["h_max"], xmin - 2*self.config["w_max"]:xmax + 2*self.config["w_max"]].copy()
         if self.debug:
             cv2.rectangle(self.roiblobs, (xmin - 2*self.config["w_max"], ymin - self.config["h_max"]), (xmax + 2*self.config["w_max"], ymax + self.config["h_max"]), (255, 0, 0), 1)
         #gray = plateregion#cv2.cvtColor(plateregion, cv2.COLOR_BGR2GRAY)
         gray = cv2.GaussianBlur(plateregion, (5, 5), 0)
         edged = cv2.Canny(gray, 25, 250)
         #edged = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 25, thesh_offset)
         (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
         cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
         screenCnt = None
         plate_detail["edged"] = edged
         # find contours, stolen from pyimagesearch
         for c in cnts:
             # approximate the contour
             peri = cv2.arcLength(c, True)
             approx = cv2.approxPolyDP(c, 0.02 * peri, True)
             
             cv2.drawContours(plateregion, [approx], -1, (0, 0, 0), 1)
             if len(approx) == 4:
                 screenCnt = approx
                 break
         plate_detail["screenCnt"] = screenCnt
         if plate_detail["screenCnt"] is not None:
             warped = four_point_transform(plateregion, screenCnt.reshape(4, 2) * 1)
             #cv2.imshow("warped2", warped)
             #cv2.waitKey(0)
             warped2 = cv2.adaptiveThreshold(warped, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, self.config["thesh_window"], self.config["thesh_offset"])
             plate_detail["warped"] = warped
             plate_detail["warped2"] = warped2
         return plate_detail
Пример #30
0
def process(controller):
    map_initialized = False
    num_images = 0
    num_samples = 0
    while True:
        frame = controller.frame()
        images = frame.images
        if images[0].is_valid and images[1].is_valid:
            if not map_initialized:
                left_x_map, left_y_map = initDistortionMap(frame.images[0])
                right_x_map, right_y_map = initDistortionMap(frame.images[1])
                map_initialized = True
            undistorted_left = interpolate(get_image_as_array(images[0]), left_x_map, left_y_map)
            undistorted_right = interpolate(get_image_as_array(images[1]), left_x_map, left_y_map)
            left_bw = cv2.adaptiveThreshold(undistorted_left, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 13, 3)
            right_bw = cv2.adaptiveThreshold(undistorted_right, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 13, 3)
            corners_left = cv2.findChessboardCorners(left_bw, board_size, flags=cv2.CALIB_CB_FILTER_QUADS)
            corners_right = cv2.findChessboardCorners(right_bw, board_size, flags=cv2.CALIB_CB_FILTER_QUADS)
            if corners_left[0] and corners_right[0]:
                cv2.cornerSubPix(left_bw, corners_left[1], (3, 3), (-1, -1), stereocalib_criteria)
                cv2.cornerSubPix(right_bw, corners_right[1], (3, 3), (-1, -1), stereocalib_criteria)
                cv2.drawChessboardCorners(undistorted_left, board_size, corners_left[1], corners_left[0])
                cv2.drawChessboardCorners(undistorted_right, board_size, corners_right[1], corners_right[0])
                image_points1.append(corners_left[1])
                image_points2.append(corners_right[1])
                object_points.append(obj)
                num_samples += 1
                print(num_samples)
                if num_samples > num_boards:
                    break
            cv2.imshow('left', undistorted_left)
            cv2.imshow('right', undistorted_right)
            cv2.imshow('left_bw', left_bw)
            cv2.imshow('right_bw', right_bw)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                cv2.imwrite(f'left{num_images}.jpg', left_bw)
                cv2.imwrite(f'right{num_images}.jpg', right_bw)
                num_images += 1
                break

    retval,cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(
        object_points, image_points1, image_points2, C1, D1, C2, D2, imageSize=(640, 240), flags=stereocalib_flags, criteria=stereocalib_criteria
    )

    print("params")
    print(retval,cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F)

    print("reprojection matrix")
    a = cv2.stereoRectify(C1, D1, C2, D2, (640, 240), R, T)
    print(a[-3])
Пример #31
0
def main(argv):
    # [load_image]
    # Check number of arguments
    if len(argv) < 1:
        print('Not enough parameters')
        print('Usage:\nmorph_lines_detection.py < path_to_image >')
        return -1

    # Load the image
    src = cv2.imread(argv[0], cv2.IMREAD_COLOR)

    # Check if image is loaded fine
    if src is None:
        print('Error opening image: ' + argv[0])
        return -1

    # Show source image
    cv2.imshow("src", src)
    # [load_image]

    # [gray]
    # Transform source image to gray if it is not already
    if len(src.shape) != 2:
        gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    else:
        gray = src

    # Show gray image
    show_wait_destroy("gray", gray)
    # [gray]

    # [bin]
    # Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol
    gray = cv2.bitwise_not(gray)
    bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
                                cv2.THRESH_BINARY, 15, -2)
    # Show binary image
    show_wait_destroy("binary", bw)
    # [bin]

    # [init]
    # Create the images that will use to extract the horizontal and vertical lines
    horizontal = np.copy(bw)
    vertical = np.copy(bw)
    # [init]

    # [horiz]
    # Specify size on horizontal axis
    cols = horizontal.shape[1]
    horizontal_size = cols / 30

    # Create structure element for extracting horizontal lines through morphology operations
    horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT,
                                                    (horizontal_size, 1))

    # Apply morphology operations
    horizontal = cv2.erode(horizontal, horizontalStructure)
    horizontal = cv2.dilate(horizontal, horizontalStructure)

    # Show extracted horizontal lines
    show_wait_destroy("horizontal", horizontal)
    # [horiz]

    # [vert]
    # Specify size on vertical axis
    rows = vertical.shape[0]
    verticalsize = rows / 30

    # Create structure element for extracting vertical lines through morphology operations
    verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT,
                                                  (1, verticalsize))

    # Apply morphology operations
    vertical = cv2.erode(vertical, verticalStructure)
    vertical = cv2.dilate(vertical, verticalStructure)

    # Show extracted vertical lines
    show_wait_destroy("vertical", vertical)
    # [vert]

    # [smooth]
    # Inverse vertical image
    vertical = cv2.bitwise_not(vertical)
    show_wait_destroy("vertical_bit", vertical)
    '''
    Extract edges and smooth image according to the logic
    1. extract edges
    2. dilate(edges)
    3. src.copyTo(smooth)
    4. blur smooth img
    5. smooth.copyTo(src, edges)
    '''

    # Step 1
    edges = cv2.adaptiveThreshold(vertical, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
                                cv2.THRESH_BINARY, 3, -2)
    show_wait_destroy("edges", edges)

    # Step 2
    kernel = np.ones((2, 2), np.uint8)
    edges = cv2.dilate(edges, kernel)
    show_wait_destroy("dilate", edges)

    # Step 3
    smooth = np.copy(vertical)

    # Step 4
    smooth = cv2.blur(smooth, (2, 2))

    # Step 5
    (rows, cols) = np.where(edges != 0)
    vertical[rows, cols] = smooth[rows, cols]

    # Show final result
    show_wait_destroy("smooth - final", vertical)
    # [smooth]

    return 0
Пример #32
0
    def reg_line(self, img, show=False):
        allBinary = cv2.resize(img.copy(),
                               (self.img_size[1], self.img_size[0]))
        # if show==True:
        #     cv2.imshow("allBinary",allBinary)

        # r_channel=resized[:,:,2]
        # binary=np.zeros_like(r_channel)
        # binary[(r_channel>200)]=1
        # #if show==True:("r_channel",binary)

        # hls=cv2.cvtColor(resized,cv2.COLOR_BGR2HLS)
        # s_channel = resized[:, :, 2]
        # binary2 = np.zeros_like(s_channel)
        # binary2[(r_channel > 160)] = 1

        # allBinary= np.zeros_like(binary)
        # allBinary[((binary==1)|(binary2==1))]=255
        if show == True:
            cv2.imshow("binary", allBinary)

        allBinary_visual = allBinary.copy()

        if show == True:
            cv2.polylines(allBinary_visual, [self.src_draw], True, 255)
            cv2.imshow("polygon", allBinary_visual)

        # M = cv2.getPerspectiveTransform(self.src, self.dst)
        warped = self.wrap(allBinary)
        # warped =
        # warped = cv2.adaptiveThreshold(cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY),255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
        #     cv2.THRESH_BINARY_INV,5,2)
        warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
        # _, warped = cv2.threshold(warped, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
        warped[(warped < 100)] = 100
        img_blur = cv2.medianBlur(warped, 5)

        if show == True:
            cv2.imshow("warpedq", img_blur)
        # cv2.imshow("warped1",warped)
        # warped = cv2.adaptiveThreshold(warped,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
        #     cv2.THRESH_BINARY_INV,5,2)

        warped = cv2.adaptiveThreshold(img_blur, 255,
                                       cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                       cv2.THRESH_BINARY_INV, 5, 2)

        # element = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
        # kernel = np.ones((10,10),np.uint8)
        warped = cv2.erode(warped, np.ones((1, 1), np.uint8))
        # warped =
        # warped = cv2.dilate(warped,np.ones((1,1),np.uint8),iterations = 1)
        # warped = self.thresh(warped)
        # warped = cv2.morphologyEx(warped, cv2.MORPH_OPEN, np.ones((3,3),np.uint8))
        warped = cv2.medianBlur(warped, 3)
        if show == True:
            cv2.imshow("warped", warped)

        histogram = np.sum(warped[warped.shape[0] // 2:, :], axis=0)

        midpoint = histogram.shape[0] // 2
        IndWhitestColumnL = np.argmax(histogram[:midpoint])
        IndWhitestColumnR = np.argmax(histogram[midpoint:]) + midpoint
        warped_visual = warped.copy()
        if show == True:
            cv2.line(warped_visual, (IndWhitestColumnL, 0),
                     (IndWhitestColumnL, warped_visual.shape[0]), 110, 2)
            cv2.line(warped_visual, (IndWhitestColumnR, 0),
                     (IndWhitestColumnR, warped_visual.shape[0]), 110, 2)
            cv2.imshow("WitestColumn", warped_visual)

        nwindows = 8
        window_height = np.int(warped.shape[0] / nwindows)
        window_half_width = 60

        XCenterLeftWindow = IndWhitestColumnL
        XCenterRightWindow = IndWhitestColumnR

        left_lane_inds = np.array([], dtype=np.int16)
        right_lane_inds = np.array([], dtype=np.int16)

        out_img = np.dstack((warped, warped, warped))

        nonzero = warped.nonzero()
        WhitePixelIndY = np.array(nonzero[0])
        WhitePixelIndX = np.array(nonzero[1])

        for window in range(nwindows):

            win_y1 = warped.shape[0] - (window + 1) * window_height
            win_y2 = warped.shape[0] - (window) * window_height

            left_win_x1 = XCenterLeftWindow - window_half_width
            left_win_x2 = XCenterLeftWindow + window_half_width
            right_win_x1 = XCenterRightWindow - window_half_width
            right_win_x2 = XCenterRightWindow + window_half_width

            if show == True:
                cv2.rectangle(out_img, (left_win_x1, win_y1),
                              (left_win_x2, win_y2), (50 + window * 21, 0, 0),
                              2)
                cv2.rectangle(out_img, (right_win_x1, win_y1),
                              (right_win_x2, win_y2), (0, 0, 50 + window * 21),
                              2)
                cv2.imshow("windows", out_img)

            good_left_inds = ((WhitePixelIndY >= win_y1) &
                              (WhitePixelIndY <= win_y2) &
                              (WhitePixelIndX >= left_win_x1) &
                              (WhitePixelIndX <= left_win_x2)).nonzero()[0]

            good_right_inds = ((WhitePixelIndY >= win_y1) &
                               (WhitePixelIndY <= win_y2) &
                               (WhitePixelIndX >= right_win_x1) &
                               (WhitePixelIndX <= right_win_x2)).nonzero()[0]

            left_lane_inds = np.concatenate((left_lane_inds, good_left_inds))
            right_lane_inds = np.concatenate(
                (right_lane_inds, good_right_inds))

            if len(good_left_inds) > 50:
                XCenterLeftWindow = np.int(
                    np.mean(WhitePixelIndX[good_left_inds]))
            if len(good_right_inds) > 50:
                XCenterRightWindow = np.int(
                    np.mean(WhitePixelIndX[good_right_inds]))

        out_img[WhitePixelIndY[left_lane_inds],
                WhitePixelIndX[left_lane_inds]] = [255, 0, 0]
        out_img[WhitePixelIndY[right_lane_inds],
                WhitePixelIndX[right_lane_inds]] = [0, 0, 255]
        if show == True:
            cv2.imshow("Lane", out_img)

        leftx = WhitePixelIndX[left_lane_inds]
        lefty = WhitePixelIndY[left_lane_inds]
        rightx = WhitePixelIndX[right_lane_inds]
        righty = WhitePixelIndY[right_lane_inds]
        center_fit = []
        if (len(lefty) > 10) and (len(leftx) > 10) and (len(righty) > 10) and (
                len(rightx) > 10):
            left_fit = np.polyfit(lefty, leftx, 2)
            right_fit = np.polyfit(righty, rightx, 2)

            center_fit = ((left_fit + right_fit) / 2)
            self.points = []
            for ver_ind in range(out_img.shape[0]):
                gor_ind = ((center_fit[0]) * (ver_ind**2) +
                           center_fit[1] * ver_ind + center_fit[2])

                # cv2.circle(out_img,(int(gor_ind),int(ver_ind)),2,(255,0,255),1)
                self.points.append([gor_ind, ver_ind])

        p_s = len(self.points)
        err = 0
        err2 = 0
        if (p_s > 0):
            qq = p_s // 8 * 4
            cv2.circle(
                out_img,
                (int(self.points[qq - 1][0]), int(self.points[qq - 1][1])), 2,
                (0, 80, 255), 1)
            cv2.circle(
                out_img,
                (int(self.points[p_s - 1][0]), int(self.points[p_s - 1][1])),
                2, (0, 80, 255), 1)
            err2 = self.img_size[1]//2 - \
                (self.points[p_s-1][0]+self.points[qq-1][0])/2+10
            err = self.points[p_s - 1][0] - self.points[qq - 1][0]
        # if show==True:
        #     cv2.imshow("CenterLine",out_img)
        # crop = warped[warped.shape[0]-200:warped.shape[0], warped.shape[1]//10*5-50:warped.shape[1]//10*5+50].copy()
        # su = np.sum(crop[:, :])
        # print("su", su)
        # su2 = 0
        # ccc = self.img_size[1]//2
        # if (p_s > 0):
        #     ccc = (self.points[p_s-1][0]+self.points[qq-1][0])//2
        # ccc = self.img_size[1]//2
        # yyyyy = 70

        # crop2 = warped[yyyyy:yyyyy+80, int(ccc-40):int(ccc+32)]
        # cv2.circle(out_img, (int(ccc), int((yyyyy + yyyyy + 70)/2)),
        #    2, (0, 255, 255), 3)
        # su2 = np.sum(crop2[:, :])
        # yyyyy = 0
        # crop2 = img_blur[yyyyy:yyyyy+70, int(ccc-80):int(ccc+80)]
        # su2 = np.sum(crop2[:, :])-su1
        # if (ccc-10) > 0 and (ccc+10) < self.img_size[1]:
        #     yyyyy= 70
        #     cv2.circle(out_img,(int(ccc),int((yyyyy + yyyyy+ 70)/2)),2,(0,255,255),3)
        #     crop2 = img_blur[yyyyy:yyyyy+70, int(ccc-5):int(ccc+5)]
        #     su2 = np.sum(crop2[:, :])
        # if (err < -80 or err > 80) or (su2 > 75000):  # 1866213
        #     err = 0
        #     err2 = 0
        if show == True:
            cv2.imshow("CenterLine", out_img)
        return err, err2, out_img  #, su2
Пример #33
0
import cv2
import numpy as np
from skimage.filters import threshold_adaptive

img = cv2.imread('Assets/test.jpg', 0)
img = cv2.medianBlur(img, 23)
th3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                            cv2.THRESH_BINARY, 31, 2)

cv2.imshow('image', th3)
cv2.waitKey(0)
cv2.destroyAllWindows()
Пример #34
0
# read and scale down image
# wget https://bigsnarf.files.wordpress.com/2017/05/hammer.png
img = cv2.pyrDown(cv2.imread('test2.jpg', cv2.IMREAD_UNCHANGED))
# # Apply dilation and erosion to remove some noise
# kernel = np.ones((1, 1), np.uint8)
# img = cv2.dilate(img, kernel, iterations=10)
# img = cv2.erode(img, kernel, iterations=1)
#
# # Apply blur to smooth out the edges
# img = cv2.GaussianBlur(img, (5, 5), 0)
# threshold image
# ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
#                                   127, 255, cv2.THRESH_BINARY)

threshed_img = cv2.adaptiveThreshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
                                     255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                     cv2.THRESH_BINARY, 11, 2)
# find contours and get the external one

kernel = np.ones((1, 1), np.uint8)
threshed_img = cv2.dilate(threshed_img, kernel, iterations=1)
threshed_img = cv2.erode(threshed_img, kernel, iterations=1)
# img[threshed_img == 255] = 0
# img[threshed_img == 0] = 255

# threshed_img = cv2.GaussianBlur(threshed_img, (9, 9), 0)

contours, hier = cv2.findContours(threshed_img, cv2.RETR_TREE,
                                  cv2.CHAIN_APPROX_SIMPLE)

# with each contour, draw boundingRect in green
Пример #35
0
import cv2
import numpy as np

# Read in
img = cv2.imread('11_2.jpg')

# Filter
# img = cv2.bilateralFilter(img, 9,75,75)
img = cv2.GaussianBlur(img, (3, 3), 0)

# Convert2binary
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                               cv2.THRESH_BINARY, 3, 5)
#adaptive_method = ADAPTIVE_THRESH_MEAN_C, threshold_type=THRESH_BINARY, block_size=3, param1=5

# edges = cv2.Canny(img, 20, 150, apertureSize = 3)

# FindContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
edges, contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE,
                                              cv2.CHAIN_APPROX_SIMPLE)

# show immediate canny image.
cv2.drawContours(img, contours, -1, (0, 0, 255), 3)
cv2.imshow("img", img)
cv2.imshow("edges", edges)
cv2.imwrite("edge.jpg", edges)
cv2.waitKey(200)

#print(hierarchy)
Пример #36
0
基于阈值方法图像分割
'''

import cv2

img = cv2.imread('output.jpg', 0)
ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)  # binary (黑白二值)
ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)  # (黑白二值反转)
ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)  # 得到的图像为多像素值
ret, thresh4 = cv2.threshold(img, 127, 255,
                             cv2.THRESH_TOZERO)  # 高于阈值时像素设置为255,低于阈值时不作处理
ret, thresh5 = cv2.threshold(img, 127, 255,
                             cv2.THRESH_TOZERO_INV)  # 低于阈值时设置为255,高于阈值时不作处理

print(ret)
th6 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                            cv2.THRESH_BINARY, 5, 2)
th7 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                            cv2.THRESH_BINARY, 11, 2)
th8 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                            cv2.THRESH_BINARY, 11, 2)

ret1, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)  # 简单滤波
ret2, th2 = cv2.threshold(img, 0, 255,
                          cv2.THRESH_BINARY + cv2.THRESH_OTSU)  # Otsu 滤波

cv2.imshow('thresh1', thresh1)
cv2.imshow('thresh2', thresh2)
cv2.imshow('thresh3', thresh3)
cv2.imshow('thresh4', thresh4)
cv2.imshow('thresh5', thresh5)
cv2.imshow('thresh6', th6)
Пример #37
0
 def __threshold(self):
     self.img = cv2.adaptiveThreshold(self.img, 255,
                                      cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                      cv2.THRESH_BINARY, 11, 5)
Пример #38
0
                #   только с numpy
                fun += 'np.' + fun_str[i]
    return fun


f = np.vectorize(norm)

model = joblib.load("cls.pkl")
path = "C:\\Program Files\\Epic Games\\UE_4.18\\Engine\\Binaries\\Win64\\RenderTarget.png"

img = cv2.bitwise_not(cv2.imread(path))

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)

thresh = cv2.adaptiveThreshold(gray, 255, 1, 1, 11, 2)
thresh_color = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)

thresh = cv2.dilate(thresh, None, iterations=3)
thresh = cv2.erode(thresh, None, iterations=2)

_, contours, _ = cv2.findContours(thresh, cv2.RETR_LIST,
                                  cv2.CHAIN_APPROX_SIMPLE)

normal_contours = np.array([], dtype='int32')

for ind, cnt in enumerate(contours):
    x, y, w, h = cv2.boundingRect(cnt)

    if ind == 0:
        normal_contours = np.array([[x, y, w, h]])
Пример #39
0
"""

import numpy as np
import cv2
from matplotlib import pyplot as plt


img = cv2.imread('b.jpg', 0)
ret,thresh1 = cv2.threshold(img, 130, 255, cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img, 140, 255, cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)

th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) # ADAPTIVE THRESHOLDING
th3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
ret2, th4 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) # OTSU'S BINARIZATION
blur = cv2.GaussianBlur(img, (5, 5), 0) # Removal of noise with gaussian for OTSU'S Binarization.
ret3, th5 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV', 'Adaptive Mean', 'Adaptive Gaussian', 'Otsu', 'Gaussian Otsu']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5, th2, th3, th4, th5]

for i in range(10):
    
    plt.subplot(4, 3, i+1)   # Sub-plot (n rows, ncolumns, indexof the particular plot
    plt.imshow(images[i], 'gray') 
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
Пример #40
0
import cv2 as cv
import numpy as np

img = cv.imread('sudoku.png', 0)
_, th1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(
    img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11,
    2)  # img, MaxValue, Adaptive Method, thr_type, block size, value of c
th3 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C,
                           cv.THRESH_BINARY, 11, 2)

cv.imshow("Image", img)
cv.imshow("th1", th1)
cv.imshow("th2", th2)
cv.imshow("th3", th3)

cv.waitKey(0)
cv.destroyAllWindows()
Пример #41
0
def segmentUpper(filename, showImages=False, save=False):

    directoryExtra = '_Data/Radiographs/extra/Cropped/'
    file_in = directoryExtra + filename
    '''Load image and resize it to see it better'''
    gray = cv2.imread(file_in, cv2.IMREAD_GRAYSCALE)
    shapeGray = gray.shape
    length = 1000
    r = float(length) / gray.shape[1]
    dim = (length, int(gray.shape[0] * r))
    gray = cv2.resize(gray, dim, interpolation=cv2.INTER_AREA)

    width = 100
    height = 100
    grayCut = gray[(int(dim[1] / 2) - height):(int(dim[1] / 2) + height),
                   (int(dim[0] / 2) - width):(int(dim[0] / 2) + width)]
    if showImages == True:
        cv2.imshow('window', gray)

    grayCut = cv2.fastNlMeansDenoising(grayCut, None, 8, 7, 21)

    edges = cv2.Canny(grayCut, 10, 35)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    dilation = cv2.dilate(edges, kernel, iterations=1)
    masked = cv2.bitwise_and(grayCut, dilation)

    nonZeroValues = masked[np.nonzero(masked)]
    threshold = np.mean(nonZeroValues)
    print threshold
    '''
    gray.shape gives (y-axis, x-axis)
    dim gives (x-axis, y-axis)
    CARFEFUL WITH USE!!
    '''

    difference = 1000
    x = 0

    meanForeground = 0
    meanBackground = 0

    #Iterative thresholding
    while abs(difference) > 0.0001:
        meanForeground = 0
        meanBackground = 0
        print 'Done ', x, ' times'
        x += 1

        masked2 = ma.masked_greater(grayCut, threshold)
        meanBackground = masked2.mean()
        other = grayCut[masked2.mask]
        meanForeground = other.mean()

        difference = threshold - (meanForeground + meanBackground) / 2.0
        threshold = (meanForeground + meanBackground) / 2.0

    ret, imgThreshold = cv2.threshold(grayCut, threshold, 1, cv2.THRESH_BINARY)

    imgThreshold = imgThreshold.astype(bool)
    #Adaptive thresholding is made on the masked image after iterative thresholding

    maskedThreshold = np.copy(grayCut)
    maskedThreshold[~imgThreshold] = 0

    #If the C constant is bigger than 0, more white areas are detected

    imgAdaptiveGaussian = cv2.adaptiveThreshold(maskedThreshold, 1,
                                                cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                                cv2.THRESH_BINARY, 51, 0)

    imgAdaptiveGaussian = imgAdaptiveGaussian.astype(bool)
    maskedAdaptiveGaussian = np.copy(grayCut)

    maskedAdaptiveGaussian[~imgAdaptiveGaussian] = 0
    imgAdaptiveGaussian = imgAdaptiveGaussian.astype(int) * 255
    imgAdaptiveGaussian = imgAdaptiveGaussian.astype('uint8')

    [n, d] = imgAdaptiveGaussian.shape

    suma = np.zeros((n))
    #Horizontal projections
    for i in range(n):

        for j in range(d):

            suma[i] += imgAdaptiveGaussian[i, j]

    minimum = np.min(suma)
    indeces = []
    #The horizontal projections with a value of 3 times the minimum are taken
    #If the minimum was 0, the threshold is 2000
    #(these thresholds were adapted experimentally)

    if minimum == 0:
        for i in range(n):
            if suma[i] <= 2000: indeces.append(i)
    else:
        for i in range(n):
            if suma[i] <= minimum * 3: indeces.append(i)

    imgAdaptiveGaussian = cv2.cvtColor(imgAdaptiveGaussian, cv2.COLOR_GRAY2BGR)

    for i in range(len(indeces)):
        cv2.line(imgAdaptiveGaussian, (0, indeces[i]), (d, indeces[i]),
                 (0, 255, 0), 2)

    if showImages == True:

        cv2.imshow('Green', imgAdaptiveGaussian)

    if save:

        cv2.imwrite(
            directoryExtra + 'upperTeeth/' + filename[:len(filename) - 12] +
            '_jawSegmentation.png', imgAdaptiveGaussian)

    #Now I crop the upper jaw and try to fin the teeth separation

    if len(indeces) > 1:
        cut = (int(np.mean(indeces)) + np.min(indeces)) / 2
        cut = (int(dim[1] / 2) - height) + cut
        #This is the place where I will segment the jaw
    else:
        cut = int(dim[1] / 2 - height + indeces[0])

    width = 100
    height = 150
    grayCut = gray[(cut - height):cut,
                   (int(dim[0] / 2) - width):(int(dim[0] / 2) + width)]

    grayCut = cv2.fastNlMeansDenoising(grayCut, None, 8, 7, 21)

    edges = cv2.Canny(grayCut, 10, 35)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    dilation = cv2.dilate(edges, kernel, iterations=1)
    masked = cv2.bitwise_and(grayCut, dilation)
    cv2.imshow('edges2', dilation)

    nonZeroValues = masked[np.nonzero(masked)]
    threshold = np.mean(nonZeroValues)
    print threshold
    '''
    gray.shape gives (y-axis, x-axis)
    dim gives (x-axis, y-axis)
    CARFEFUL WITH USE!!
    '''

    difference = 1000
    x = 0

    meanForeground = 0
    meanBackground = 0

    #Iterative thresholding
    while abs(difference) > 0.0001:

        meanForeground = 0
        meanBackground = 0
        print 'Done ', x, ' times'
        x += 1

        masked2 = ma.masked_greater(grayCut, threshold)
        meanBackground = masked2.mean()
        other = grayCut[masked2.mask]
        meanForeground = other.mean()

        difference = threshold - (meanForeground + meanBackground) / 2.0
        threshold = (meanForeground + meanBackground) / 2.0

    ret, imgThreshold = cv2.threshold(grayCut, threshold, 1, cv2.THRESH_BINARY)

    imgThreshold = imgThreshold.astype(bool)
    #Adaptive thresholding is made on the masked image after iterative thresholding

    maskedThreshold = np.copy(grayCut)
    maskedThreshold[~imgThreshold] = 0

    #If the C constant is bigger than 0, more white areas are detected

    imgAdaptiveGaussian = cv2.adaptiveThreshold(maskedThreshold, 1,
                                                cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                                cv2.THRESH_BINARY, 51, 0)

    imgAdaptiveGaussian = imgAdaptiveGaussian.astype(bool)
    maskedAdaptiveGaussian = np.copy(grayCut)

    maskedAdaptiveGaussian[~imgAdaptiveGaussian] = 0
    imgAdaptiveGaussian = imgAdaptiveGaussian.astype(int) * 255
    imgAdaptiveGaussian = imgAdaptiveGaussian.astype('uint8')

    [n, d] = imgAdaptiveGaussian.shape

    if showImages == True:

        cv2.imshow('window2', grayCut)
    if save:

        cv2.imwrite(
            directoryExtra + 'upperTeeth/' + filename[:len(filename) - 12] +
            '_upperTeeth.png', grayCut)

    suma2 = np.zeros((d))
    #Vertical projections
    for j in range(d):

        for i in range(n):

            suma2[j] += imgAdaptiveGaussian[i, j]

    minimum = min(suma2)
    indeces2 = []
    #The vertical projections with a value of 3 times the minimum are taken
    #If the minimum was 0, the threshold is 2000
    #(these thresholds were adapted experimentally)

    if minimum == 0:
        for i in range(d):
            if suma2[i] <= 2000: indeces2.append(i)
    else:
        for i in range(d):
            if suma2[i] <= minimum * 3: indeces2.append(i)

    imgAdaptiveGaussian2 = cv2.cvtColor(imgAdaptiveGaussian,
                                        cv2.COLOR_GRAY2BGR)

    for i in range(len(indeces2)):
        cv2.line(imgAdaptiveGaussian2, (indeces2[i], 0), (indeces2[i], n),
                 (0, 255, 0), 2)

    if showImages == True:

        cv2.imshow('Green2', imgAdaptiveGaussian2)
    if save:

        cv2.imwrite(
            directoryExtra + 'upperTeeth/' + filename[:len(filename) - 12] +
            '_upperTeethSeparation.png', imgAdaptiveGaussian2)

    meanValue = int(round(np.mean(indeces2)))

    imgAdaptiveGaussian3 = cv2.cvtColor(imgAdaptiveGaussian,
                                        cv2.COLOR_GRAY2BGR)

    if showImages == True:

        cv2.line(imgAdaptiveGaussian3, (meanValue, 0), (meanValue, n),
                 (0, 255, 0), 2)

        cv2.imshow('Green3', imgAdaptiveGaussian3)

    meanValue = int(round((dim[0] / 2 - width + meanValue + dim[0] / 2) / 2.0))

    point = np.array([cut, meanValue])

    gray2 = gray.copy()
    gray2 = gray2[(point[0] - 200):point[0],
                  (point[1] - width):(point[1] + width)]

    if save:
        cv2.imwrite(
            directoryExtra + 'upperTeeth/' + filename[:len(filename) - 12] +
            '_upperTeethFinal.png', gray2)

    cv2.imshow('final', gray2)
    print n, d

    point[0] *= float(shapeGray[0]) / dim[1]
    point[1] *= float(shapeGray[1]) / dim[0]

    return point
def extracTextFromImage(imgTestingNumbers):

    allContoursWithData = []                # declare empty lists,
    validContoursWithData = []              # we will fill these shortly

    try:
        npaClassifications = np.loadtxt("classifications.txt", np.float32)                  # read in training classifications
    except:
        print "error, unable to open classifications.txt, exiting program\n"
        os.system("pause")
        return
    # end try

    try:
        npaFlattenedImages = np.loadtxt("flattened_images.txt", np.float32)                 # read in training images
    except:
        print "error, unable to open flattened_images.txt, exiting program\n"
        os.system("pause")
        return
    # end try

    npaClassifications = npaClassifications.reshape((npaClassifications.size, 1))       # reshape numpy array to 1d, necessary to pass to call to train

    kNearest = cv2.ml.KNearest_create()                   # instantiate KNN object

    kNearest.train(npaFlattenedImages, cv2.ml.ROW_SAMPLE, npaClassifications)

    # imgTestingNumbers = cv2.imread("test/output (8).png")          # read in testing numbers image

    if imgTestingNumbers is None:                           # if image was not read successfully
        print "error: image not read from file \n\n"        # print error message to std out
        os.system("pause")                                  # pause so user can see error message
        return                                              # and exit function (which exits program)
    # end if

    # imgGray = cv2.cvtColor(imgTestingNumbers, cv2.COLOR_BGR2GRAY)       # get grayscale image
    imgBlurred = cv2.GaussianBlur(imgTestingNumbers, (5,5), 0)                    # blur

                                                        # filter image from grayscale to black and white
    imgThresh = cv2.adaptiveThreshold(imgBlurred,                           # input image
                                      255,                                  # make pixels that pass the threshold full white
                                      cv2.ADAPTIVE_THRESH_GAUSSIAN_C,       # use gaussian rather than mean, seems to give better results
                                      cv2.THRESH_BINARY_INV,                # invert so foreground will be white, background will be black
                                      15,                                   # size of a pixel neighborhood used to calculate threshold value
                                      1)                                    # constant subtracted from the mean or weighted mean

    imgThreshCopy = imgThresh.copy()        # make a copy of the thresh image, this in necessary b/c findContours modifies the image
    # cv2.imshow('thresimg',imgThreshCopy)

    imgContours, npaContours, npaHierarchy = cv2.findContours(imgThreshCopy,             # input image, make sure to use a copy since the function will modify this image in the course of finding contours
                                                 cv2.RETR_EXTERNAL,         # retrieve the outermost contours only
                                                 cv2.CHAIN_APPROX_SIMPLE)   # compress horizontal, vertical, and diagonal segments and leave only their end points

    for npaContour in npaContours:                             # for each contour
        contourWithData = ContourWithData()                                             # instantiate a contour with data object
        contourWithData.npaContour = npaContour                                         # assign contour to contour with data
        contourWithData.boundingRect = cv2.boundingRect(contourWithData.npaContour)     # get the bounding rect
        contourWithData.calculateRectTopLeftPointAndWidthAndHeight()                    # get bounding rect info
        contourWithData.fltArea = cv2.contourArea(contourWithData.npaContour)           # calculate the contour area
        allContoursWithData.append(contourWithData)                                     # add contour with data object to list of all contours with data
    # end for

    for contourWithData in allContoursWithData:                 # for all contours
        if contourWithData.checkIfContourIsValid():             # check if valid
            validContoursWithData.append(contourWithData)       # if so, append to valid contour list
        # end if
    # end for

    validContoursWithData.sort(key = operator.attrgetter("intRectX"))         # sort contours from left to right

    strFinalString = ""         # declare final string, this will have the final number sequence by the end of the program

    for contourWithData in validContoursWithData:            # for each contour
                                                # draw a green rect around the current char
        cv2.rectangle(imgTestingNumbers,                                        # draw rectangle on original testing image
                      (contourWithData.intRectX, contourWithData.intRectY),     # upper left corner
                      (contourWithData.intRectX + contourWithData.intRectWidth, contourWithData.intRectY + contourWithData.intRectHeight),      # lower right corner
                      (0, 255, 0),              # green
                      2)                        # thickness

        imgROI = imgThresh[contourWithData.intRectY : contourWithData.intRectY + contourWithData.intRectHeight,     # crop char out of threshold image
                           contourWithData.intRectX : contourWithData.intRectX + contourWithData.intRectWidth]

        imgROIResized = cv2.resize(imgROI, (RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT))             # resize image, this will be more consistent for recognition and storage

        npaROIResized = imgROIResized.reshape((1, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT))      # flatten image into 1d numpy array

        npaROIResized = np.float32(npaROIResized)       # convert from 1d numpy array of ints to 1d numpy array of floats

        retval, npaResults, neigh_resp, dists = kNearest.findNearest(npaROIResized, k = 1)     # call KNN function find_nearest

        strCurrentChar = str(chr(int(npaResults[0][0])))                                             # get character from results

        strFinalString = strFinalString + strCurrentChar            # append current char to full string
    # end for

    # print " " + strFinalString + ""                  # show the full string

    # cv2.imshow("imgTestingNumbers", imgTestingNumbers)      # show input image with green boxes drawn around found digits
    # cv2.waitKey(0)                                          # wait for user key press

    # cv2.destroyAllWindows()             # remove windows from memory

    return strFinalString
Пример #43
0
def HSUDetect(img):
    #Getting images + applying thresholding
    black = np.zeros((240, 320, 3),
                     dtype="uint8")  #Completely Black Image, (width, height

    #To get rid of buffer
    '''
    for i in range(9):
        cap.grab()
    '''
    orig = img
    frame = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY)
    img = cv2.GaussianBlur(frame, (9, 9), 6)
    dst = cv2.adaptiveThreshold(
        img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 17,
        2)  #cv2.THRESH_BINARY, 15, -1) #cv2.THRESH_BINARY_INV, 17, 2)
    #dum, dst = cv2.threshold(img,20,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    (cont, h) = cv2.findContours(dst.copy(), cv2.RETR_EXTERNAL,
                                 cv2.CHAIN_APPROX_SIMPLE)

    # Contour Filter (by area and dimension filtering)
    threshold_area = 500  #threshold area
    aCount = 0
    hCount = 0
    for i in range(0, len(cont)):
        area = cv2.contourArea(cont[i])
        x, y, width, height = cv2.boundingRect(cont[i])
        if area < threshold_area:
            cv2.drawContours(dst, cont, i, (0, 0, 0), cv2.FILLED)
            #aCount+=1
        if height > 200:
            cv2.drawContours(dst, cont, i, (0, 0, 0), cv2.FILLED)
        if width > 200:
            cv2.drawContours(dst, cont, i, (0, 0, 0), cv2.FILLED)
        if height * width > 22500:
            cv2.drawContours(dst, cont, i, (0, 0, 0), cv2.FILLED)
    #print("Area Filtered: " + str(aCount))

    #Finding Contours:
    (contours, h) = cv2.findContours(dst.copy(), cv2.RETR_EXTERNAL,
                                     cv2.CHAIN_APPROX_SIMPLE)
    cv2.imwrite("parsedimg.png", dst)
    #print(len(contours))
    if len(contours) == 0:
        print("No Letters!")
        return None
    dstcopy = dst.copy()

    #Countour manipulation to get region of interest
    c = max(contours, key=cv2.contourArea)  # largest countour
    contourcolor = (255, 255, 255)  #B, G, R
    x, y, w, h = cv2.boundingRect(c)
    if x > 5:
        x -= 5
    if y > 5:
        y -= 5
    w += 10
    h += 10
    #print("x: {}, y: {}, w:{}, h:{}".format(x,y,w,h))

    #Drawing Countours or Green Contours:
    cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 255, 0),
                  2)  # draw the book contour (in green)
    #cv2.rectangle(black, (x,y), (x+w, y+h), (255,255,0), 2)
    #cv2.drawContours(black, c, -1, contourcolor, -1)#, cv2.FILLED)
    #cv2.drawContours(orig, contours, -1, contourcolor, 3) #-1 is index of countour to draw. -1 means all. 3 - thickness of the lines

    # Image rotation

    sliced = dst[y:y + h, x:x + w]
    angle = cv2.minAreaRect(c)[-1]
    #(h, w) = sliced.shape[:2]
    #print ("h:{0}  w:{1}".format(h,w))
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, angle, 1.0)
    dst2 = cv2.warpAffine(sliced,
                          M, (w, h),
                          flags=cv2.INTER_CUBIC,
                          borderMode=cv2.BORDER_CONSTANT)
    (h2, w2) = dst2.shape
    print("h:{0}  w:{1}".format(h2, w2))

    # Grabbing contour of rotated image (determine if needs 90 deg rotate)
    (contours, __) = cv2.findContours(dst2.copy(), cv2.RETR_EXTERNAL,
                                      cv2.CHAIN_APPROX_SIMPLE)
    c2 = max(contours, key=cv2.contourArea)  # largest countour
    x2, y2, w2, h2 = cv2.boundingRect(c2)
    '''x2 += 10
    y2 += 10
    w2 -= 10
    h2 -= 10'''
    #print("x2: {}, y2: {}, w2:{}, h2:{}".format(x2,y2,w2,h2))
    #cv2.rectangle(dst2,(x2,y2),(x2+w2,y2+h2),(255,255,255),2)
    #print(angle)

    # Rotates 90 if needed
    if (w2 >= h2):  #width must be smaller than height
        dst2 = RotateImage(dst2, 90, 1.0)

    #Slicing Image
# top = dst[y:y+int(0.33*h), x:x+w]
# mid = dst[y+int(0.38*h):y+int(0.63*h), x:x+w]
# smid = dst[y+int(0.45*h):y+int(0.55*h), x:x+w]
# bot = dst[y+int(0.66*h):y+h, x:x+w]

#dum, dst2 = cv2.threshold(dst2,20,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

#Filtering due to random tiny contours from rotating the image (area filter)

    (cont, h) = cv2.findContours(dst2.copy(), cv2.RETR_EXTERNAL,
                                 cv2.CHAIN_APPROX_SIMPLE)
    threshold_area = 100  #threshold area
    for i in range(0, len(cont)):
        area = cv2.contourArea(cont[i])
        if area < threshold_area:
            cv2.drawContours(dst2, cont, i, (0, 0, 0), cv2.FILLED)

    #dst = cv2.adaptiveThreshold( img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 17, 2)

    # Gathering slices
    (roiy, roix) = dst2.shape
    # print ("y:{0} x:{1}".format(y,x))
    top = dst2[0:(roiy // 3), 0:roix]
    mid = dst2[int(roiy * 0.37):int(roiy * 0.63), 0:roix]
    smid = dst2[8 + int(roiy * 0.40):int(roiy * 0.55), 0:roix]
    bot = dst2[2 * roiy // 3:roiy, 0:roix]

    # Area filter on each of the slices
    (contop, h) = cv2.findContours(top.copy(), cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)
    (conmid, h) = cv2.findContours(mid.copy(), cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)
    (consmid, h) = cv2.findContours(smid.copy(), cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)  #For S
    (conbot, h) = cv2.findContours(bot.copy(), cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)

    areaFilter(top, contop, 20)
    areaFilter(mid, conmid, 20)
    areaFilter(smid, consmid, 20)
    areaFilter(bot, conbot, 20)

    #Calculating HSU
    (contop, h) = cv2.findContours(top.copy(), cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)
    (conmid, h) = cv2.findContours(mid.copy(), cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)
    (consmid, h) = cv2.findContours(smid.copy(), cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)  #For S
    (conbot, h) = cv2.findContours(bot.copy(), cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)
    '''
    widthmin = 20
    widthmax = 300
    heightmin = 20
    heightmax = 300
    print("Top: {}, Mid: {}, Bot: {}".format(len(contop), len(conmid), len(conbot)))
    for cnt in contop:
        rect = cv2.minAreaRect(cnt)       #I have used min Area rect for better result
        width = rect[1][0]
        height = rect[1][1]
        if (width>widthmax) and (height >heightmax) and (width <= widthMin) and (height < heightMin):
            del cnt
    cv2.drawContours(black, contop, 0, contourcolor, 3)
    '''

    # Determing HSU
    print("Top: {}, Mid: {}, SMid: {}, Bot: {}".format(len(contop),
                                                       len(conmid),
                                                       len(consmid),
                                                       len(conbot)))
    if len(contop) == 2 and len(conmid) == 1 and len(conbot) == 2:
        print("H DETECTED")
    elif len(contop) == 1 and len(consmid) == 1 and len(conbot) == 1:
        print("S DETECTED")
    elif (len(contop) == 2 and len(conmid) == 2
          and len(conbot) == 1) or (len(contop) == 1 and len(conmid) == 2
                                    and len(conbot) == 2):
        print("U DETECTED")

    #Showing Image
# cv2.imshow('Camera1', frame)
    cv2.imshow('dst', dst)
    cv2.imshow("RETR_EXTERNAL", orig)
    # cv2.imshow('black', black)
    # cv2.imshow('slice', sliced)
    cv2.imshow('dst2', dst2)

    (smidh, smidw) = smid.shape
    cv2.imshow('top', top)
    cv2.imshow('mid', mid)
    if smidh != 0 and smidw != 0:
        cv2.imshow('smid', smid)
    cv2.imshow('bot', bot)
Пример #44
0
kernelOp = np.ones((3, 3), np.uint8)
kernelCl = np.ones((9, 9), np.uint8)
# areaTH=area minima para considerar uma pessoa
areaTH = 500
i = 0
n_cont = 0
while (cap.isOpened()):
    ret, frame = cap.read()  #read a frame
    #i=i+1
    #if (i>30000):
    fgmask = fgbg.apply(frame)  #Use the substractor
    try:
        #threshold: If pixel value is greater than a threshold value, it is assigned one value (may be white), else it is assigned another value (may be black).First argument is the source image, which should be a grayscale image. Second argument is the threshold value which is used to classify the pixel values. Third argument is the maxVal which represents the value to be given if pixel value is more than (sometimes less than) the threshold value. OpenCV provides different styles of thresholding and it is decided by the fourth parameter of the function.
        #Adaptive thresholding: In this, the algorithm calculate the threshold for a small regions of the image. So we get different thresholds for different regions of the same image and it gives us better results for images with varying illumination.
        #ver http://docs.opencv.org/trunk/d7/d4d/tutorial_py_thresholding.html
        imBin = cv2.adaptiveThreshold(fgmask,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv2.THRESH_BINARY_INV,11,2)
        #Opening (erode->dilate) para tirar ruido.
        mask = cv2.morphologyEx(imBin, cv2.MORPH_OPEN, kernelOp)
        #Closing (dilate -> erode) para juntar regioes brancas.
        mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernelCl)
    except:
        #if there are no more frames to show...
        print('EOF')
        break
    #finding contours is like finding white object from black background. Object to be found should be white and background should be black.
    #cv2.RETR_EXTERNAL means we only care about external contours (contours within contours will not be detected)
    #cv2.CHAIN_APPROX_NONE is the algorithm used to make the contour
    #ver mais http://docs.opencv.org/3.2.0/d4/d73/tutorial_py_contours_begin.html
    _, contours0, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                               cv2.CHAIN_APPROX_NONE)
    for cnt in contours0:
Пример #45
0
  def execute(self):
    img=self.img

    #gray is used for classification and search, gray_c only for search
    gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(4,8))
    gray_c = clahe.apply(gray)

    #guess 'white' color
    epsilon=0.0001
    white=np.median(gray)
    white=np.mean(gray[gray>white-epsilon])
    white_c=np.median(gray_c)
    white_c=np.mean(gray_c[gray_c>white_c-epsilon])

    window=21
    #adaptive mean
    th2 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, window, 2)
    #adaptive gaussian
    th3 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 2)
    #Otsu's
    _,th4 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    #adaptive mean
    th_c2 = cv2.adaptiveThreshold(gray_c, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, window, 2)
    #adaptive gaussian
    th_c3 = cv2.adaptiveThreshold(gray_c, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 2)
    #Otsu's
    _,th_c4 = cv2.threshold(gray_c,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

    #extend borders(probably only needed for detectMultiscale)
    img=cv2.copyMakeBorder(img, 4, 4, 2, 2, cv2.BORDER_CONSTANT, value=(int(white), int(white), int(white)))
    gray=cv2.copyMakeBorder(gray, 4, 4, 2, 2, cv2.BORDER_CONSTANT, value=int(white))
    gray_c=cv2.copyMakeBorder(gray_c, 4, 4, 2, 2, cv2.BORDER_CONSTANT, value=int(white_c))
    ths=[th2, th3, th4, th_c2, th_c3, th_c4]
    for i in xrange(len(ths)):
      ths[i]=cv2.copyMakeBorder(ths[i], 4, 4, 2, 2, cv2.BORDER_CONSTANT, value=255)
    th2, th3, th4, th_c2, th_c3, th_c4 = ths

    self.debug(img, "svm_img")
    self.debug(gray, "svm_gr")
    self.debug(gray_c, "svm_gr_c")
    self.debug(th2, "svm_th2")
    self.debug(th3, "svm_th3")
    self.debug(th4, "svm_th4")
    self.debug(th_c2, "svm_th_c2")
    self.debug(th_c3, "svm_th_c3")
    self.debug(th_c4, "svm_th_c4")

    plate=[]

    min_height=10
    min_width=5
    min_area=70
    epsilon=0.00001

    @memoize
    def max_score_hsplit(box, n=3):
      x,y,w,h=box
      l,s=max_score(box)

      if s<0.0:
        l_s=[(s, [(l,s,box)])]
      else:
        l_s=[(epsilon, [(l,epsilon,box)])]

      if n>1:
        for w0 in xrange(1, w):
          if w0*h<min_area or w0<min_width or h<min_height:
            l0, s0=(None, epsilon)
          else:
            l0, s0=max_score((x,y,w0,h))

          if (w-w0)*h<min_area or (w-w0)<min_width or h<min_height:
            s1, ls1=(epsilon, [(None, epsilon, (x+w0,y,w-w0,h))])
          else:
            s1, ls1=max_score_hsplit((x+w0,y,w-w0,h),n-1)

          if s0>epsilon:
            s0=epsilon

          score=(s0*w0+s1*(w-w0)+0.0)/w

          l_s+=[(score, [(l0, s0, (x,y,w0,h))]+ls1)]
      return min(l_s)

    #functions defined as closures to avoid passing multiple and/or complex arguments
    #which allows memoize_simple use and autoresets after execute comletion

    def compute_hog(box):
      X,Y,W,H=box
      gray_=gray[Y-1:Y+H+1, X-1:X+W+1] #FIXME should check area bounds

      winSize = (20, 30)
      blockSize = (4,6)
      blockStride = (2,3)
      cellSize = (2,3)
      nbins=9

      winStride = (20,30)
      padding = (0,0)

      gray_=cv2.resize(gray_, winSize, interpolation = cv2.INTER_CUBIC)

      hog=cv2.HOGDescriptor(winSize, blockSize,blockStride,cellSize, nbins)
      desc = hog.compute(gray_, winStride, padding, ((0, 0),))

      return desc

    letters=['1','2','3','4','5','6','7','8','9','0O','A','B','C','E','H','K','M','P','T','X','Y']
    @memoize_simple
    def max_score(box):
      x,y,w,h=box

      if w*h<min_area or w<min_width or h<min_height:
        return (None, 1.0)

      desc=compute_hog(box)

      l_s=[(l, -self.svm_letters[l].predict(desc, returnDFVal=True)) for l in letters]

      return min(l_s, key=lambda x: x[1])

    letter_ligatures=['8dot', 'dotO', 'dotM', 'dotB', 'dotC', 'dotH', 'dotE', 'dotP']
    @memoize_simple
    def max_score_ligatures(box):
      x,y,w,h=box
      if w*h<min_area or w<min_width or h<min_height:
        return (None, 1.0)

      desc=compute_hog(box)

      l_s=[(l, -self.svm_letters[l].predict(desc, returnDFVal=True)) for l in letter_ligatures]
      return min(l_s, key=lambda x: x[1])

    h1_candidates=[10,5]
    h2_candidates=[16,22]

    @memoize_simple
    def max_score_vsplit(box):
      x,y,w,h=box
      l_s=[]
      min_score=1.0
      min_letter=None
      min_box=(x,y,w,h)

      for h1 in h1_candidates:
        for h2 in h2_candidates:
          l,s=max_score((x,h1,w,h2))
          s=s*h2/(h+0.0)
          if s<min_score:
            min_score=s
            min_letter=l
            min_box=(x,h1,w,h2)

      return min_letter, min_score, min_box

    def max_score_hsplit3(box):
      x,y,w,h=box

      min_score=1.0
      min_letter=None
      min_box=(x,y,w,h)

      for w1 in xrange(0,min(w-min_width,10)):

        for w2 in xrange(min_width, min(w-w1,16)):
          b_=(x+w1,y,w2,h)

          l,s,b=max_score_vsplit(b_)
          s=s/(w+0)*w2
          if s<min_score:
            min_score=s
            min_letter=l
            min_box=b

      return min_score, min_letter, min_box

    #replacing original compute_hog with memoized version
    #will be restored after ligatures detection
    compute_hog_raw=compute_hog
    compute_hog=memoize_simple(compute_hog)

    boxes=[]
    for th in ths:
      boxes+=self.get_boxes_from_contour(th, gray)
    boxes=list(set(boxes)) #get uniq boxes

    #annotate each box with name for debug, letter, score, cropped image
    boxes=[box+(str(box), None, 1.0, None) for box in boxes]

    #search all boxes for letters
    boxes_left=[]
    while boxes:
      X,Y,W,H,m,min_letter,min_score,b_img = boxes.pop()

      b_img=gray[Y-1:Y+H+1, X-1:X+W+1]
      self.debug(b_img, "svm1_t_"+str(m))

      min_letter, min_score=max_score((X,Y,W,H))

      if min_score<0:
        self.debug(b_img, "svm1_f_"+min_letter+"_"+str(m))
        plate+=[(min_letter, (X,Y,W,H), -min_score)]
      else:
        boxes_left+=[(X,Y,W,H,m,min_letter,min_score, b_img)]

    #prune plate, distructive to origianl
    plate=prune_plate(plate, threshold=0.799)

    #are we done?
    #RUSSIAN PLATE TYPE1 SPECIFIC
    alphas, nums, alphanums=get_stats_symbols(plate)
    if alphanums>=9:
      return TaskResultSVMLetterDetector(plate)

    #prune boxes by content
    hranges=get_free_hranges(gray, plate, 2)
    hranges=range_diff_many([(0,gray.shape[1])], [(r[0], r[1]-r[0]+1) for r in hranges])
    hranges=[r for r in hranges if r[1]>0]

    boxes=boxes_left
    boxes_left=[]
    while boxes:
      X,Y,W,H,m,min_letter,min_score,b_img = boxes.pop()

      fr=range_diff_many([(X,W)], hranges)
      for r in fr:
        X, W=r
        if W<min_width:
          continue
        b_img=gray[Y-1:Y+H+1, X-1:X+W+1]
        min_letter, min_score=max_score((X,Y,W,H))
        m_r=str(m)+"_"+str(r)
        boxes_left+=[(X,Y,W,H,m_r,min_letter,min_score, b_img)]
        self.debug(b_img, "svm1_t2_"+str(m_r))

    #search known 'ligatures'
    boxes=boxes_left
    boxes_left=[]
    while boxes:
      X,Y,W,H,m,min_letter,min_score,b_img = boxes.pop()

      min_letter_new, min_score_new=max_score_ligatures((X,Y,W,H))

      if min_score_new<0:
        min_letter=min_letter_new.replace('dot','').replace('O','0O')
        min_score=min_score_new
        self.debug(b_img, "svm1_fl_"+min_letter+"_"+str(m))
        plate+=[(min_letter, (X,Y,W,H), -min_score)]
      else:
        boxes_left+=[(X,Y,W,H,m,min_letter,min_score,b_img)]

    #prune plate, distructive to origianl
    plate=prune_plate(plate, threshold=0.799)

    #replace score if ligaturazed version if better
    #FIXME maybe just recrop?
    for i in xrange(len(plate)):
      letter, box, score = plate[i]
      X,Y,W,H = box
      if letter in ['8', 'O0', 'M', 'B', 'C', 'H', 'E', 'P']:
        if letter=='8':
          ligature='8dot'
        elif letter=='0O':
          ligature='dotO'
        else:
          ligature='dot'+letter
        desc=compute_hog(box)
        score=max(score, self.svm_letters[ligature].predict(desc, returnDFVal=True))
        plate[i]=(letter, (X,Y,W,H), score)

    #are we done?
    #RUSSIAN PLATE TYPE1 SPECIFIC
    alphas, nums, alphanums=get_stats_symbols(plate)
    if alphanums>=9:
      return TaskResultSVMLetterDetector(plate)

    #search by splitting
    boxes=boxes_left
    boxes_left=[]
    while boxes:
      X,Y,W,H,m,min_letter,min_score,b_img = boxes.pop()

      s, splt=max_score_hsplit((X,Y,W,H), n=3)

      for k in xrange(len(splt)):
        letter_s, score_s, box_s=splt[k]
        if score_s<0:
          b_img_s=gray[box_s[1]-1:box_s[1]+box_s[3]+1, box_s[0]-1:box_s[0]+box_s[2]+1]
          self.debug(b_img_s, "svm1_fspl_"+letter_s+"_"+str(m)+"_"+str(k))
          plate+=[(letter_s, box_s, -score_s)]

      if s>0:
        self.debug(b_img, "svm1_nf_"+str(m))

    #restore original compute_hog
    compute_hog_raw=compute_hog_raw

    #prune plate, distructive to original
    plate=prune_plate(plate, threshold=0.799) #distructive
    #plate=sorted(plate, key=lambda x: x[1][0]+x[1][2]/2.0)

    #'bruteforce' search
    hranges=[(r[0], r[1]-r[0]+1) for r in get_free_hranges(gray, plate, 2)]
    h1_cnds=list(set([l[1][1] for l in plate]))
    h2_cnds=list(set([l[1][3] for l in plate]))

    if len(h1_cnds)>2:
      h1_candidates=h1_cnds
    if len(h2_cnds)>2:
      h2_candidates=h2_cnds

    ws=[l[1][2] for l in plate]
    if ws:
       min_width=max(min_width, int(np.floor(min(ws)*0.75)))
    max_width=20

    for r in hranges:
      x,w=r
      if w<min_width:
        continue
      if x==0:
        x+=1
      if x+w==gray.shape[1]:
        w-=1

      scores=[max_score_hsplit3((x+i, 0, min(w-i,max_width), gray.shape[0])) for i in xrange(0,w-min_width,3)]
      for s in [s for s in scores if s[0]<0.0]:
        min_letter, min_score=max_score(s[2])
        b_img=gray[s[2][1]-1:s[2][1]+s[2][3]+1, s[2][0]-1:s[2][0]+s[2][2]+1]

        self.debug(b_img, "svm1_fbf_"+min_letter+"_"+str(s[2]))
        plate+=[(min_letter, s[2], -min_score)]

    plate=prune_plate(plate, threshold=0.799) #distructive

    return TaskResultSVMLetterDetector(plate)
def apply_w_shed_segmentation(img_8bit, adapThresh_blcokSize,
                              adapThresh_constant, min_distance):

    img_8bit_copy = img_8bit.copy()
    img_8bit_copy = cv2.cvtColor(img_8bit_copy, cv2.COLOR_GRAY2BGR)

    height, width = img_8bit.shape[:2]

    # binarise image
    img_binary = cv2.adaptiveThreshold(img_8bit, 255,
                                       cv2.ADAPTIVE_THRESH_MEAN_C,
                                       cv2.THRESH_BINARY, adapThresh_blcokSize,
                                       adapThresh_constant)

    # modify the binary image by filling holes
    img_binary = fill_holes_in_binary_image(img_binary)

    # watershed segmentation
    D = ndimage.distance_transform_edt(img_binary)
    localMax = peak_local_max(D,
                              indices=False,
                              min_distance=min_distance,
                              labels=img_binary)
    markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]

    labels = watershed(
        -D, markers,
        mask=img_binary)  # If returned warning, upgrade to latest Skimage
    sp_arr = csr_matrix(labels.reshape(1, -1))
    labels_shape = labels.shape
    cluster_index_total = len(np.unique(sp_arr.data))
    cluster_index_list = np.arange(1, cluster_index_total +
                                   1)  # excluding index = [background]

    # keep positions
    points = np.zeros([20000, 2], dtype=int)
    X, Y = 0, 1
    n_cell = 0

    # go through non-zero values in the sparsed version of the 'labels' array
    # for i in np.unique(sp_arr.data)[1:10]:  # as a bonus this `unique` call should be faster too
    for cluster_index in cluster_index_list:  # OR for i in np.unique(sp_arr.data)

        # get mask image and its top-left corner position
        cnt_mask, cnt_topLeft_P = get_cnt_mask(cluster_index, sp_arr,
                                               labels_shape)

        # detect contours in the cnt_mask and grab the largest one
        cnt = get_contour_in_mask(cnt_mask, cnt_topLeft_P)

        if len(cnt) >= 5:

            ellipse = cv2.fitEllipse(cnt)
            (x, y), (MA, ma), angle = ellipse
            centre = (int(x), int(y))

            points[n_cell][X] = x
            points[n_cell][Y] = height - y

            ellipse_area = np.pi * (MA / 2.0) * (ma / 2.0)
            #             cv2.ellipse(img_8bit_copy, ellipse, (255,0,0), 1)
            cv2.drawContours(img_8bit_copy, [cnt], 0, (0, 255, 0), 1)
            #             cv2.circle(img_8bit_copy, centre, 1, (255,255,255), 1)
            n_cell += 1

    print("Total No of cells: ", n_cell)
    points = points[0:n_cell]

    return points, img_8bit_copy
Пример #47
0
def load_thresholded_image(image_path, x=11, y=5):
    data = read_data(image_path).reshape(16 * 660, 512, order="A")
    data *= 255 / data.max()
    data = data.astype(np.uint8)
    return cv2.adaptiveThreshold(data, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                 cv2.THRESH_BINARY, x, y)
Пример #48
0
import cv2
import numpy as np

img = cv2.imread('closeTest/closerubix2.jpg')
small = cv2.imread('normSquare.png', 0)
grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.adaptiveThreshold(grayscale, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                             cv2.THRESH_BINARY, 31, 7)
#gray = cv2.erode(gray, kernelg)
cv2.imshow('adaptive thresh', gray)
cv2.waitKey(0)
kernel2 = np.ones((8, 5), np.uint8)
#gray = cv2.erode(gray, kernel2)

cv2.imshow('adaptive thresh', gray)
cv2.waitKey(0)

contours, hierarchy = cv2.findContours(gray, cv2.RETR_LIST,
                                       cv2.CHAIN_APPROX_SIMPLE)

contours_small, hierarchy_small = cv2.findContours(small.copy(), cv2.RETR_LIST,
                                                   cv2.CHAIN_APPROX_SIMPLE)
cnt1 = contours[0]

squares = []
for cnt in contours:
    ret = cv2.matchShapes(cnt1, cnt, 1, 0.0)
    area = cv2.contourArea(cnt)
    print ret
    print 'this is area'
    print area
Пример #49
0
scale = booksScaleDictionary[bookName]


def rescaleFrame(frame, scale):
    width = int(frame.shape[1] * scale)
    len = int(frame.shape[0] * scale)
    dim = (width, len)
    return cv.resize(frame, dim, interpolation=cv.INTER_AREA)


img = rescaleFrame(img, scale)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
blur = cv.GaussianBlur(gray, (3, 3), 9)

threshold = cv.adaptiveThreshold(blur, 255, cv.CALIB_CB_ADAPTIVE_THRESH,
                                 cv.THRESH_BINARY, 13, 3)
kernel = np.ones((2, 2), np.uint8)
threshold = cv.erode(threshold, kernel, iterations=1)


# Performs flood fill and report height and width of the image
def dfs(image, n, m, begi, begj, i, j, height, width, color):
    if (i >= n) or (j >= m) or (i < 0) or (j < 0):
        return
    if image[i][j] != 255:
        return
    image[i][j] = color
    dfs(image, n, m, begi, begj, i + 1, j, height, width, color)
    dfs(image, n, m, begi, begj, i, j + 1, height, width, color)
    dfs(image, n, m, begi, begj, i - 1, j, height, width, color)
    dfs(image, n, m, begi, begj, i, j - 1, height, width, color)
Пример #50
0
R, G, B = cv2.split(RGB)

#Create a CLAHE object: The image is divided into small block 8x8 which they are equalized as usual.
clahe = cv2.createCLAHE(clipLimit=2.5, tileGridSize=(8, 8))
#Applying this method to each channel of the color image
output_2R = clahe.apply(R)
output_2G = clahe.apply(G)
output_2B = clahe.apply(B)

#mergin each channel back to one
img_output = cv2.merge((output_2R, output_2G, output_2B))
#coverting image from RGB to Grayscale
eq = cv2.cvtColor(img_output, cv2.COLOR_BGR2GRAY)
#Using image thresholding to classify pixels as dark or light
#This method provides changes in illumination and the contrast of the image is improved.
gauss = cv2.adaptiveThreshold(eq, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                              cv2.THRESH_BINARY, 17, 45)


def cropText(img, clear_image):
    edges = cv2.Canny(img, 100, 200)  #canny edge detection
    kernel = cv2.getStructuringElement(
        cv2.MORPH_CROSS, (3, 3))  # Define the area of focus around each pixel
    dilation = cv2.dilate(
        edges, kernel,
        iterations=9)  # dilate merge all edges into one big group
    _, contours, hierarchy = cv2.findContours(
        dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
    )  # This method returns 3 variables for getting contours
    for contour in contours:
        # get sizes of the rectangle return by the contours
        [x, y, w, h] = cv2.boundingRect(contour)
Пример #51
0
        croppedImgszzz = []
        height = 15
        width = 11
        x = 349
        y = 338
        cv2.rectangle(croppedImg, (x, y), (x+width, y+ height), (255, 255, 0), 1)
        croppedImgszzz.append(croppedGrayImg[y:y+height,x:x+width])

        x = 397
        cv2.rectangle(croppedImg, (x, y), (x+width, y+ height), (255, 255, 0), 1)
        croppedImgszzz.append(croppedGrayImg[y:y+height,x:x+width])
        
        numpy.set_printoptions(linewidth=500)
        bitArray = []
        for i in range (len(croppedImgszzz)):
            th3 = cv2.adaptiveThreshold(croppedImgszzz[i],255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
                    cv2.THRESH_BINARY,11,2)
            bitArray.append(th3)
        for k in range(2):
            print (bitArray[k])
        time.sleep(1)
        

        


        #######################################################################################################################
        #Identify Value inside contours and reduce # to two (this will only happen if a card is highlighted twice
        #SHOULD IDENTIFY DIGITS %

        #Identify Color boundaries
        # define the list of boundaries
Пример #52
0
 def thresholdify(self, img):
     img = cv2.adaptiveThreshold(img.astype(np.uint8), 255,
                                 cv2.ADAPTIVE_THRESH_MEAN_C,
                                 cv2.THRESH_BINARY, 11, 3)
     return 255 - img
Пример #53
0
#pip install pytesseract
#pip install opencv-python
import cv2
import numpy as np
from pytesseract import Output
import pytesseract
import cv2

img = cv2.imread('img1.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray, 3, 0)
thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                               cv2.THRESH_BINARY_INV, 11, 30)

mask = cv2.threshold(img, 120, 250, cv2.THRESH_BINARY_INV)[1][:, :, 0]
dst = cv2.inpaint(img, mask, 20, cv2.INPAINT_TELEA)

file1 = open("result.txt", "w")

custom_config = r'-c tessedit_char_blacklist= --psm 6'

rgb = cv2.cvtColor(thresh, cv2.COLOR_BGR2RGB)
results = pytesseract.image_to_data(rgb,
                                    output_type=Output.DICT,
                                    config=custom_config)
cord = []
for i in range(0, len(results["text"])):

    x = results["left"][i]
    y = results["top"][i]
    resize_scale = 0.85
    new_x, new_y = image.shape[1]*resize_scale, image.shape[0]*resize_scale
    image = cv2.resize(image,(int(new_x),int(new_y)))
    height, width, depth = image.shape

    # cv2.imshow("Image", image)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # cv2.imshow("gray", gray)

    median = cv2.medianBlur(gray, 7)

    blur = cv2.GaussianBlur(median, (5,5), 0)
    # cv2.imshow("blur", blur)

    thresh = cv2.adaptiveThreshold(median, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 15, 2)
    # cv2.imshow("thresh", thresh)

    _, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    max_area = 0
    c = 0
    for i in contours:
            area = cv2.contourArea(i)
            if area > height * width / 2:
                if area > max_area:
                    max_area = area
                    best_cnt = i
                    # image = cv2.drawContours(image, contours, c, (0, 255, 0), 3)
            c+=1
Пример #55
0
def Adaptive_Thresolding():
    inpath = os.path.join(os.getcwd(), ".\\Scaned image\\.", "")
    for x in os.listdir(inpath):
        if os.path.isfile(x):
            print('f-', x)
        elif os.path.isdir(x):
            print('d-', x)
        elif os.path.islink(x):
            print('l-', x)
        else:
            print('---', x)

    new = input("ENTER YOUR INPUT IMAGE DIRECTORY: ")
    inpath = os.getcwd() + '\\Scaned image\\' + new

    if not os.path.exists(inpath):
        print("ENTER CORRECT DIRECTORY...")
        return 0

    out_arr = [
        'Adaptive_Threshold', 'Global_Thresold', 'Otsus_Threshold',
        'Local_Binary'
    ]

    entries = os.listdir(inpath)

    for i in range(len(out_arr)):
        outpath = os.getcwd() + '\\Scaned image\\OUT\\' + out_arr[i]
        if os.path.exists(outpath):
            shutil.rmtree(outpath)

        os.mkdir(outpath)

    outpath = os.getcwd() + '\\Scaned image\\OUT\\'
    #src_fname, ext = os.path.splitext(x)
    k = 0
    for entry in entries:

        img = np.array([], dtype=int)

        img = cv2.imread(os.path.join(inpath, entry))

        #img = img[1150:2900,0:img.shape[1]]

        print(entry)

        Gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        """Adaptive Thresolding"""
        img_in = cv2.medianBlur(Gray_img, 5)
        img_out = cv2.adaptiveThreshold(img_in, 255,
                                        cv2.ADAPTIVE_THRESH_MEAN_C,
                                        cv2.THRESH_BINARY, 11, 2)
        output = np.concatenate((img_in, img_out), axis=1)
        out_string = 'Original + Adaptive 0000' + str(k) + '.tif'
        i = 0
        cv2.imwrite(os.path.join(outpath, out_arr[i], out_string), output)
        """Global Thresolding"""
        b, img_out = cv2.threshold(Gray_img, 127, 255, cv2.THRESH_BINARY)
        output = np.hstack((Gray_img, img_out))
        out_string = 'Original + Global 0000' + str(k) + '.tif'
        i = 1
        cv2.imwrite(os.path.join(outpath, out_arr[i], out_string), output)
        """Otsus Thresolding"""
        b, img_out = cv2.threshold(Gray_img, 127, 255,
                                   cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        output = np.hstack((Gray_img, img_out))
        out_string = 'Original + Otsus 0000' + str(k) + '.tif'
        i = 2
        cv2.imwrite(os.path.join(outpath, out_arr[i], out_string), output)
        """Local Binary Pattern"""
        img_out = feature.local_binary_pattern(Gray_img,
                                               8,
                                               1,
                                               method='default')
        output = np.hstack((Gray_img, img_out))
        out_string = 'Original + LBP 0000' + str(k) + '.tif'
        i = 3
        cv2.imwrite(os.path.join(outpath, out_arr[i], out_string), output)
        k += 1

    # cwd = os.getcwd()   "USE TO GET THE CURRENT DIRECTORY"
    print("VIEW OUTPUT FROM THIS DIRECTORY.... ", outpath)
    return 1
Пример #56
0
import numpy as np

img = cv2.imread('ocr1/img_17.jpg')
imgGrey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

kernel1 = np.ones((3, 3), np.uint8)
kernel2 = np.ones((3, 3), np.uint8)
kernel = np.zeros((5, 5), np.uint8)
kernel[2, :] = 1
#kernel[:,3]=1

# Można użyć do wykrywania samych literek i cyfr
#edges = cv2.Canny(imgGrey,220,300,apertureSize = 3)

blur = cv2.GaussianBlur(imgGrey, (5, 5), 0)
edges = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2)
#can = cv2.erode(can, kernel, iterations=1)
ero = cv2.dilate(edges, kernel, iterations=1)
dil = cv2.erode(ero, kernel, iterations=2)

# Wykrywanie linii
lines = cv2.HoughLinesP(dil,
                        1,
                        np.pi / 180,
                        80,
                        minLineLength=400,
                        maxLineGap=20)

# Pozostawienie tylko linii poziomych
lines = list(filter(lambda l: abs(l[0][1] - l[0][3]) < 20, lines))
linesSorted = sorted(lines, key=lambda l: l[0][1])
import cv2
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.models import load_model

img = cv2.imread("11.jpeg")
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img_checker = cv2.adaptiveThreshold(img_gray, 255,
                                    cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                    cv2.THRESH_BINARY_INV, 175, 45)
# img_checker = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 31, 25)
plt.imshow(img_checker)
plt.show()
_, contours, hierarchy = cv2.findContours(img_checker, cv2.RETR_EXTERNAL,
                                          cv2.CHAIN_APPROX_SIMPLE)

rects = [cv2.boundingRect(contour) for contour in contours]

for rect in rects:
    if rect[2] * rect[3] < 1000:
        continue
    print(rect)
    cv2.rectangle(img, (rect[0], rect[1]),
                  (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)
    if rect[2] < 50:
        margin = 100
    else:
        margin = 60
    roi = img_checker[rect[1] - margin:rect[1] + rect[3] + margin,
                      rect[0] - margin:rect[0] + rect[2] + margin]
    # cv2.imshow("Resulting Image with Rectangular ROIs", img)
Пример #58
0
sys.path.append('/usr/local/lib/python3.5/dist-packages')
import numpy as np
import cv2
import ocr_mnist

# MNISTの学習データを読む --- (※1)
mnist = ocr_mnist.build_model()
mnist.load_weights('mnist.hdf5')

# 画像の読み込み --- (※2)
im = cv2.imread('numbers100.png')

# 輪郭を抽出 --- (※3)
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # グレイスケールに
blur = cv2.GaussianBlur(gray, (5, 5), 0) # ぼかす
thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2) # 二値化
cv2.imwrite("numbers100-th.png", thresh)
contours = cv2.findContours(thresh,
    cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[1]

# 抽出した座標を左上から右下へと並び替える --- (※4)
rects = []
im_w = im.shape[1]
for i, cnt in enumerate(contours):
    x, y, w, h = cv2.boundingRect(cnt)
    if w < 10 or h < 10: continue # 小さすぎるのは飛ばす
    if w > im_w / 5: continue # 大きすぎるのも飛ばす
    y2 = round(y / 10) * 10 # Y座標を揃える
    index = y2 * im_w  + x
    rects.append((index, x, y, w, h))
rects = sorted(rects, key=lambda x:x[0]) # 並び替え
def preprocess_image(image):
    gray_blur = cv2.GaussianBlur(image, (15, 15), 0)
    thresh = cv2.adaptiveThreshold(gray_blur, 255,
                                   cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                   cv2.THRESH_BINARY_INV, 11, 1)
    return thresh
def main():
    allContoursWithData = []  # declare empty lists,
    validContoursWithData = []  # we will fill these shortly

    try:
        npaClassifications = np.loadtxt(
            "classifications.txt",
            np.float32)  # read in training classifications
    except:
        print("error, unable to open classifications.txt, exiting program\n")
        os.system("pause")
        return
    # end try

    try:
        npaFlattenedImages = np.loadtxt("flattened_images.txt",
                                        np.float32)  # read in training images
    except:
        print("error, unable to open flattened_images.txt, exiting program\n")
        os.system("pause")
        return
    # end try

    npaClassifications = npaClassifications.reshape(
        (npaClassifications.size,
         1))  # reshape numpy array to 1d, necessary to pass to call to train

    kNearest = cv2.ml.KNearest_create()  # instantiate KNN object

    kNearest.train(npaFlattenedImages, cv2.ml.ROW_SAMPLE, npaClassifications)

    imgTestingNumbers = cv2.imread(
        "img{}.png".format(i))  # read in testing numbers image

    if imgTestingNumbers is None:  # if image was not read successfully
        print("error: image not read from file \n\n"
              )  # print error message to std out
        os.system("pause")  # pause so user can see error message
        return  # and exit function (which exits program)
    # end if

    imgGray = cv2.cvtColor(imgTestingNumbers,
                           cv2.COLOR_BGR2GRAY)  # get grayscale image
    imgBlurred = cv2.GaussianBlur(imgGray, (5, 5), 0)  # blur

    # filter image from grayscale to black and white
    imgThresh = cv2.adaptiveThreshold(
        imgBlurred,  # input image
        255,  # make pixels that pass the threshold full white
        cv2.
        ADAPTIVE_THRESH_GAUSSIAN_C,  # use gaussian rather than mean, seems to give better results
        cv2.
        THRESH_BINARY_INV,  # invert so foreground will be white, background will be black
        11,  # size of a pixel neighborhood used to calculate threshold value
        2)  # constant subtracted from the mean or weighted mean

    imgThreshCopy = imgThresh.copy(
    )  # make a copy of the thresh image, this in necessary b/c findContours modifies the image

    imgContours, npaContours, npaHierarchy = cv2.findContours(
        imgThreshCopy,  # input image, make sure to use a copy since the function will modify this image in the course of finding contours
        cv2.RETR_EXTERNAL,  # retrieve the outermost contours only
        cv2.CHAIN_APPROX_SIMPLE
    )  # compress horizontal, vertical, and diagonal segments and leave only their end points

    for npaContour in npaContours:  # for each contour
        contourWithData = ContourWithData(
        )  # instantiate a contour with data object
        contourWithData.npaContour = npaContour  # assign contour to contour with data
        contourWithData.boundingRect = cv2.boundingRect(
            contourWithData.npaContour)  # get the bounding rect
        contourWithData.calculateRectTopLeftPointAndWidthAndHeight(
        )  # get bounding rect info
        contourWithData.fltArea = cv2.contourArea(
            contourWithData.npaContour)  # calculate the contour area
        allContoursWithData.append(
            contourWithData
        )  # add contour with data object to list of all contours with data
    # end for

    for contourWithData in allContoursWithData:  # for all contours
        if contourWithData.checkIfContourIsValid():  # check if valid
            validContoursWithData.append(
                contourWithData)  # if so, append to valid contour list
        # end if
    # end for

    validContoursWithData.sort(key=operator.attrgetter(
        "intRectX"))  # sort contours from left to right

    strFinalString = ""  # declare final string, this will have the final number sequence by the end of the program

    for contourWithData in validContoursWithData:  # for each contour
        # draw a green rect around the current char
        cv2.rectangle(
            imgTestingNumbers,  # draw rectangle on original testing image
            (contourWithData.intRectX,
             contourWithData.intRectY),  # upper left corner
            (contourWithData.intRectX + contourWithData.intRectWidth,
             contourWithData.intRectY +
             contourWithData.intRectHeight),  # lower right corner
            (0, 255, 0),  # green
            2)  # thickness

        imgROI = imgThresh[
            contourWithData.intRectY:contourWithData.intRectY +
            contourWithData.intRectHeight,  # crop char out of threshold image
            contourWithData.intRectX:contourWithData.intRectX +
            contourWithData.intRectWidth]

        imgROIResized = cv2.resize(
            imgROI, (RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT)
        )  # resize image, this will be more consistent for recognition and storage

        npaROIResized = imgROIResized.reshape(
            (1, RESIZED_IMAGE_WIDTH *
             RESIZED_IMAGE_HEIGHT))  # flatten image into 1d numpy array

        npaROIResized = np.float32(
            npaROIResized
        )  # convert from 1d numpy array of ints to 1d numpy array of floats

        retval, npaResults, neigh_resp, dists = kNearest.findNearest(
            npaROIResized, k=1)  # call KNN function find_nearest

        strCurrentChar = str(chr(int(
            npaResults[0][0])))  # get character from results

        strFinalString = strFinalString + strCurrentChar  # append current char to full string
        # end for

        r = strFinalString
    ###########################################
    iisf = sqlite3.connect("chalan.db")
    cur = iisf.cursor()

    sql = "select * from vehicle where vehicle_number='" + r + "';"
    x = cur.execute(sql)

    if x != None:
        y = cur.fetchone()
        try:
            mail = "*****@*****.**"
            password = "******"
            sub = "E-CHALAN VEHICLE POLLUTION"
            person = y[2]
            a = y[5]
            d = datetime.datetime.today()
            date = datetime.datetime.strptime(a, "%Y-%m-%d")
            com = d.year - date.year
            if (com < 15):
                body = "Subject: {}\n Hello {} vehicle number {} model {} producing excesive amount of harmfull gases. \n please submit fine of Rs 1000 to the nearest RTO office or police station. \n Thank You \n RTO office".format(
                    sub, y[1], y[0], y[3])
                c = canvas.Canvas("{}.pdf".format(y[1]))
                c.drawString(280, 800, "E-challan")
                c.drawString(50, 650,
                             "Date:{}".format(datetime.datetime.now()))
                c.drawString(
                    50, 630,
                    "Chalan No. {}".format(random.randint(654789, 987654)))
                seal = 'download.jpg'
                c.drawImage(seal, 260, 670, width=100, height=100)
                c.drawString(50, 610, "Sir,")
                c.drawString(
                    80, 590,
                    "{} vehicle number {} model {} ".format(y[1], y[0], y[3]))
                c.drawString(80, 570,
                             "producing excesive amount of harmfull gases.")
                c.drawString(
                    80, 550,
                    "please submit fine of Rs 1000 to the nearest RTO office or police station."
                )
                c.drawString(50, 500, "Thank You:")
                c.drawString(50, 480, "RTO OFFICE")
                c.save()
                subject = "E-chalan"
                message = MIMEMultipart()
                message['From'] = mail
                message['To'] = person
                message['Subject'] = subject
                ##
                message.attach(MIMEText(body, 'plain'))
                filename = "{}.pdf".format(y[1])
                attachment = open(filename, 'rb')
                part = MIMEBase('application', 'octet-stream')
                part.set_payload((attachment).read())
                encoders.encode_base64(part)
                part.add_header('content-Disposition',
                                'attachment; filename=' + filename)
                message.attach(part)
                text = message.as_string()
                ##
                ##
                server = smtplib.SMTP('smtp.gmail.com', 587)
                server.ehlo()
                server.starttls()
                server.login(mail, password)
                ##

                server.sendmail(mail, person, text)
                server.quit()
                print("Chalan send on number {}".format(r))

            else:
                body = "Subject: {}\n Hello {} vehicle number {} model {} is cross the limit of 15 years so we cancelling your registration number.".format(
                    sub, y[1], y[0], y[3])
                c = canvas.Canvas("{}.pdf".format(y[1]))
                c.drawString(280, 800, "E-challan")
                c.drawString(50, 650,
                             "Date:{}".format(datetime.datetime.now()))
                c.drawString(
                    50, 630,
                    "Chalan No. {}".format(random.randint(654789, 987654)))
                seal = 'download.jpg'
                c.drawImage(seal, 260, 670, width=100, height=100)
                c.drawString(50, 610, "Sir,")
                c.drawString(
                    80, 590,
                    "{} vehicle number {} model {} is cross the limit of 15 year "
                    .format(y[1], y[0], y[3]))
                #c.drawString(80,570,"is cross the limit of 15 year ")
                c.drawString(80, 570,
                             "So we are cancelling you vehicle registration.")
                c.drawString(50, 530, "Thank You:")
                c.drawString(50, 500, "RTO OFFICE")
                c.save()
                subject = "Vechicle Registration Cancellation"
                message = MIMEMultipart()
                message['From'] = mail
                message['To'] = person
                message['Subject'] = subject
                message.attach(MIMEText(body, 'plain'))
                filename = "{}.pdf".format(y[1])
                attachment = open(filename, 'rb')
                part = MIMEBase('application', 'octet-stream')
                part.set_payload((attachment).read())
                encoders.encode_base64(part)
                part.add_header('content-Disposition',
                                'attachment; filename=' + filename)
                message.attach(part)
                text = message.as_string()
                server = smtplib.SMTP('smtp.gmail.com', 587)
                server.ehlo()
                server.starttls()
                server.login(mail, password)
                server.sendmail(mail, person, text)
                server.quit()
                print("{} vehicle registration cancel".format(r))

        except:
            print("{} VEHICLE NOT REGISTERED".format(r))
#########################3

    cv2.imshow("imgTestingNumbers", imgTestingNumbers)
    # show input image with green boxes drawn around found digits
    cv2.waitKey(2)

    #cv2.destroyAllWindows()             # remove windows from memory

    return