opencv - 从图像中提取对象
问题描述
我已经使用inRange
函数删除了图像中除了两个对象之外的所有组件。有什么方法可以从图像中单独提取对象?我尝试使用HoughCircle
但得到的结果非常不一致。
没有 inRange 功能(原图)
解决方案
cv::findContours
似乎很适合这里:
img = cv.imread('images/suawk.png')
search = cv.dilate(img, cv.getStructuringElement(cv.MORPH_RECT, (5,5)))
search = cv.cvtColor(search, cv.COLOR_BGR2GRAY)
contours, _ = cv.findContours(search, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
bboxes = [cv.boundingRect(c) for c in contours]
fig, axes = plt.subplots(1, sum(rect[2]*rect[3] > 250 for rect in bboxes))
fig.set_size_inches([12,3])
fig.tight_layout()
figi = 0
for i in range(len(contours)):
rect = cv.boundingRect(contours[i])
area = rect[2] * rect[3]
if area < 250:
continue
obj = img[rect[1]:rect[1]+rect[3]+1, rect[0]:rect[0]+rect[2]+1, :]
obj = cv.cvtColor(obj, cv.COLOR_BGR2RGB)
axes[figi].imshow(obj)
figi += 1
fig.show()
我在 findContours 之前做了一次膨胀,这样我得到的轮廓碎片更少。我丢弃所有边界框小于 250px 的轮廓以减少噪点。
推荐阅读
- arrays - 使用 C 对数组进行排序
- python - 如何解释使用 timeit 的结果?
- flask - 尝试在端口 5000 上运行时找不到 URL''
- typescript - 在 commonjs 和 ejs 之间共享文件,Typescript 不是 commonjs 文件上的 module.ts(2306)
- c++ - 如何解决来自 stb_image 的这些编译器警告?
- c# - 使用字符串从字符串中删除文本以查找文本?
- r - 在 R-Shiny 中悬停在 dygraph 上时如何显示定性值?
- html - 如何在 HTML 中使用 iframe 进行此布局
- html - UL 课程不像我希望的那样工作
- ruby-on-rails - 如何在 Rails 中将其他变量传递给 CarrierWave 上传器?