python - 检测和定位两个与文本有区别的图像中的区域(普通/曲线/旋转)
问题描述
我有两张相同的图片,唯一的区别是其中一张包含一些文字。我想围绕该文本制作一个边界框或多边形框。为此,我尝试比较这两个图像并测量差异区域,然后在不同区域或简单的文本区域周围制作一个边界框。
原始图像
文字+原图
before = cv2.imread('before.jpg')
after = cv2.imread('after.jpg')
# Convert images to grayscale
before_gray = cv2.cvtColor(before, cv2.COLOR_BGR2GRAY)
after_gray = cv2.cvtColor(after, cv2.COLOR_BGR2GRAY)
# Compute SSIM between two images
(score, diff) = compare_ssim(before_gray, after_gray, full=True)
print("Image similarity", score)
# The diff image contains the actual image differences between the two images
# and is represented as a floating point data type in the range [0,1]
# so we must convert the array to 8-bit unsigned integers in the range
# [0,255] before we can use it with OpenCV
diff = (diff * 255).astype("uint8")
# Threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
or c in contours:
area = cv2.contourArea(c)
if area > 40:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(before, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.rectangle(after, (x, y), (x + w, y + h), (36,255,12), 2)
问题是,它无法使用旋转文本和曲线形状文本的边界框进行捕获。请看下图:
比较结果
通缉
我想在文本周围实现适当的边界框或多边形框,无论它是旋转的还是曲线的。预期的演示输出如下所示:
预期结果
解决方案
推荐阅读
- excel - 范围被隐藏的行值填满,不知道为什么
- rabbitmq - 生产者如何知道将哪些数据发布到 RabbitMQ 中的队列?
- asp.net-core - 包含实体框架核心参考的清洁架构 UI 层
- r - R 上的 library() 错误消息,需要更新包吗?
- python - 如何使用 scrapy 从给定 URL 列表中下载 .csv 文件
- amazon-web-services - 多分支代码管道 im aws react.js 和 serverless,sam applicatiln
- ios - UItableView automaticDimension 不适用于自定义 UITableViewCell
- python - 设置火花上下文配置优先级火花提交
- python - 如何将树莓派的实时输出发送到我的外部计算机?
- node.js - Discord.js 触发时删除所有机器人和用户消息