I'm using opencv right now to display different colours by masking everything but that colour. What I want to achieve is to get all the pixel coordinates that are green, black etc.
Some screenshots:
the first image is of a black line and the second image is of a green square. I would like to be able to record the pixel coordinates that have black or green on them. Here's the main code:
import sys
sys.path.append("\Python\Opencv_codes")
import line_following_testing as lf
from line_following_testing import lower_green as lg
from line_following_testing import upper_green as ug
from line_following_testing import lower_black as lb
from line_following_testing import upper_black as ub
import numpy as np
from time import sleep as wait
import cv2
from PIL import Image
green_boundaries = [
([75, 52, 60], [106, 255, 255])
]
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
lf.percentage_calculator(green_boundaries, "green", img)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#cropping and getting the hsv value and converting it to see of the value is their
mask = cv2.inRange(hsv, lg, ug)
green_result = cv2.bitwise_and(hsv, hsv, mask=mask)
#cropping and getting the hsv value and converting it to see of the value is their
mask1 = cv2.inRange(hsv, lb, ub)
black_result = cv2.bitwise_and(hsv, hsv, mask=mask1)
black_canny = cv2.Canny(black_result, 700,900)
cv2.imshow("green", green_result)
cv2.imshow('black', black_result)
cv2.imshow("hsv", hsv)
cv2.imshow('img', img)
cv2.imshow('black_canny', black_canny)
k = cv2.waitKey(30) & 0xff
if k==27:
break
and here is the imported script:
import numpy as np
from time import sleep as wait
import cv2
from PIL import Image
lower_green = np.array([75, 52, 60])
upper_green = np.array([106, 255, 255])
lower_black = np.array([0,0,0])
upper_black = np.array([180,255,45])
def percentage_calculator(boundaries, colour, image):
for(lower, upper) in boundaries:
lower = np.array(lower)
upper = np.array(upper)
# finds colors in boundaries a applies a mask
mask = cv2.inRange(image, lower, upper)
output = cv2.bitwise_and(image, image, mask = mask)
tot_pixel = image.size
pixel = np.count_nonzero(output)
percentage = round(pixel * 100 / tot_pixel, 2)
print(colour + " pixels: " + str(pixel))
print("Total pixels: " + str(tot_pixel))
print("Percentage of " + colour + " pixels: " + str(percentage) + "%")
New code:
import sys
sys.path.append("\Python\Opencv_codes")
import line_following_testing as lf
from line_following_testing import lower_green as lg
from line_following_testing import upper_green as ug
from line_following_testing import lower_black as lb
from line_following_testing import upper_black as ub
import numpy as np
from time import sleep as wait
import cv2
green_boundaries = [
([75, 52, 60], [106, 255, 255])
]
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
lf.percentage_calculator(green_boundaries, "green", img)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#cropping and getting the hsv value and converting it to see of the value is their
mask = cv2.inRange(hsv, lg, ug)
green_result = cv2.bitwise_and(hsv, hsv, mask=mask)
#cropping and getting the hsv value and converting it to see of the value is their
mask1 = cv2.inRange(hsv, lb, ub)
black_result = cv2.bitwise_and(hsv, hsv, mask=mask1)
x, y = mask1[-5:].nonzero()
x_min = min(x)
x_max = max(x)
y_min = min(y)
y_max = max(y)
center_coords_min = (x_min, y_min)
center_coords_max = (x_max, y_max)
cv2.circle(img, center_coords_min, 10, (0, 0, 255), 2)
cv2.circle(img, center_coords_max, 10, (255, 0, 0), 2)
print(x_min, y_min)
print(x_max, y_max)
black_canny = cv2.Canny(black_result, 700,900)
cv2.imshow("green", green_result)
cv2.imshow('black', black_result)
cv2.imshow("hsv", hsv)
cv2.imshow('img', img)
cv2.imshow('black_canny', black_canny)
k = cv2.waitKey(30) & 0xff
if k==27:
break