小编sca*_*sor的帖子

如何使用 Python OpenCV 裁剪图像上的每个字符?

我已经生成了这样的 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 > …
Run Code Online (Sandbox Code Playgroud)

python opencv image image-processing computer-vision

4
推荐指数
1
解决办法
1844
查看次数

AttributeError: 'numpy.ndarray' 对象没有属性 'save'

我有裁剪图像的短代码,我标记的文件夹中的所有图像都使用 opencv 保存为 csv,如下所示:

import os, sys
from PIL import Image
import cv2
import pandas as pd

# The annotation file consists of image names, text label, 
# bounding box information like xmin, ymin, xmax and ymax.
ANNOTATION_FILE = 'data/annot_crop_plate.csv'
df = pd.read_csv(ANNOTATION_FILE)

#image directory path
IMG_DIR = 'data/images'
# The cropped images will be stored here
CROP_DIR = 'data/crops'

files = df['filename']

size = (200,200)

for file in files:
    print(file)
    img = cv2.imread(IMG_DIR +'/' + file)
    annot_data = df[df['filename'] …
Run Code Online (Sandbox Code Playgroud)

python opencv pandas

3
推荐指数
1
解决办法
8420
查看次数