简体   繁体   English

如何使用opencv / javacv识别图像中的U形?

[英]How to identify U shape in a image using opencv/javacv?

Currently I'am developing project on image processing on javacv. 目前我正在开发javacv上的图像处理项目。 In that I have to identify U shape inside a particular polygon. 在那里我必须识别特定多边形内的U形。

This are the two types of images and I have to identify whether image have two U shapes or single U shape in images. 这是两种类型的图像,我必须识别图像中的图像是否具有两个U形或单个U形。 I had went through many tutorials but I couldn't able to find proper guide line to clarify this. 我经历了很多教程,但我无法找到适当的指导方针来澄清这一点。 So please can expert person give some help to clarify this problem. 所以请专家可以帮助澄清这个问题。 Its really appreciate if you can provide some code example using opencv or javacv. 如果您可以使用opencv或javacv提供一些代码示例,我们非常感谢。

图像有两个U形

单U形图像

If all your images have similar pattern, you simply use aspect ratio (width / height) of the bounding rect of the contours to filter them out. 如果所有图像都具有相似的图案,则只需使用轮廓边界矩形的纵横比(宽度/高度)将其过滤掉即可。

ie, if you find bounding rect of all the contours, outer shape has an aspect_ratio close to 1. 即,如果找到所有轮廓的边界矩形,则外形的aspect_ratio接近1。

But U shape will have an aspect_ratio of more than 10. 但U形的aspect_ratio超过10。

Below is a python code : 下面是一个python代码:

import cv2
import numpy as np

img = cv2.imread('sofud.jpg')

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(gray,127,255,1)

contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    if 10 < w/float(h) or w/float(h) < 0.1:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)


cv2.imshow('res',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Below are the results : 以下是结果:

在此输入图像描述

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM