From bd264ba1c58856fadda83481e886de264ba97dcf Mon Sep 17 00:00:00 2001 From: def <8384198-dettlaff@users.noreply.gitlab.com> Date: Wed, 1 Dec 2021 20:48:11 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=84=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ai/color.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 ai/color.py diff --git a/ai/color.py b/ai/color.py new file mode 100644 index 0000000..d27b550 --- /dev/null +++ b/ai/color.py @@ -0,0 +1,76 @@ +from __future__ import division +import cv2 +import numpy as np + +def nothing(*arg): + pass + +# Initial HSV GUI slider values to load on program start. +icol = (36, 202, 59, 71, 255, 255) # Green +#icol = (18, 0, 196, 36, 255, 255) # Yellow +#icol = (89, 0, 0, 125, 255, 255) # Blue +#icol = (0, 100, 80, 10, 255, 255) # Red +cv2.namedWindow('colorTest') +# Lower range colour sliders. +cv2.createTrackbar('lowHue', 'colorTest', icol[0], 255, nothing) +cv2.createTrackbar('lowSat', 'colorTest', icol[1], 255, nothing) +cv2.createTrackbar('lowVal', 'colorTest', icol[2], 255, nothing) +# Higher range colour sliders. +cv2.createTrackbar('highHue', 'colorTest', icol[3], 255, nothing) +cv2.createTrackbar('highSat', 'colorTest', icol[4], 255, nothing) +cv2.createTrackbar('highVal', 'colorTest', icol[5], 255, nothing) + +# Raspberry pi file path example. +frame = cv2.imread('box.png') +# Windows file path example. +#frame = cv2.imread('colour-circles-test.jpg') + +while True: + # Get HSV values from the GUI sliders. + lowHue = cv2.getTrackbarPos('lowHue', 'colorTest') + lowSat = cv2.getTrackbarPos('lowSat', 'colorTest') + lowVal = cv2.getTrackbarPos('lowVal', 'colorTest') + highHue = cv2.getTrackbarPos('highHue', 'colorTest') + highSat = cv2.getTrackbarPos('highSat', 'colorTest') + highVal = cv2.getTrackbarPos('highVal', 'colorTest') + + # Show the original image. + cv2.imshow('frame', frame) + + # Blur methods available, comment or uncomment to try different blur methods. + frameBGR = cv2.GaussianBlur(frame, (7, 7), 0) + #frameBGR = cv2.medianBlur(frameBGR, 7) + #frameBGR = cv2.bilateralFilter(frameBGR, 15 ,75, 75) + """kernal = np.ones((15, 15), np.float32)/255 + frameBGR = cv2.filter2D(frameBGR, -1, kernal)""" + + # Show blurred image. + cv2.imshow('blurred', frameBGR) + + # HSV (Hue, Saturation, Value). + # Convert the frame to HSV colour model. + hsv = cv2.cvtColor(frameBGR, cv2.COLOR_BGR2HSV) + + # HSV values to define a colour range. + colorLow = np.array([lowHue,lowSat,lowVal]) + colorHigh = np.array([highHue,highSat,highVal]) + mask = cv2.inRange(hsv, colorLow, colorHigh) + # Show the first mask + #cv2.imshow('mask-plain', mask) + + kernal = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7)) + mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernal) + mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernal) + + # Show morphological transformation mask + #cv2.imshow('mask', mask) + + # Put mask over top of the original image. + #result = cv2.bitwise_and(frame, frame, mask = mask) + + # Show final output image + cv2.imshow('colorTest', result) + + k = cv2.waitKey(5) & 0xFF + if k == 27: + break