我正试图从图像中获取数字轮廓。 原始图片位于number_img:
我使用了以下代码后:
gray = cv2.cvtColor(number_img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (1, 1), 0)
ret, thresh = cv2.threshold(blur, 70, 255, cv2.THRESH_BINARY_INV)
img2, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
area = cv2.contourArea(c)
[x, y, w, h] = cv2.boundingRect(c)
if (area > 50 and area < 1000):
[x, y, w, h] = cv2.boundingRect(c)
cv2.rectangle(number_img, (x, y), (x + w, y + h), (0, 0, 255), 2)
由于中间有小盒子,我试图用高度来限制:
if (area > 50 and area < 1000) and h > 50:
[x, y, w, h] = cv2.boundingRect(c)
cv2.rectangle(number_img, (x, y), (x + w, y + h), (0, 0, 255), 2)
我还应该采取哪些其他方法来获得最佳的数字轮廓来进行OCR?
感谢。
答案 0 :(得分:3)
刚刚在Matlab中尝试过。希望您可以将代码调整为OpenCV并调整一些参数。目前尚不清楚最正确的blob是否为数字。
img1 = imread('DSYEW.png');
% first we can convert the grayscale image you provided to a binary
% (logical) image. It is always the best option in image preprocessing.
% Here I used the threshold .28 based on your image. But you may change it
% for a general solution.
img = im2bw(img1,.28);
% Then we can use the Matlab 'regionprops' command to identify the
% individual blobs in binary image. 'regionprops' gives us an output, the
% Area of the each blob.
s = regionprops(imcomplement(img));
% Now as you did, we can filter out the bounding boxes with an area
% threshold. I used 350 originally. But it can be changed for a better
% output.
s([s.Area] < 350) = [];
% Now we draw each bounding box on the image.
figure; imshow(img);
for k = 1 : length(s)
bb = s(k).BoundingBox;
rectangle('Position', [bb(1),bb(2),bb(3),bb(4)],...
'EdgeColor','r','LineWidth',2 )
end
输出图片:
更新1:
刚才更改了上面代码中的area参数,如下所示。不幸的是我的Mac上没有Python OpenCV。但是,这完全是关于调整代码中的参数。
s([s.Area] < 373) = [];
输出图片:
更新2:
上图中的数字3和4被检测为一位数。如果仔细观察,你会发现3和4相互连接,这就是为什么上面的代码将它检测为一个数字。所以我使用imdilate
函数来摆脱它。接下来,在您的代码中,甚至某些数字内的白洞也被检测为数字。为了消除这种情况,我们可以使用Matlab中的imfill
来填补漏洞。
更新的代码:
img1 = imread('TCXeuO9.png');
img = im2bw(img1,.28);
img = imcomplement(img);
img = imfill(img,'holes');
img = imcomplement(img);
se = strel('line',2,90);
img = imdilate(img, se);
s = regionprops(imcomplement(img));
s([s.Area] < 330) = [];
figure; imshow(img);
for k = 1 : length(s)
bb = s(k).BoundingBox;
rectangle('Position', [bb(1),bb(2),bb(3),bb(4)],...
'EdgeColor','r','LineWidth',2 )
end
输出图片: