【问题标题】:Draw multiple rectangles in an image在图像中绘制多个矩形
【发布时间】:2019-02-09 18:55:38
【问题描述】:

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

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:Assertion failed) !ssize函数'resize'中的.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

【问题讨论】:

    标签: python opencv computer-vision


    【解决方案1】:

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

    解决步骤

    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)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多