python - 从 ROI 中提取图像 (OpenCV)

标签 python opencv text detection roi

给出以下代码(python)...

# Import the modules
import cv2
from sklearn.externals import joblib
from skimage.feature import hog
import numpy as np
from scipy import ndimage
import PIL
from PIL import Image

# Load the classifier
clf = joblib.load("digits_cls.pkl")

# Read the input image 
im = cv2.imread("C:\\Users\\Wkgrp\\Desktop\\test.jpg")

# Convert to grayscale and apply Gaussian filtering
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im_gray = cv2.GaussianBlur(im_gray, (5, 5), 0)

# Threshold the image
ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)

# Find contours in the image
image, ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Get rectangles contains each contour
rects = [cv2.boundingRect(ctr) for ctr in ctrs]


# For each rectangular region, calculate HOG features and predict
# the digit using Linear SVM.
for rect in rects:
    # Draw the rectangles
    cv2.rectangle(im, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3) 
    # Make the rectangular region around the digit
    leng = int(rect[3] * 1.6)
    pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
    pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
    roi = im_th[pt1:pt1+leng, pt2:pt2+leng]
    # Resize the image
    roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
    roi = cv2.dilate(roi, (3, 3))

    # Calculate the HOG features - Number Recognition (Not to print...)
    #roi_hog_fd = hog(roi, orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False)
    #nbr = clf.predict(np.array([roi_hog_fd], 'float64'))
    #cv2.putText(im, str(int(nbr[0])), (rect[0], rect[1]),cv2.FONT_HERSHEY_DUPLEX, 2, (0, 255, 255), 3)


#cv2.imshow("Resulting Image with Rectangular ROIs", im)
#cv2.waitKey()
#cv2.imwrite("C:\\Users\\Wkgrp\\Desktop\\crop\\img_with_ROI.jpg",im)
#cv2.imwrite("C:\\Users\\Wkgrp\\Desktop\\crop\\img_threshold.jpg",im_th)
cv2.imwrite("C:\\Users\\Wkgrp\\Desktop\\crop\\.jpg",roi)

print("NO ERRORS")

以及用于...的图像

Test Image

我可以执行 ROI 并保存它。问题是代码只保存第一个数字(可能是因为第 32 行的“for rects”)。 我必须修改什么才能保存所有识别的字符(周围有边界框)?

此外,请考虑示例图像中的 10 个。我必须将它们全部保存在一个文件夹中,每个文件夹都有不同的文件名(自动)。怎么做?

谢谢

最佳答案

这是响应请求的代码。 唯一的问题是它并不以特定的方式对字符进行排序,而是以它如何识别它们的方式排序。

# Import the modules
import cv2
from sklearn.externals import joblib
from skimage.feature import hog
import numpy as np
from scipy import ndimage
import PIL
from PIL import Image

# Load the classifier
clf = joblib.load("digits_cls.pkl")

# Read the input image 
im = cv2.imread("C:\\Users\\Bob\\Desktop\\causale.jpg")

# Convert to grayscale and apply Gaussian filtering
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im_gray = cv2.GaussianBlur(im_gray, (5, 5), 0)

# Threshold the image
ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)

# Find contours in the image
image, ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Get rectangles contains each contour
rects = [cv2.boundingRect(ctr) for ctr in ctrs]

idx =0

for ctr in ctrs:
    idx += 1
    x,y,w,h = cv2.boundingRect(ctr)
    roi=im[y:y+h,x:x+w]
    cv2.imwrite('C:\\Users\\Bob\\Desktop\\crop\\' + str(idx) + '.jpg', roi)
    #cv2.rectangle(im,(x,y),(x+w,y+h),(200,0,0),2)
    #cv2.imshow('img',roi)
    #cv2.waitKey(0) 

'''
# For each rectangular region, calculate HOG features and predict
# the digit using Linear SVM.
for rect in rects:
    # Draw the rectangles
    cv2.rectangle(im, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3) 
    # Make the rectangular region around the digit
    leng = int(rect[3] * 1.6)
    pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
    pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
    roi = im_th[pt1:pt1+leng, pt2:pt2+leng]
    # Resize the image
    roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
    roi = cv2.dilate(roi, (3, 3))

'''

    # Calculate the HOG features - Number Recognition (Not to print...)
    #roi_hog_fd = hog(roi, orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False)
    #nbr = clf.predict(np.array([roi_hog_fd], 'float64'))
    #cv2.putText(im, str(int(nbr[0])), (rect[0], rect[1]),cv2.FONT_HERSHEY_DUPLEX, 2, (0, 255, 255), 3)


#cv2.imshow("Resulting Image with Rectangular ROIs", im)
#cv2.waitKey()
#cv2.imwrite("C:\\Users\\Bob\\Desktop\\crop\\img_with_ROI.jpg",im)
#cv2.imwrite("C:\\Users\\Bob\\Desktop\\crop\\img_threshold.jpg",im_th)
#cv2.imwrite("C:\\Users\\Bob\\Desktop\\crop\\.jpg",roi)

print("NO ERRORS")

关于python - 从 ROI 中提取图像 (OpenCV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44902376/

相关文章:

python - 我的 CSS 和图像没有显示 Django

python - OpenCV 透视变换给出意想不到的结果

matlab - 如果 MATLAB Rb2020 中的行和列维度彼此不一致,如何取消嵌套具有嵌套数据和文本内容的元胞数组?

python - 如何在Python中打印异常?

python - 选择指定位置具有最高整数值的子列表

python-3.x - 如何减慢使用 cv2 捕获的视频?

php - 从 TXT 文件中提取随机行作为字符串

java - 如何使用像素着色器实现流畅的文字?

python - 如何使用Pycharm在SQL Server中运行SQL?

使用 H264 (mp4) 编解码器的 OpenCV 流捕获 CAM