Q:

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

Currently I'am developing project on image processing on javacv. In that I have to identify U shape inside a particular polygon. 
This are the two types of images and I have to identify whether image have two U shapes or single U shape in images. 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.

A:

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.
But U shape will have an aspect_ratio of more than 10.
Below is a python code :
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 :

