Python 在图像中绘制多个矩形

Python 在图像中绘制多个矩形,python,opencv,computer-vision,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(featur

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

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:断言失败)!函数“resize”中的ssize.empty()

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

如何修复代码


请注意,作为参考,请选中复制代码时会发生这种情况

解决方案的步骤

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)

您将看到正在发生的情况

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

解决方案的步骤

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)

您将看到正在发生的情况

我确实正在加载一个映像,路径指的是当前磁盘上的映像@马丁尼如果是那样的话,你的形象会在途中受损。显示错误发生率在“roi=cv2.resize(roi,(28,28),interpolation=cv2.INTER_AREA)”处升高的那一行,但这是由于前面的几行造成的。我在这几行中闻到:#在数字周围制作矩形区域#,会损坏图像。打印这些值pt1:pt1+leng、pt2:pt2+leng并查看发生了什么。我想你是这样裁剪图像的,以至于你丢失了你的矩阵。重点是roi有形状(350,0),而im_th(571,926)我确实在加载图像,路径指的是当前在我磁盘上的图像@马丁尼如果是那样的话,你的形象会在途中受损。显示错误发生率在“roi=cv2.resize(roi,(28,28),interpolation=cv2.INTER_AREA)”处升高的那一行,但这是由于前面的几行造成的。我在这几行中闻到:#在数字周围制作矩形区域#,会损坏图像。打印这些值pt1:pt1+leng、pt2:pt2+leng并查看发生了什么。我认为你是这样裁剪图像的,以至于你失去了矩阵。关键是roi有形状(350,0),而im(571,926)