python - Python:在自己的窗口中显示图像的每个对象
问题描述
我编写了一些代码,从图像中裁剪对象(在本例中为数据矩阵代码):
import numpy as np
import cv2
image = cv2.imread("datamatrixc.png")
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
img_height, img_width = image.shape[:2]
WHITE = [255, 255, 255]
# Threshold filter
ret, thresh = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY_INV)
# Get Contours
_, contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Get Last element of the contours object
max = len(contours) - 1
cnt = contours[max]
# Get coordinates for the bounding box
x, y, w, h = cv2.boundingRect(cnt)
image_region = image[ int(((img_height / 2) - h) / 2) : int(((img_height / 2) - h) / 2 + h), int(x): int(x + w) ]
dmc = cv2.copyMakeBorder(image_region, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value = WHITE)
cv2.imshow("Test", dmc)
cv2.waitKey(0)
cv2.destroyAllWindows()
代码工作正常,我收到了结果:
但是,下一个图像要复杂一些。我收到与上一张图片相同的结果,但我不知道如何检测另外两个对象。
每个对象都显示在其窗口中是否有更简单的方法?
解决方案
对于这个特定的图像,取你拥有的最大轮廓并检查对象是否是 4 边形状。如果边界框角之间的半点(见下面的对)在轮廓数组中,那么问题就解决了。
对:TopRight-TopLeft、TopRight-BottomRight、TopLeft-BottomLeft、BottomLeft-BottomRight
或者您可以检查边界框内是否有非黑色/白色的像素?
而对于策划个人来说,只需为你已经拥有的东西打一巴掌
推荐阅读
- java - Java线程组和线程本地
- node.js - 如何从 NodeJS 提升 Facebook 帖子
- javascript - 成功提交 ajax 后,我无法清除我的 textare 值
- linux - 如何在 windows server 2012 中增加打开文件限制
- system-verilog - SystemVerilog 忽略未使用的端口
- c# - iTrace 日志文件问题,截断为 640KB
- sql - 如何根据时间戳和值查找同步 ID
- ruby-on-rails - 我的控制器中的显示功能没有找到对象的创建者
- javascript - 创建一个后退按钮以在数组中使用
- docker - 在 docker 的入口点上运行命令输出没有文件错误