简体   繁体   English

如何使用 opencv 来识别和 label 形状

[英]How do I use opencv to identify and label shapes

I am trying to use opencv to create a rectangle around a cone.我正在尝试使用 opencv 在圆锥周围创建一个矩形。 Where I am currently at is I have outlined the code which has resulted in a triangle shape.我目前所处的位置是我已经概述了导致三角形的代码。 How can I use opencv to create a rectangle around the triangle.如何使用 opencv 在三角形周围创建一个矩形。

My code so far:到目前为止我的代码:

import cv2
import numpy as np

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

ret, mask = cv2.threshold(img[:, :,2], 235, 255, cv2.THRESH_BINARY)

mask3 = np.zeros_like(img)
mask3[:, :, 0] = mask
mask3[:, :, 1] = mask
mask3[:, :, 2] = mask

orange = cv2.bitwise_and(img, mask3)


cv2.imwrite("output.jpg", orange)

im = cv2.imread('output.jpg')

imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.imshow('img',im)

cv2.waitKey(0)
cv2.destroyAllWindows

Jpeg File: JPEG文件: 在此处输入图像描述

One approach is using multi-scale template matching一种方法是使用多尺度模板匹配

    1. You crop the object you want to find:您裁剪要查找的 object:

      在此处输入图像描述

    1. Apply Canny edge-detection to find the edges应用 Canny 边缘检测来查找边缘
    edged = cv2.Canny(resized, 50, 200)
    1. Find the matched template using matchTemplate使用matchTemplate查找匹配的模板
    result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)

Result:结果:

在此处输入图像描述

Code:代码:

import numpy as np
import imutils
import glob
import cv2

template = cv2.imread("template.jpg")
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
template = cv2.Canny(template, 50, 200)
(h, w) = template.shape[:2]

for imagePath in glob.glob("img2" + "/pXobJ.jpg"):
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    found = None

    for scale in np.linspace(0.2, 1.0, 20)[::-1]:
        resized = imutils.resize(gray, width=int(gray.shape[1] * scale))
        r = gray.shape[1] / float(resized.shape[1])

        if resized.shape[0] < h or resized.shape[1] < w:
            break

        edged = cv2.Canny(resized, 50, 200)
        result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
        (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)

        if found is None or maxVal > found[0]:
            found = (maxVal, maxLoc, r)

    (_, maxLoc, r) = found
    (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
    (endX, endY) = (int((maxLoc[0] + w) * r), int((maxLoc[1] + h) * r))

    cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
    cv2.imwrite("img2/out.jpg", image)
    print("Table coordinates: ({}, {}, {}, {})".format(startX, startY, endX, endY))
  • You can also use deep learning object detection with trained networks.您还可以对经过训练的网络使用深度学习 object 检测。

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

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