pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

根據(jù)文本方向檢測圖像方向角度

Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
本文介紹了根據(jù)文本方向檢測圖像方向角度的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在執(zhí)行一項 OCR 任務,以從多個身份證明文件中提取信息.一個挑戰(zhàn)是掃描圖像的方向.需要固定 PAN、Aadhaar、駕駛執(zhí)照或任何身份證明的掃描圖像的方向.

已經在 Stackoverflow 和其他論壇上嘗試過所有建議的方法,例如 OpenCV minAreaRect、霍夫線變換、FFT、單應性、具有 psm 0 的 tesseract osd.沒有一個有效.

邏輯應返回文本方向的角度 - 0、90 和 270 度.附上0、90、270度的圖片.這與確定偏度無關.

解決方案

這是一種基于大部分文本偏向一側的假設的方法.這個想法是我們可以根據(jù)主要文本區(qū)域的位置來確定角度

  • 將圖像轉換為灰度和高斯模糊
  • 獲取二值圖像的自適應閾值
  • 使用輪廓區(qū)域查找輪廓和過濾
  • 在蒙版上繪制過濾輪廓
  • 根據(jù)方向水平或垂直分割圖像
  • 計算每一半的像素數(shù)

轉換為灰度和高斯模糊后,我們自適應閾值得到二值圖像

從這里我們找到輪廓并使用輪廓區(qū)域進行過濾以去除小的噪聲顆粒和大的邊界.我們將通過此過濾器的任何輪廓繪制到蒙版上

為了確定角度,我們根據(jù)圖像的尺寸將圖像分成兩半.如果 <代碼> 寬度 >height 那么它必須是水平圖像,所以我們垂直分成兩半.如果 <代碼> 高度 >寬度 那么它必須是垂直圖像所以我們水平分割成兩半

現(xiàn)在我們有兩半,我們可以使用 cv2.countNonZero() 來確定每一半的白色像素的數(shù)量.以下是確定角度的邏輯:

如果是水平的如果左 >= 右度->0別的度->180如果垂直如果頂部 >= 底部度->270別的度->90

<塊引用>

離開9703

右 3975

因此圖像是 0 度.這是其他方向的結果

<塊引用>

離開 3975

右 9703

我們可以得出結論,圖像翻轉了 180 度

這是垂直圖像的結果.注意因為它是一個垂直的圖像,我們水平分割

<塊引用>

前 3947 個

底部 9550

因此結果是90度

導入 cv2將 numpy 導入為 npdef 檢測角度(圖像):掩碼 = np.zeros(image.shape,dtype=np.uint8)灰色 = cv2.cvtColor(圖像,cv2.COLOR_BGR2GRAY)模糊 = cv2.GaussianBlur(灰色, (3,3), 0)自適應 = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,15,4)cnts = cv2.findContours(自適應,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)cnts = cnts[0] 如果 len(cnts) == 2 否則 cnts[1]對于 cnts 中的 c:面積 = cv2.contourArea(c)如果面積 <45000 和區(qū)域 >20:cv2.drawContours(掩碼,[c],-1,(255,255,255),-1)掩碼 = cv2.cvtColor(掩碼,cv2.COLOR_BGR2GRAY)h, w = mask.shape# 水平的如果 w >H:左 = 掩碼[0:h, 0:0+w//2]右 = 掩碼 [0:h, w//2:]left_pixels = cv2.countNonZero(左)right_pixels = cv2.countNonZero(右)如果 left_pixels >= right_pixels 則返回 0 否則 180# 垂直的別的:頂部 = 掩碼[0:h//2, 0:w]底部 = 掩碼[h//2:, 0:w]top_pixels = cv2.countNonZero(top)bottom_pixels = cv2.countNonZero(底部)如果 bottom_pixels >= top_pixels 則返回 90,否則返回 270如果 __name__ == '__main__':圖像 = cv2.imread('1.png')角度 = 檢測角度(圖像)打印(角度)

I am working on a OCR task to extract information from multiple ID proof documents. One challenge is the orientation of the scanned image. The need is to fix the orientation of the scanned image of PAN, Aadhaar, Driving License or any ID proof.

Already tried all suggested approaches on Stackoverflow and other forums such as OpenCV minAreaRect, Hough Lines Transforms, FFT, homography, tesseract osd with psm 0. None are working.

The logic should return the angle of the text direction - 0, 90 and 270 degrees. Attached are the images of 0, 90 and 270 degrees. This is not about determining the skewness.

解決方案

Here's an approach based on the assumption that the majority of the text is skewed onto one side. The idea is that we can determine the angle based on the where the major text region is located

  • Convert image to grayscale and Gaussian blur
  • Adaptive threshold to get a binary image
  • Find contours and filter using contour area
  • Draw filtered contours onto mask
  • Split image horizontally or vertically based on orientation
  • Count number of pixels in each half

After converting to grayscale and Gaussian blurring, we adaptive threshold to obtain a binary image

From here we find contours and filter using contour area to remove the small noise particles and the large border. We draw any contours that pass this filter onto a mask

To determine the angle, we split the image in half based on the image's dimension. If width > height then it must be a horizontal image so we split in half vertically. if height > width then it must be a vertical image so we split in half horizontally

Now that we have two halves, we can use cv2.countNonZero() to determine the amount of white pixels on each half. Here's the logic to determine angle:

if horizontal
    if left >= right 
        degree -> 0
    else 
        degree -> 180
if vertical
    if top >= bottom
        degree -> 270
    else
        degree -> 90

left 9703

right 3975

Therefore the image is 0 degrees. Here's the results from other orientations

left 3975

right 9703

We can conclude that the image is flipped 180 degrees

Here's results for vertical image. Note since its a vertical image, we split horizontally

top 3947

bottom 9550

Therefore the result is 90 degrees

import cv2
import numpy as np

def detect_angle(image):
    mask = np.zeros(image.shape, dtype=np.uint8)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (3,3), 0)
    adaptive = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,15,4)

    cnts = cv2.findContours(adaptive, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]

    for c in cnts:
        area = cv2.contourArea(c)
        if area < 45000 and area > 20:
            cv2.drawContours(mask, [c], -1, (255,255,255), -1)

    mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    h, w = mask.shape
    
    # Horizontal
    if w > h:
        left = mask[0:h, 0:0+w//2]
        right = mask[0:h, w//2:]
        left_pixels = cv2.countNonZero(left)
        right_pixels = cv2.countNonZero(right)
        return 0 if left_pixels >= right_pixels else 180
    # Vertical
    else:
        top = mask[0:h//2, 0:w]
        bottom = mask[h//2:, 0:w]
        top_pixels = cv2.countNonZero(top)
        bottom_pixels = cv2.countNonZero(bottom)
        return 90 if bottom_pixels >= top_pixels else 270

if __name__ == '__main__':
    image = cv2.imread('1.png')
    angle = detect_angle(image)
    print(angle)

這篇關于根據(jù)文本方向檢測圖像方向角度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
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 centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
Calculating percentage of Bounding box overlap, for image detector evaluation(計算邊界框重疊的百分比,用于圖像檢測器評估)
主站蜘蛛池模板: KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 微学堂-电动能源汽车评测_电动车性能分享网 | 全自动贴标机-套标机-工业热风机-不干胶贴标机-上海厚冉机械 | 信阳网站建设专家-信阳时代网联-【信阳网站建设百度推广优质服务提供商】信阳网站建设|信阳网络公司|信阳网络营销推广 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 广州食堂承包_广州团餐配送_广州堂食餐饮服务公司 - 旺记餐饮 | 国资灵活用工平台_全国灵活用工平台前十名-灵活用工结算小帮手 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 专业深孔加工_东莞深孔钻加工_东莞深孔钻_东莞深孔加工_模具深孔钻加工厂-东莞市超耀实业有限公司 | 航空障碍灯_高中低光强航空障碍灯_民航许可认证航空警示灯厂家-东莞市天翔航天科技有限公司 | 篷房[仓储-婚庆-展览-活动]生产厂家-江苏正德装配式帐篷有限公司 | 胜为光纤光缆_光纤跳线_单模尾纤_光纤收发器_ODF光纤配线架厂家直销_北京睿创胜为科技有限公司 - 北京睿创胜为科技有限公司 | 裹包机|裹膜机|缠膜机|绕膜机-上海晏陵智能设备有限公司 | 宝鸡市人民医院| 车辆定位管理系统_汽车GPS系统_车载北斗系统 - 朗致物联 | 流量检测仪-气密性检测装置-密封性试验仪-东莞市奥图自动化科技有限公司 | 中央空调温控器_风机盘管温控器_智能_液晶_三速开关面板-中央空调温控器厂家 | 宁夏档案密集柜,智能密集柜,电动手摇密集柜-盛隆柜业宁夏档案密集柜厂家 | 宝鸡市人民医院| 轴流风机-鼓风机-离心风机-散热风扇-罩极电机,生产厂家-首肯电子 | 托盘租赁_塑料托盘租赁_托盘出租_栈板出租_青岛托盘租赁-优胜必达 | 碳刷_刷握_集电环_恒压簧_电刷厂家-上海丹臻机电科技有限公司 | 温控器生产厂家-提供温度开关/热保护器定制与批发-惠州市华恺威电子科技有限公司 | 佛山市德信昌电子有限公司| 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 安平县鑫川金属丝网制品有限公司,防风抑尘网,单峰防风抑尘,不锈钢防风抑尘网,铝板防风抑尘网,镀铝锌防风抑尘网 | 一体化污水处理设备,一体化污水设备厂家-宜兴市福源水处理设备有限公司 | 手持式浮游菌采样器-全排二级生物安全柜-浙江孚夏医疗科技有限公司 | 无缝钢管-聊城无缝钢管-小口径无缝钢管-大口径无缝钢管 - 聊城宽达钢管有限公司 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 湖南印刷厂|长沙印刷公司|画册印刷|挂历印刷|台历印刷|杂志印刷-乐成印刷 | 隔爆型防爆端子分线箱_防爆空气开关箱|依客思 | 广东机电安装工程_中央空调工程_东莞装饰装修-广东粤标建设有限公司 | 北京软件开发_软件开发公司_北京软件公司-北京宜天信达软件开发公司 | 深圳活动策划公司|庆典策划|专业公关活动策划|深圳艺典文化传媒 重庆中专|职高|技校招生-重庆中专招生网 | 济南律师,济南法律咨询,山东法律顾问-山东沃德律师事务所 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 冲锋衣滑雪服厂家-冲锋衣定制工厂-滑雪服加工厂-广东睿牛户外(S-GERT) | 艺术涂料|木纹漆施工|稻草漆厂家|马来漆|石桦奴|水泥漆|选加河南天工涂料 | 定硫仪,量热仪,工业分析仪,马弗炉,煤炭化验设备厂家,煤质化验仪器,焦炭化验设备鹤壁大德煤质工业分析仪,氟氯测定仪 | 媒介云-全网整合营销_成都新闻媒体发稿_软文发布平台 |