在图像中绘制多个矩形

时间:2019-02-09 18:55:38

标签: python opencv computer-vision

我正尝试在图像中定位矩形,并使用以前训练有素的分类器应用分类器来识别每个数字:

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

features = x_train[:8000, :, :]
labels = y_train[:8000]
list_hog_fd = []

for feature in features:
    fd = hog(feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False)
    list_hog_fd.append(fd)
hog_features = np.array(list_hog_fd, 'float64')


clf = LinearSVC()
clf.fit(hog_features, labels)

imPath = "/Users/alessandro/Downloads/prova prova(2).jpg"
im = cv2.imread(imPath)

# 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
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循环时,它都会生成空的“ roi”,并返回错误“错误:

OpenCV(4.0.0)/Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/resize.cpp:3784:错误:(-215:断言失败)!ssize函数'resize'中的.empty()“

在“ roi = cv2.resize(roi,(28,28),插值= cv2.INTER_AREA)”行。

# 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
    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()

如何修复代码?

P.s。供参考,请检查http://hanzratech.in/2015/02/24/handwritten-digit-recognition-using-opencv-sklearn-and-python.html

1 个答案:

答案 0 :(得分:0)

在复制代码时会发生这种情况。

解决步骤

1)如果您复制代码,请尝试了解正在发生的事情

2)意识到复制代码不是魔术。它根本不必在任何地方都可以工作

3)如果有错误,请搜索错误并尝试找出错误发生的原因

4)如果您从教程中复制代码后仍然无法使用,请不要在StackOverflow上询问

5)您的问题已经解决

# 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]

确定

roi

具有图像尺寸,并且其中包含任何像素。您可以通过简单地打印这样的形状来进行测试

print(im_th.shape)
# 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]
print(roi.shape)

您将看到发生了什么