从图像中检测非文本数据:Python

时间:2019-03-07 04:24:31

标签: python opencv

enter image description here有什么方法可以从具有文本的图像中提取非文本数据?我有一个形象,假设有一个带有文本以及签名和徽标的字母。我只想提取标志和徽标,或者删除文本上的所有内容。有什么办法吗? 预先感谢。

2 个答案:

答案 0 :(得分:3)

您可以执行以下步骤:(重要部分在代码中注释

cv::namedWindow("result", cv::WINDOW_FREERATIO);
cv::Mat img = cv::imread(R"(wMHGk.png)");

// to gray
cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
// threshold to for segmentation
cv::threshold(gray, gray, 150, 255, cv::THRESH_BINARY_INV);

// connect the text components (here it is 21 you can change it)
cv::dilate(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(21, 3)));
// remove noise
cv::erode(gray, gray, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)));

// last result (leaving only logo and sign)
cv::Mat result(img.size(), CV_8UC3);
result.setTo(cv::Scalar(255, 255, 255));

std::vector<std::vector<cv::Point> > contours;
cv::findContours(gray, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
for (unsigned int i(0); i<contours.size(); ++i) {
    cv::Rect rect = cv::boundingRect(contours[i]);
    if(rect.height > 20) { // only detect the logo and the sign, (here 20 also you can change it)
        cv::Mat submat1 = result(rect);
        cv::Mat submat2 = img(rect);
        submat2.copyTo(submat1);
        cv::rectangle(img, rect, cv::Scalar(0, 0, 255), 3);
    }
}

cv::imshow("result1", img);
cv::imshow("result2", result);

cv::waitKey();

结果为1:

enter image description here

这是结果2:清洁图像

enter image description here

注意:该代码是C ++,您可以按照以下步骤操作,并在Python中重新实现。

答案 1 :(得分:1)

python解决方案:

import cv2
image = cv2.imread("test.png", 1)
img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU,img)
cv2.bitwise_not(img,img)
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 5))
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, rect_kernel)
im2, contours, hier = cv2.findContours(img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

if len(contours) != 0:
    for c in contours:
        x,y,w,h = cv2.boundingRect(c)
        if(h>20):
            cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),1)

cv2.imshow("Result", image)
cv2.waitKey(0)

结果:

enter image description here