python读图片生成ROI并保存

先识别数字,后用数字命名,再保存

import cv2
import numpy as np
import matplotlib.pyplot as plt
import pytesseract
import os
from os.path import join

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"

config='--psm 6 --oem 1 -c tessedit_char_whitelist=0123456789'

path = r"D:\BUFFER\Pycharm\OpencvLearn\images\OCR"
determination = "images/ROI_Images"

def rotate_bound(image, angle):
    # grab the dimensions of the image and then determine the
    # center
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)

    # grab the rotation matrix (applying the negative of the
    # angle to rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])

    # compute the new bounding dimensions of the image
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))

    # adjust the rotation matrix to take into account translation
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY

    # perform the actual rotation and return the image
    return cv2.warpAffine(image, M, (nW, nH))


for filename in os.listdir(path):  # listdir的参数是文件夹的路径
    filenames = path + '\\' + filename
    # print(filenames)
    img_orig = cv2.imread(filenames, 1)
    # print(filenames)

    image_RGB = cv2.cvtColor(img_orig, cv2.COLOR_BGR2RGB)
    # cv2.rectangle(image_RGB, (1150,820),(1400,950),(0,0,255),1)
    ROI = image_RGB[820:950, 1165:1380]
    img = ROI.copy()
    # print(pytesseract.image_to_boxes(ROI))

    # cv2.imshow("roi", ROI)
    # cv2.waitKey(0)

    kernel = np.ones((5,5), np.uint8)
    open_img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

    # cv2.imshow("images", open_img)
    left_thred = 240
    gray_img = cv2.cvtColor(open_img,cv2.COLOR_RGB2GRAY)
    ret, thresh1 = cv2.threshold(gray_img, left_thred, 255, cv2.THRESH_BINARY)

    thresh1[0:20, 0:100] = 255
    thresh1[105:129, 0:210] = 255
    blur = cv2.blur(thresh1, (3,3))
    # cv2.imshow("thresh",thresh1)
    # cv2.waitKey(0)
    img_strings = pytesseract.image_to_string(blur)

    img_rotate = rotate_bound(img, -5)

    ROI = img_rotate[28:113, 15:220]

    strings = img_strings[:6]
    if len(strings) == 6:
        fileName = strings + ".jpg"
        # cv2.imwrite('images/ROI_Images/' + fileName,ROI)
        cv2.imwrite('images/rcnnROI/'+ fileName,ROI)

    print(img_strings[:6])

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值