gpt4 book ai didi

python - 在图像中绘制多个矩形

转载 作者:行者123 更新时间:2023-12-02 16:37:25 30 4
gpt4 key购买 nike

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

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”,并返回错误“error:

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

在“roi = cv2.resize(roi, (28, 28), interpolation=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()

如何修复代码?

附:如需引用,请查看 http://hanzratech.in/2015/02/24/handwritten-digit-recognition-using-opencv-sklearn-and-python.html

最佳答案

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

解决步骤

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)

你会看到发生了什么

关于python - 在图像中绘制多个矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54609607/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com