Skip to content Skip to sidebar Skip to footer

Simple Method To Extract Specific Color Range From An Image In Python?

I'm trying to extract a specific color from an image within a defined RGB range using the cv2 module. In the example below I am trying to isolate the fire from the exhaust of the s

Solution 1:

It was a pretty simple issue; you gave a larger color before a smaller one to cv2.inRange, so there was no valid intersection! Here's some working code that shows the output. This should be easy to adapt into your own script.

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('shuttle.jpg')   # you can read in images with opencv
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

hsv_color1 = np.asarray([0, 0, 255])   # white!
hsv_color2 = np.asarray([30, 255, 255])   # yellow! note the order

mask = cv2.inRange(img_hsv, hsv_color1, hsv_color2)

plt.imshow(mask, cmap='gray')   # this colormap will display in black / white
plt.show()

enter image description here

Post a Comment for "Simple Method To Extract Specific Color Range From An Image In Python?"