gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:29:19 24 4
gpt4 key购买 nike

我有下面的图片:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com