第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Python OpenCV裁剪圖像上的每個字符?

如何使用Python OpenCV裁剪圖像上的每個字符?

Qyouu 2022-08-16 18:27:38
我生成了像這樣的OpenCV圖像從最后一行代碼中,如何分別裁剪和顯示當前圖像中的每個字符?法典    labels = measure.label(thresh, connectivity=2, background=0)    charCandidates = np.zeros(thresh.shape, dtype="uint8")    for label in np.unique(labels):        if label == 0:            continue        labelMask = np.zeros(thresh.shape, dtype="uint8")        labelMask[labels == label] = 255        cnts = cv2.findContours(labelMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)        cnts = imutils.grab_contours(cnts)        if len(cnts) > 0:            c = max(cnts, key=cv2.contourArea)            (boxX, boxY, boxW, boxH) = cv2.boundingRect(c)            aspectRatio = boxW / float(boxH)            solidity = cv2.contourArea(c) / float(boxW * boxH)            heightRatio = boxH / float(crop_frame.shape[0])            keepAspectRatio = aspectRatio < 1.0            keepSolidity = solidity > 0.15            keepHeight = heightRatio > 0.4 and heightRatio < 0.95        if keepAspectRatio and keepSolidity and keepHeight:            hull = cv2.convexHull(c)            cv2.drawContours(charCandidates, [hull], -1, 255, -1)    charCandidates = segmentation.clear_border(charCandidates)    cnts = cv2.findContours(charCandidates.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)    cnts = imutils.grab_contours(cnts)    cv2.imshow("Original Candidates", charCandidates)    thresh = cv2.bitwise_and(thresh, thresh, mask=charCandidates)    cv2.imshow("Char Threshold", thresh)謝謝。
查看完整描述

1 回答

?
汪汪一只貓

TA貢獻1898條經(jīng)驗 獲得超8個贊

這是一個簡單的方法:

  • 轉(zhuǎn)換為灰度

  • 大津的門檻

  • 查找等值線,從左到右對等值線進行排序,并使用等值線區(qū)域進行過濾

  • 提取投資回報率


在 Otsu 的閾值化以獲得二進制圖像之后,我們使用 imutils.contours.sort_contours() 從左到右對輪廓進行排序。這可確保當我們循環(huán)訪問每個輪廓時,每個字符的順序都正確。此外,我們使用最小閾值區(qū)域進行濾波,以消除小噪聲。這是檢測到的字符

http://img1.sycdn.imooc.com//62fb71430001de4103930154.jpg

我們可以使用Numpy切片提取每個字符。以下是每個已保存角色的投資回報率

http://img1.sycdn.imooc.com//62fb71520001ca8207250110.jpg

import cv2

from imutils import contours


# Load image, grayscale, Otsu's threshold

image = cv2.imread('1.png')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]


# Find contours, sort from left-to-right, then crop

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cnts = cnts[0] if len(cnts) == 2 else cnts[1]

cnts, _ = contours.sort_contours(cnts, method="left-to-right")


ROI_number = 0

for c in cnts:

    area = cv2.contourArea(c)

    if area > 10:

        x,y,w,h = cv2.boundingRect(c)

        ROI = 255 - image[y:y+h, x:x+w]

        cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)

        cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)

        ROI_number += 1


cv2.imshow('thresh', thresh)

cv2.imshow('image', image)

cv2.waitKey()


查看完整回答
反對 回復 2022-08-16
  • 1 回答
  • 0 關注
  • 223 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號