Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Select right color to threshold and image with opencv

import numpy as np 
import cv2

# load image
img = cv2.imread("Eding.png")
# convert to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 
# set lower and upper color limits
lower_val = np.array([50,100,170])
upper_val = np.array([70,255,255])
# Threshold the HSV image to get only green colors
mask = cv2.inRange(hsv, lower_val, upper_val)
# apply mask to original image - this shows the green with black blackground
only_green = cv2.bitwise_and(img,img, mask= mask)

# create a black image with the dimensions of the input image
background = np.zeros(img.shape, img.dtype)
# invert to create a white image
background = cv2.bitwise_not(background)
# invert the mask that blocks everything except green -
# so now it only blocks the green area's
mask_inv = cv2.bitwise_not(mask)
# apply the inverted mask to the white image,
# so it now has black where the original image had green
masked_bg = cv2.bitwise_and(background,background, mask= mask_inv)
# add the 2 images together. It adds all the pixel values, 
# so the result is white background and the the green from the first image
final = cv2.add(only_green, masked_bg)

#show image
cv2.imshow("img", final)
cv2.waitKey(0)
cv2.destroyAllWindows()
Comment

PREVIOUS NEXT
Code Example
Python :: z3 symbolic expressions cannot be cast to concrete boolean values 
Python :: how to deploy django app on heroku with mongodb 
Python :: ring Trace library usage to pass an error 
Python :: Hiding and encrypting passwords in Python using advpass() module 
Python :: ring create an application to ask the user about his/her name. 
Python :: python list insert multiple 
Python :: Use miraculous with enviroment variable token 
Python :: purge python3.y from every place in my path 
Python :: how to create dataframe from rdd 
Python :: insertar valor python 
Python :: how to execute more than one line of code in one line python 
Python :: We want to estimate the cost of painting a property. Interior wall painting cost is Rs.18 per sq.ft. and exterior wall painting cost is Rs.12 per sq.ft. 
Python :: Find dataframe column values containing a certain string 
Python :: alternatives for appending to numpy array 
Python :: print single pixel from numpy 
Python :: python print string in red color 
Python :: KMeans 
Python :: python notification image 
Python :: pls work 
Python :: jupyter notebook do not show matplotlib text above plot 
Python :: affinity propagation cosine similarity python 
Python :: tuples in python 
Python :: discard method in python 
Python :: where is memory and register in python python 
Python :: plotly garden wing map 
Python :: custom save method django 
Python :: import all files on the same directory python 
Python :: Classe wrapper en python 
Python :: How to Add Elements to a dictionary using the update() method 
Python :: page views count django 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =