gpt4 book ai didi

python-2.7 - 边缘检测图像中的对象识别?

转载 作者:行者123 更新时间:2023-12-02 17:43:09 25 4
gpt4 key购买 nike

请帮助我使用轮廓对边缘检测图像进行对象识别。这是我使用它的代码的一部分,我可以分离一些图像,但在大型详细图像中很难。我该如何修改这个

import numpy as np
import cv2

# load image
img = cv2.imread('res/test6.jpg', 1)

# convert the image to grayscale, blur it, and detect edges
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 35, 125)
height, width = edged.shape

# find contours of object
ret, thresh = cv2.threshold(edged, 127, 255, 0)
contours = cv2.findContours(thresh, 1, 2)
cnts = contours[1]
for cnt in cnts:
# find and draw a rectangle around object
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

# line parameter
x1 = x + w / 2
y1 = y + h
x2 = x + w / 2
y2 = height

# mark pixel depth with arrow
cv2.arrowedLine(img, (x2, y2), (x1, y1), (255, 0, 0), 4)
distance = (y2 - y1) * 0.03 + 4

cv2.putText(img, str(distance) + "m", (x1 + 5, y1 + 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 255)

print height, width

cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import numpy as np
import cv2

# load image
img = cv2.imread('res/test6.jpg', 1)

# convert the image to grayscale, blur it, and detect edges
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 35, 125)
height, width = edged.shape

# find contours of object
ret, thresh = cv2.threshold(edged, 127, 255, 0)
contours = cv2.findContours(thresh, 1, 2)
cnts = contours[1]
for cnt in cnts:
# find and draw a rectangle around object
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

# line parameter
x1 = x + w / 2
y1 = y + h
x2 = x + w / 2
y2 = height

# mark pixel depth with arrow
cv2.arrowedLine(img, (x2, y2), (x1, y1), (255, 0, 0), 4)
distance = (y2 - y1) * 0.03 + 4

cv2.putText(img, str(distance) + "m", (x1 + 5, y1 + 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 255)

print height, width

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

我想要的是限制识别对象。
img1
out1

最佳答案

从给定的代码中,我想您已经使用轮廓识别了您的对象。然后你用矩形绑定(bind)了这些轮廓。

现在更进一步。查找 质心 包围您刚刚获得的轮廓的矩形。测量从质心到图像底部的距离。

for c in cnts:
------# compute the center of the contour
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

------# draw the contour and center of the shape on the image
cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1)

------# show the image
cv2.imshow("Image", image)

我遇到了 THIS POST这对我有帮助。

为了更好地了解图像时刻是什么,我建议 THIS ARTICLE来自维基百科。

关于python-2.7 - 边缘检测图像中的对象识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41458304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com