比较两个图像并检测那些特征

时间:2017-01-23 15:23:18

标签: python opencv

我正在研究使用OpenCV和python进行“反向图像搜索”的图像处理 我的问题是我怎样才能找到图像中的各种物体。 我使用cv2.calchist来寻找图像和训练的特征,但是找到对象效果不好,它给我的结果更多是根据颜色而不是形状和特征

我的提取功能代码是:

image=cv2.imread(args["query"])
hsvimg = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

ret, thresh = cv2.threshold(hsvimg, 127, 255, cv2.THRESH_BINARY)
edged = cv2.Canny(hsvimg, 50, 100)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
                    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

features = []

(cnts, _) = contours.sort_contours(cnts)

maxArea = -1
#detect bigest ellipse
for c in cnts:
    if len(c) < 5:
        continue

    (ex, ey), (MA, ma), angle = cv2.fitEllipse(c)

    if angle == 0:
        continue

    ellipseArea = math.pi * MA * ma / 4

    if ellipseArea > maxArea:
        maxArea = ellipseArea


for c in cnts:
    if len(c) < 5:
        continue
    (ex, ey), (MA, ma), angle = cv2.fitEllipse(c)
    if angle == 0:
        continue

    ellipseArea = math.pi * MA * ma / 4
    if ellipseArea != maxArea:
        continue

    ellipse = cv2.fitEllipse(c)


    mask = np.zeros(image.shape[:2], dtype="uint8")
    cv2.ellipse(mask, ellipse, 255, -1)

    hist = cv2.calcHist([image], [0, 1, 2], mask, self.bins, [0, 180, 0, 256, 0, 256])
    hist = cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX).flatten()

    features.extend(hist)

return features

这段代码说下面的两个imeges是相同的

Image Of Sky

Image Of Car

0 个答案:

没有答案