Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python recognize every white color

import cv2
import sys
import numpy as np

def nothing(x):
    pass

# Load in image
image = cv2.imread('1.jpg')

# Create a window
cv2.namedWindow('image')

# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)

# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)

# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0

output = image
wait_time = 33

while(1):

    # get current positions of all trackbars
    hMin = cv2.getTrackbarPos('HMin','image')
    sMin = cv2.getTrackbarPos('SMin','image')
    vMin = cv2.getTrackbarPos('VMin','image')

    hMax = cv2.getTrackbarPos('HMax','image')
    sMax = cv2.getTrackbarPos('SMax','image')
    vMax = cv2.getTrackbarPos('VMax','image')

    # Set minimum and max HSV values to display
    lower = np.array([hMin, sMin, vMin])
    upper = np.array([hMax, sMax, vMax])

    # Create HSV Image and threshold into a range.
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower, upper)
    output = cv2.bitwise_and(image,image, mask= mask)

    # Print if there is a change in HSV value
    if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
        print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
        phMin = hMin
        psMin = sMin
        pvMin = vMin
        phMax = hMax
        psMax = sMax
        pvMax = vMax

    # Display output image
    cv2.imshow('image',output)

    # Wait longer to prevent freeze for videos.
    if cv2.waitKey(wait_time) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()
Comment

PREVIOUS NEXT
Code Example
Python :: from django.core.management import execute_from_command_line ImportError: No module named django.core.management 
Python :: if and else in python 
Python :: Flatten List in Python Using NumPy flat 
Python :: list to dictionary 
Python :: python check if ip is up or down 
Python :: binary list to decimal 
Python :: help() function in python 
Python :: how to change the name of a variable in a loop python 
Python :: check space in string python 
Python :: python timestamp to string 
Python :: x y coordinates in python 
Python :: python try catch print stack 
Python :: db connection string timeout 
Python :: web socket in python 
Python :: python string starts with any char of list 
Python :: SUMOFPROD1 
Python :: regex find all french phone number python 
Python :: histogram for categorical data with plotly 
Python :: django model different schema 
Python :: python decision tree classifier 
Python :: cookies in django 
Python :: Python 3 program to find factorial of given number 
Python :: python3 format leading 0 
Python :: combining strings in python 
Python :: python type annotations list of possible values 
Python :: what is iteration in python 
Python :: change python from 3.8 to 3.7 
Python :: excel with python 
Python :: long in python 
Python :: os dir exists 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =