How to print coordinate of objects with diiferent colors? Python3 OpenCV

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to print coordinate of objects with diiferent colors? Python3 OpenCV



I try to detect 3 objects of blue colore and 3 objects of pink colore in one program, after that print coordinate of object's center. But coordinates of different centers are identical or real strange(I subtracted from one value another, these differences are needed for further calculations)! I CAN'T understand,why. Terminal output that:enter image description here.



I use Python3 and OpenCV 3. My OC is Ubuntu 18.04.
Help me pleace.
My code(I import many libs becouse it is a part of all programm):



I initialize the libraries


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



work with mask for colors (HERE MAYBE I HAVE MISTAKE)


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)



The first part. Here I detect pink object and write coordinates of pink object (HERE MAYBE I HAVE MISTAKE)


#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)



The second part of my programm is the same with first, but for blue color (HERE MAYBE I HAVE MISTAKE)


#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)



I close all windows and finish work of programm


cv2.imshow("Color Tracking",img)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()





please upload the image
– Ishara Madhawa
Aug 13 at 4:09





Thats a lot of code, Please paste only relevant sections of code which are not working as expected by debugging yourself.
– ZdaR
Aug 13 at 6:25





I uploaded image with output of terminal
– Sandra Archipova
Aug 13 at 8:50





I made modules in programm and wrote where i can make mistake
– Sandra Archipova
Aug 13 at 8:55









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard