# how can I convert blue color to white and any other color to black by using python

how can I convert blue color to white and any other color to black by using python especially if the blue in the image has different degree

edit retag close merge delete

Sort by ยป oldest newest most voted

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

if frame is None:
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)

more

You'll have difficulty getting the whitecaps to be detected because purely white pixels are counted as red for hue by default... same with those white boats.

( 2018-05-05 18:39:58 -0500 )edit

I have used many codes but the results were vary from one image to other for example...Kindly check my answer below

( 2018-05-06 03:56:53 -0500 )edit

import cv2 import numpy as np

from PIL import Image

import colorsys

import matplotlib.pyplot as plt import matplotlib.image as mpimg

def rgb2gray(rgb): return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

gray = rgb2gray(img)
plt.imshow(gray, cmap = plt.get_cmap('gray')) plt.show()

cv2.waitKey() cv2.destroyAllWindows()

( 2018-05-06 03:57:31 -0500 )edit

And you know I am trying to separate any color from the blue . As you can see there is a oil spill in the water and I am trying to give the spill clear, obvious color (green, red, black, etc ) which will be easy to calculate the total area of the pollution

( 2018-05-06 04:19:48 -0500 )edit

OK, best of luck to you. :)

( 2018-05-06 11:01:27 -0500 )edit

@sjhalayka thanks for the effort. @salute838, please in the future have at least a look at the documentation and the samples... this is a really basic issue that is explained very well there ...

( 2018-05-08 08:27:10 -0500 )edit
1

You're welcome! Thanks for the upvotes everyone. :)

( 2018-05-08 08:48:15 -0500 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2018-05-05 02:48:39 -0500

Seen: 5,903 times

Last updated: May 05 '18