Created
September 11, 2019 18:32
-
-
Save ninjakx/2ccbc043d4d0899f76930689e34df007 to your computer and use it in GitHub Desktop.
To do HSV masking with interactive control.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%capture | |
%matplotlib inline | |
from ipywidgets import interact, widgets | |
from IPython.display import display | |
import cv2 | |
from scipy import misc | |
import matplotlib.pyplot as plt | |
import numpy as np | |
def f(lower_H, lower_S, lower_V, upper_H, upper_S, upper_V, **kwargs): | |
hsv = cv2.cvtColor(racoon, cv2.COLOR_BGR2HSV) | |
lower_range = np.array([lower_H, lower_S, lower_V]) | |
upper_range = np.array([upper_H, upper_S, upper_V]) | |
mask = cv2.inRange(hsv, lower_range, upper_range) | |
ax_neu.imshow(mask, cmap="gray") | |
ax_neu.set_title(f"Lower Range: H = {lower_H}, S = {lower_S}, V = {lower_V}\ | |
\nUpper Range: H = {upper_H}, S = {upper_S}, V = {upper_V}") | |
fig.canvas.draw() | |
display(fig) | |
racoon = misc.face() | |
fig = plt.figure(figsize=(10, 8)) | |
ax_orig = fig.add_subplot(121) | |
ax_neu = fig.add_subplot(122) | |
ax_orig.imshow(racoon, cmap="gray") | |
ax_neu.imshow(racoon, cmap="gray") | |
print("Setting Lower and Upper Range for HSV") | |
interact(f, lower_H=widgets.IntSlider(min=1,max=255,step=1,value=105), | |
lower_S=widgets.IntSlider(min=1,max=255,step=1,value=130), | |
lower_V=widgets.IntSlider(min=1,max=255,step=1,value=110), | |
upper_H=widgets.IntSlider(min=1,max=255,step=1,value=225), | |
upper_S=widgets.IntSlider(min=1,max=255,step=1,value=205), | |
upper_V=widgets.IntSlider(min=1,max=255,step=1,value=255)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment