python - 识别文档中的图像并自动将其删除的算法

标签 python image opencv image-processing

我有下面的图片:

enter image description here

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

最佳答案

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

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中重新实现它。

关于python - 识别文档中的图像并自动将其删除的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56861632/

相关文章:

python - 排序元素(排列)的数量,在列表的所有可能排列中

html - 如何使用 HTML 渲染图像,使用 Flutter 创建 iOS-App 的 CSS?

python - OpenCV HSV 值根据屏幕/相机上的位置而变化

javascript - jQuery 图像 slider 问题

ios - 尝试在已经工作的项目中使用 OpenCV 框架时 Apple O-Linker 出错

c++ - 图片对齐-匹配模板

python - 如何将相同类别的列置于 Pandas 数据框中的单个标题下?

python - 激活或权重的丢失

python - 为类似于 R 的模块、函数、类生成单个 HTML 帮助页面

python - Django 通过 iOS 上传图片时图像旋转不正确(EXIF 问题)