简体   繁体   English

使用 openCV 跳过不相关的轮廓以进行数字识别

[英]Skipping irrelevant contours for digit recognition using openCV

Extra contours getting populated:额外的轮廓被填充:
在此处输入图像描述

I am using the following code to perform the contouring on a given image我正在使用以下代码对给定图像执行轮廓

image = cv.imread('/content/drive/My Drive/Colab Notebooks/digit-recognition/test-2.jfif')
grey = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
grey = cv.GaussianBlur(grey,(5,5),0)
thresh = cv.adaptiveThreshold(grey,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY_INV,11,2)
contours, hierarchy = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

preprocessed_digits = []
for c in contours:
    x,y,w,h = cv.boundingRect(c)
    
    cv.rectangle(image, (x,y), (x+w, y+h), color=(0, 255, 0), thickness=2)
    digit = thresh[y:y+h, x:x+w]
    resized_digit = cv.resize(digit, (18,18))
    padded_digit = np.pad(resized_digit, ((5,5),(5,5)), "constant", constant_values=0)
    plt.imshow(padded_digit, cmap="gray")
    plt.show()
    xdigit = padded_digit.reshape(1,784)
    prediction = neigh.predict(xdigit)
    print("prediction = ",prediction[0])
print("\n\n\n----------------Contoured Image--------------------")
plt.imshow(image, cmap="gray")
plt.show()

This is the image I am using这是我正在使用的图像
在此处输入图像描述

How can I skip the unnecessary contours?如何跳过不必要的轮廓?
If I don't use Adaptive Thresholding then the contours are not detected at all properly due to light effects in this image.如果我不使用自适应阈值,则由于此图像中的光效应,轮廓根本无法正确检测到。 Although this contouring is虽然这种轮廓是
good as it detects the letters properly only thing is that it detects the noise areas too.很好,因为它可以正确检测到字母,唯一的问题是它也可以检测到噪音区域。

Experiments:实验:
I changed the blockSize in adaptive thresholding to 3 and the contouring appeared perfect:我将自适应阈值中的块大小更改为 3,轮廓看起来很完美:
在此处输入图像描述

Now I gave a different image with the same it produced the following contours现在我给出了一个不同的图像,它产生了以下轮廓
在此处输入图像描述

It is like it's making contours inside contours.就像在轮廓内制作轮廓一样。 That's a little confusing这有点令人困惑
because I thought RETR_EXTERNAL will prevent that.因为我认为 RETR_EXTERNAL 会阻止这种情况。

Another example:另一个例子:
在此处输入图像描述

The contouring for this appears fine.这个轮廓看起来很好。 But the images come like this但是图像是这样的在此处输入图像描述

I am not sure if because of the distortion of the image it's getting我不确定是否因为图像失真
predicted wrong.预测错了。 在此处输入图像描述

The easiest way would be to filter the detected bounding boxes by size, as all the noisy detections seem to be smaller than the ones you are looking for:最简单的方法是按大小过滤检测到的边界框,因为所有嘈杂的检测似乎都比您要查找的要小:

for c in contours:
    x,y,w,h = cv.boundingRect(c)    
    if w*h >= 200: 
        cv.rectangle(image, (x,y), (x+w, y+h), color=(0, 255, 0), thickness=2)
        digit = thresh[y:y+h, x:x+w]
        resized_digit = cv.resize(digit, (18,18))
        padded_digit = np.pad(resized_digit, ((5,5),(5,5)), "constant", constant_values=0)
        plt.imshow(padded_digit, cmap="gray")
        plt.show()
        xdigit = padded_digit.reshape(1,784)
        prediction = neigh.predict(xdigit)
        print("prediction = ",prediction[0])

Alternatively you can employ different filtering methods, eg by defining thresholds on the number of dark pixels that should appear in each bounding box (histogram) or contrast etc.或者,您可以使用不同的过滤方法,例如通过定义每个边界框(直方图)或对比度等中应出现的暗像素数量的阈值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM