gpt4 book ai didi

python - 如何检测轮廓的位置

转载 作者:行者123 更新时间:2023-12-02 17:57:58 28 4
gpt4 key购买 nike

即时通讯与opencv和python相当新。我想制作一个程序,用相机对汽车进行计数。我已经编写了程序并找到了轮廓,但是问题是我不知道如何知道轮廓的位置在哪条车道上。我已经在contours on a 4 lane road下面使用油漆做了一个例子
在上图中,北有1辆车,南有2辆车,西有3辆车,东有2辆车。我想知道如何检测轮廓位置。谢谢

from picamera.array import PiRGBArray
from picamera import PiCamera
import picamera.array
import numpy as np
import time
import cv2
import RPi.GPIO as GPIO
import time
import sign2

GPIO.setmode(GPIO.BCM)


cam = PiCamera()
cam.resolution=(480,480)
cam.framerate=30
raw=PiRGBArray(cam, size=(480,480))

time.sleep(0.1)

colorLower = np.array([0,100,100])
colorUpper = np.array([179,255,255])

purplelow = np.array([125,100,100])
purpleup = np.array([136,255,255])

initvert = 0
inithoriz = 0
initverta = 0
inithoriza = 0




for frame in cam.capture_continuous(raw, format="bgr", use_video_port=True):
frame = frame.array
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

purplemask = cv2.inRange(hsv,purplelow,purpleup)

mask = cv2.inRange(hsv,colorLower,colorUpper)
mask = cv2.blur(mask,(3,3))
mask= cv2.dilate(mask,None,iterations=5)
mask= cv2.erode(mask,None,iterations=1)
mask= cv2.dilate(mask,None,iterations=3)

purplemask = cv2.inRange(hsv,purplelow,purpleup)
purplemask = cv2.blur(purplemask,(3,3))
purplemask= cv2.dilate(purplemask,None,iterations=5)
purplemask= cv2.erode(purplemask,None,iterations=1)
purplemask= cv2.dilate(purplemask,None,iterations=3)



me,thresh = cv2.threshold(mask,127,255,cv2.THRESH_BINARY)
me,thresh2 = cv2.threshold(mask,127,255,cv2.THRESH_BINARY)

cnts = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[-2]
center = None
cnts2 = cv2.findContours(thresh2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[-2]
center = None

vert = 0
horiz = 0
verta = 0
horiza = 0

最佳答案

要在代码中找到轮廓及其质心,请添加以下内容。重心列表存储在centroids

conts, hier = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
centroids = []

for c in conts[0:-1]:
x = int(sum(c[:,0,0]) / len(c))
y = int(sum(c[:,0,1]) / len(c))
centroids.append([x, y])
# if you want to display on original image
cv2.circle(frame, (x, y), 3, (255, 255, 0), -1)

关于python - 如何检测轮廓的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64303369/

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