识别文档中的图像并自动删除i

2024-04-19 11:45:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的图像:

enter image description here

我可以使用什么算法从图像文本文档中识别图像?我想从文档中删除图像以降低文本提取错误率。我想分割图像。我不知道我能用什么方法来处理它。你知道吗?你知道吗


Tags: 方法文档图像文本算法文本文档错误率
1条回答
网友
1楼 · 发布于 2024-04-19 11:45:29

您可以遵循以下步骤:(代码注释中有描述)

namedWindow("Original_Image", cv::WINDOW_FREERATIO);
namedWindow("Result", cv::WINDOW_FREERATIO);
cv::Mat img = cv::imread("5ZKfM.png");
cv::Mat copy;   // this just for showing image
img.copyTo(copy);

// to gray
cv::Mat gray;
cvtColor(img, gray, cv::COLOR_BGR2GRAY);
cv::Mat binaryImg;
// threshold the img to get a binary image
threshold(gray, binaryImg, 80, 255, cv::THRESH_BINARY_INV);

cv::morphologyEx(binaryImg, binaryImg, cv::MORPH_CLOSE, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5)));

// Floodfill from point (0, 0)
cv::Mat im_floodfill = binaryImg.clone();
cv::floodFill(im_floodfill, cv::Point(0, 0), cv::Scalar(255));

// Invert floodfilled image
cv::Mat im_floodfill_inv;
bitwise_not(im_floodfill, im_floodfill_inv);
// Combine the two images to get the foreground.
cv::bitwise_or(im_floodfill_inv, binaryImg, binaryImg);

// find the contours
std::vector<std::vector<cv::Point> > contours;
cv::findContours(binaryImg, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

// get the largest contoure
cv::Rect rect;
for (std::vector<cv::Point> &contour : contours) {
    cv::Rect tempRect = cv::boundingRect(contour);
    if(tempRect.area() > rect.area()) {
        rect = tempRect;
    }
}

// get the sub mat of the picture from the original image
cv::Mat submatOriginal = img(rect);
// prepare the mask
cv::Mat submatBinary = binaryImg(rect);
// remove the picture from the image (set all pixels to white)
submatOriginal.setTo(cv::Scalar(255, 255, 255), submatBinary);

imshow("Result", img);
imshow("Original_Image", copy);
cv::waitKey();

结果如下:

enter image description here

<>注释:代码是C++,但可以按照步骤执行,并在Python中重新实现。你知道吗

相关问题 更多 >