Ask Your Question
0

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

asked 2018-05-05 02:48:39 -0600

salute838 gravatar image

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 image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-05-05 18:17:30 -0600

sjhalayka gravatar image

updated 2018-05-05 18:19:00 -0600

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)
edit flag offensive delete link more

Comments

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.

sjhalayka gravatar imagesjhalayka ( 2018-05-05 18:39:58 -0600 )edit

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

salute838 gravatar imagesalute838 ( 2018-05-06 03:56:53 -0600 )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])

img = mpimg.imread('0.jpg')
gray = rgb2gray(img)
plt.imshow(gray, cmap = plt.get_cmap('gray')) plt.show()

cv2.waitKey() cv2.destroyAllWindows()

salute838 gravatar imagesalute838 ( 2018-05-06 03:57:31 -0600 )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

salute838 gravatar imagesalute838 ( 2018-05-06 04:19:48 -0600 )edit

OK, best of luck to you. :)

sjhalayka gravatar imagesjhalayka ( 2018-05-06 11:01:27 -0600 )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 ...

StevenPuttemans gravatar imageStevenPuttemans ( 2018-05-08 08:27:10 -0600 )edit
1

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

sjhalayka gravatar imagesjhalayka ( 2018-05-08 08:48:15 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 10,826 times

Last updated: May 05 '18