久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用于 OCR 的 Python OpenCV 偏斜校正

Python OpenCV skew correction for OCR(用于 OCR 的 Python OpenCV 偏斜校正)
本文介紹了用于 OCR 的 Python OpenCV 偏斜校正的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

目前,我正在做一個 OCR 項目,我需要從標簽上讀取文本(參見下面的示例圖片).我遇到了圖像傾斜問題,我需要幫助修復圖像傾斜,以便文本是水平的而不是傾斜的.目前,我正在使用的過程嘗試從給定范圍(代碼包含在下面)中對不同角度進行評分,但這種方法不一致,有時會過度校正圖像歪斜或完全無法識別歪斜并糾正它.請注意,在進行歪斜校正之前,我將所有圖像旋轉 270 度以使文本直立,然后通過下面的代碼傳遞圖像.傳遞給函數的圖像已經是二值圖像了.

Currently, I am working on an OCR project where I need to read the text off of a label (see example images below). I am running into issues with the image skew and I need help fixing the image skew so the text is horizontal and not at an angle. Currently the process I am using attempts to score different angles from a given range (code included below), but this method is inconsistent and sometimes overcorrects an image skew or flat out fails to identify the skew and correct it. Just as a note, before the skew correction I am rotating all of the images by 270 degrees to get the text upright, then I am passing the image through the code below. The image passed through to the function is already a binary image.

代碼:


def findScore(img, angle):
    """
    Generates a score for the binary image recieved dependent on the determined angle.

    Vars:

    - array <- numpy array of the label

    - angle <- predicted angle at which the image is rotated by

    Returns:

    - histogram of the image
    - score of potential angle
    """
    data = inter.rotate(img, angle, reshape = False, order = 0)
    hist = np.sum(data, axis = 1)
    score = np.sum((hist[1:] - hist[:-1]) ** 2)
    return hist, score

def skewCorrect(img):
    """
    Takes in a nparray and determines the skew angle of the text, then corrects the skew and returns the corrected image.

    Vars:

    - img <- numpy array of the label

    Returns:

    - Corrected image as a numpy array

    """
    #Crops down the skewImg to determine the skew angle
    img = cv2.resize(img, (0, 0), fx = 0.75, fy = 0.75)

    delta = 1
    limit = 45
    angles = np.arange(-limit, limit+delta, delta)
    scores = []
    for angle in angles:
        hist, score = findScore(img, angle)
        scores.append(score)
    bestScore = max(scores)
    bestAngle = angles[scores.index(bestScore)]
    rotated = inter.rotate(img, bestAngle, reshape = False, order = 0)
    print("[INFO] angle: {:.3f}".format(bestAngle))
    #cv2.imshow("Original", img)
    #cv2.imshow("Rotated", rotated)
    #cv2.waitKey(0)
    
    #Return img
    return rotated

校正前后的標簽示例圖片

Example images of the label before correction and after

修正前->修正后

如果有人能幫我解決這個問題,那將有很大幫助.

If anyone can help me figure this problem out, it would be of much help.

推薦答案

這是一個用于確定偏斜的 Projection Profile Method 的實現.在獲得二值圖像后,想法是將圖像旋轉到各個角度,并在每次迭代中生成像素的直方圖.為了確定傾斜角度,我們比較了峰值之間的最大差異,并使用這個傾斜角度,旋轉圖像來糾正傾斜

Here's an implementation of the Projection Profile Method to determine skew. After obtaining a binary image, the idea is rotate the image at various angles and generate a histogram of pixels in each iteration. To determine the skew angle, we compare the maximum difference between peaks and using this skew angle, rotate the image to correct the skew

左(原始),右(更正)

Left (original), Right (corrected)

import cv2
import numpy as np
from scipy.ndimage import interpolation as inter

def correct_skew(image, delta=1, limit=5):
    def determine_score(arr, angle):
        data = inter.rotate(arr, angle, reshape=False, order=0)
        histogram = np.sum(data, axis=1)
        score = np.sum((histogram[1:] - histogram[:-1]) ** 2)
        return histogram, score

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] 

    scores = []
    angles = np.arange(-limit, limit + delta, delta)
    for angle in angles:
        histogram, score = determine_score(thresh, angle)
        scores.append(score)

    best_angle = angles[scores.index(max(scores))]

    (h, w) = image.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, best_angle, 1.0)
    rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, 
              borderMode=cv2.BORDER_REPLICATE)

    return best_angle, rotated

if __name__ == '__main__':
    image = cv2.imread('1.png')
    angle, rotated = correct_skew(image)
    print(angle)
    cv2.imshow('rotated', rotated)
    cv2.imwrite('rotated.png', rotated)
    cv2.waitKey()

這篇關于用于 OCR 的 Python OpenCV 偏斜校正的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美在线视频不卡 | 午夜国产一级 | 久久成人激情 | 精品乱码一区二区 | 亚洲日韩中文字幕一区 | 国产成人免费网站 | 国产福利在线播放麻豆 | 日韩一区二区三区视频 | 嫩草国产 | 国产精品免费看 | 国产第一区二区 | 国产精品久久久久久 | 国产黄色在线 | 亚洲国产欧美精品 | 一级无毛片 | 久久久久国产一区二区 | www国产成人免费观看视频 | 欧美一区二区久久 | 欧美激情精品久久久久久变态 | 国产免费视频 | 国产欧美日韩综合精品一区二区 | 国产精品久久久久久一区二区三区 | 日韩欧美视频在线 | 国产日韩精品一区二区三区 | 一区二区电影 | 中文字幕视频一区二区 | 国产精品久久久久久久免费大片 | 欧美国产精品一区二区三区 | 久久伊人影院 | 国产无人区一区二区三区 | 美女在线观看国产 | 亚洲国产精品久久 | 中文字幕一页二页 | 色综合久久久 | 91在线视频在线观看 | 久久久精品日本 | www.788.com色淫免费 | 国产成都精品91一区二区三 | 国内精品伊人久久久久网站 | 国产免费黄网 | 91精品久久久久久久久中文字幕 |