Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何打印不同颜色对象的坐标?Python3 OpenCV_Python_Python 3.x_Opencv_Colors_Opencv3.0 - Fatal编程技术网

如何打印不同颜色对象的坐标?Python3 OpenCV

如何打印不同颜色对象的坐标?Python3 OpenCV,python,python-3.x,opencv,colors,opencv3.0,Python,Python 3.x,Opencv,Colors,Opencv3.0,我尝试在一个程序中检测3个蓝色物体和3个粉红色物体,然后打印物体中心的坐标。但不同中心的坐标是相同的或是非常奇怪的(我从一个值减去另一个值,这些差异需要进一步计算)!我不明白,为什么。终端输出为: 我使用Python3和opencv3。我的OC是Ubuntu 18.04。 请帮帮我。 我的代码(我导入了许多库,因为它是所有程序的一部分): 我初始化库 使用颜色遮罩(这里可能是我的错误) 第一部分。在这里,我检测到粉红色物体,并写下粉红色物体的坐标(这里可能我搞错了) 我的程序的第二部分与第一部分

我尝试在一个程序中检测3个蓝色物体和3个粉红色物体,然后打印物体中心的坐标。但不同中心的坐标是相同的或是非常奇怪的(我从一个值减去另一个值,这些差异需要进一步计算)!我不明白,为什么。终端输出为:

我使用Python3和opencv3。我的OC是Ubuntu 18.04。 请帮帮我。 我的代码(我导入了许多库,因为它是所有程序的一部分):

我初始化库 使用颜色遮罩(这里可能是我的错误) 第一部分。在这里,我检测到粉红色物体,并写下粉红色物体的坐标(这里可能我搞错了) 我的程序的第二部分与第一部分相同,但是对于蓝色(这里可能我有错误) 我关闭所有窗口并完成程序工作
请上传图片这是很多代码,请只粘贴相关的代码部分,这些代码通过自己调试无法正常工作。我上传了带有终端模块输出的图片,并在程序中编写了可能出错的地方
import time
from typing import Type
import cv2
import math
import numpy as np
import sys
from array import array
#from cv2 import DMatch
font = cv2.FONT_HERSHEY_SIMPLEX
cap = cv2.VideoCapture(0)
orig_stdout = sys.stdout
while True:
_, img = cap.read()

#converting frame(img i.e BGR) to HSV (hue-saturation-value)

hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

#definig the range of red color
red_lower=np.array([131,140,123],np.uint8)
red_upper=np.array([255,255,255],np.uint8)

#defining the Range of Blue color
blue_lower=np.array([24,166,182],np.uint8)
blue_upper=np.array([255,255,255],np.uint8)



#finding the range of red,blue and yellow color in the image
red=cv2.inRange(hsv, red_lower, red_upper)
blue=cv2.inRange(hsv,blue_lower,blue_upper)

#Morphological transformation, Dilation     
kernal = np.ones((5 ,5), "uint8")

red=cv2.dilate(red, kernal)
res=cv2.bitwise_and(img, img, mask = red)

blue=cv2.dilate(blue,kernal)
res1=cv2.bitwise_and(img, img, mask = blue)
#Tracking the Red Color
(_,contours,hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for i in range(0,3):
        for pic, contours[i] in enumerate(contours):
             area = cv2.contourArea(contours[i])
             if(area>1000):
                      x, y, w, h = cv2.boundingRect(contours[i])
                      moments = cv2.moments(contours[i])
                      dM01 = moments['m01']
                      dM10 = moments['m10']
                      dArea = moments['m00']
                      img = cv2.medianBlur(img, 5)  
                      if dArea > 1000:
                          img = cv2.drawContours(img, contours, i, (255, 105, 180), 3)
                          cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
                          cv2.putText(img, str(i), (x, y + h), font, 0.5, (0, 0, 128),2)
                          x = int(dM10 / dArea)
                          y = int(dM01 / dArea)
                          cv2.circle(img, (x, y), 10, (0, 0, 255), -1)
                          if i==0:
                              x0=x
                              y0=y

                          if i==1:
                              x1=x
                              y1=y

                          if i==2:
                              x2=x
                              y2=y
                              print(x0-x1,   x2-x1, y0-y1,  y2-y1)
#Tracking the Blue Color
(_,contours,hierarchy)=cv2.findContours(blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for j in range(0,3):
        for pic, contours[j] in enumerate(contours):
             area = cv2.contourArea(contours[j])
             if(area>1000):
                      x, y, w, h = cv2.boundingRect(contours[j])#описывающий прямоугольник
                      moments = cv2.moments(contours[j])
                      dM01 = moments['m01']
                      dM10 = moments['m10']
                      dArea = moments['m00']
                      img = cv2.medianBlur(img, 5)  
                      if dArea > 1000:
                          img = cv2.drawContours(img, contours, j, (20, 155, 100), 3)
                          cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
                          cv2.putText(img, str(j), (x, y + h), font, 0.5, (0, 0, 128),2)
                          x = int(dM10 / dArea)
                          y = int(dM01 / dArea)
                          cv2.circle(img, (x, y), 10, (0, 0, 255), -1)
                          if j==0:
                              x00=x
                              y00=y

                          if j==1:
                              x01=x
                              y01=y

                          if j==2:
                              x02=x
                              y02=y
                              print(x00-x01,   x02-x01, y00-y01,  y02-y01)                                  
cv2.imshow("Color Tracking",img)
if cv2.waitKey(10) & 0xFF == ord('q'):
        break
cam.release()
cv2.destroyAllWindows()