I'm sorry for the image of Uncle Musai suddenly.
I tried, "Can I extract each of the four colored wooden balls of red, yellow, blue, and green?"
Both Python history and OpenCV history are about one month, so please point out my poor points (really).
The following is
hTarget
) of the specified hue (hue
)hMask2
)The code.
filters.py
...
def hueMask(src, dst, hue, hueRange):
src = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(src)
hOrg = h.copy()
hTarget = h.copy()
cv2.threshold(hTarget, hue + hueRange, hue, cv2.THRESH_TOZERO_INV, hTarget)
# 「src(x,y)If is greater than thresh, then dst(x,y)is 0"
# 0
# 0
# 0
# thresh
# src
# src
# src
# src
# src
cv2.threshold(hTarget, hue - hueRange, hue, cv2.THRESH_BINARY, hTarget)
# 「src(x,y)If is less than thresh, dst(x,y)is 0"
# 0(src)
# 0(src)
# 0(src)
# src
# src
# thresh
# 0
# 0
# 0
#result
# 0
# 0
# 0
# thresh
# src
# thresh
# 0
# 0
# 0
#To exclude fluorescent light (yellow) and hair (blue)
#Exclude extremely desaturated pixels from the target range
sNotVeryLow = s.copy() #Where the saturation is not extremely low
#If the saturation is higher than 31, put it in the target range (255).
#Otherwise the non-target range (0).
cv2.threshold(sNotVeryLow, 31, 255, cv2.THRESH_BINARY, sNotVeryLow)
#The logical product of hTarget and vNotHighlight is new hTarget
cv2.bitwise_and(hTarget, vNotVeryLow, hTarget)
#Make a copy of the brightness image.
vBrightened = v.copy()
#in addition+Apply 96 gamma correction to make it brighter
cv2.addWeighted(v, 0.625, v, 0.0, 96, vBrightened)
#Insert a gamma-corrected brightness image only in the target range of the brightness image
cv2.bitwise_and(vBrightened, 255, v, hTarget)
hMask2 = h.copy()
#When the corresponding pixel of hTarget (1 channel image) is 0
#Set the corresponding pixel of hMask2 (1 channel image) to 255.
#Otherwise, set it to 0.
#In short, hMask2 is an inverted version of the hTarget mask image.
cv2.compare(hTarget, 0, cv2.CMP_EQ, hMask2)
cv2.bitwise_and(s, 0, s, hMask2) #Logical AND
cv2.merge((hOrg, s, v), src)
cv2.cvtColor(src, cv2.COLOR_HSV2BGR, dst)
...
Somehow the name of the variable is dirty, but ... Now you can extract any hue.
Next, try to extract four colors, that is, the hues of red, yellow, blue, and green.
App.py
...
class App(object):
def __init__(self):
...
self._shouldHueMask = False
...
def run(self):
...
while self._windowManager.isWindowCreated:
...
if self._shouldHueMask:
filters.hueMask(frame, frame, self._hue, self._hueRange)
...
def onKeypress(self, keycode):
...
elif keycode == ord('B'): #Blue
self._hue = 110
self._hueRange = 10
self._shouldHueMask = \
not self._shouldHueMask
elif keycode == ord('G'): #Green
self._hue = 70
self._hueRange = 25
self._shouldHueMask = \
not self._shouldHueMask
elif keycode == ord('R'): #Red
self._hue = 5
self._hueRange = 5
self._shouldHueMask = \
not self._shouldHueMask
elif keycode == ord('Y'): #yellow
self._hue = 30
self._hueRange = 15
self._shouldHueMask = \
not self._shouldHueMask
...
if __name__=="__main__":
App().run()
The hue value and width of each of red, yellow, blue, and green were decided after trial and error.
Well, it seems that the skin color has reacted to the "red extraction". With this, it is not possible to extract the wooden ball "only".
Well, this time the ceiling has reacted to the "yellow extraction".
This time the hair and the black sweater have reacted to the "blue extraction".
I was able to extract the wooden balls most beautifully.
When compositing images, you stand in front of a green cloth called a green background. There is a reason why it was green.
The reason why green is soothing may be that the eyes feel comfortable with new stimuli, unlike the colors that appear in everyday life.
Maybe that's why the blackboard is green?
I will put the brush on it, leaving room for discussion.
I'm blogging: Weed.nagoya
Recommended Posts