Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I also have the code in C++. You should use the cvtColor function to convert from BGR to HSV. Then split the channels into separate arrays using the split function. Then loop through the H image, and if H > 90 and H < 130, then draw white on a black image. You'll get blue highlighted.

import cv2
import numpy

frame = cv2.imread('blue.jpg')

if frame is None:
    print('Error loading image')
    exit()

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

hsv_channels = cv2.split(hsv)

rows = frame.shape[0]
cols = frame.shape[1]

for i in range(0, rows):
    for j in range(0, cols):
        h = hsv_channels[0][i][j]

        if h > 90 and h < 130:
            hsv_channels[2][i][j] = 255
        else:
            hsv_channels[2][i][j] = 0

cv2.imshow("show", hsv_channels[0])
cv2.imshow("show2", hsv_channels[2])

cv2.waitKey(0)

I also have the code in C++. You should use the cvtColor function to convert from BGR to HSV. Then split the channels into separate arrays using the split function. Then loop through the H image, and if H > 90 and H < 130, then draw white on a black image. You'll get blue highlighted.

image description

import cv2
import numpy

frame = cv2.imread('blue.jpg')

if frame is None:
    print('Error loading image')
    exit()

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

hsv_channels = cv2.split(hsv)

rows = frame.shape[0]
cols = frame.shape[1]

for i in range(0, rows):
    for j in range(0, cols):
        h = hsv_channels[0][i][j]

        if h > 90 and h < 130:
            hsv_channels[2][i][j] = 255
        else:
            hsv_channels[2][i][j] = 0

cv2.imshow("show", hsv_channels[0])
cv2.imshow("show2", hsv_channels[2])

cv2.waitKey(0)