Dangthrimble
Dangthrimble

Reputation: 91

How can I best detect different LED colours using an Android phone's camera and OpenCV?

I am trying to identify and differentiate the colours of LEDs against a black background using OpenCV on an Android phone, but am currently struggling. My investigations to date point to two separate issues:

  1. Using OpenCV, the camera appears to default to automatic white balance which makes it difficult to differentiate between certain colours. Using the native Android camera the best images appear to be produced with a white balance set to "Cloudy".
  2. OpenCV provides images in the RGB colour space, yet RGB does not match the human-perceived distance between colours meaning a Euclidean RGB distance metric is not an optimum solution (see How to compare two colors).

Consequently I have three questions:

  1. Is there a way in Android Java or OpenCV to set the camera's white balance so that it influences the resultant image returned by OpenCV?
  2. If not, is there an algorithm available (preferably in Java) to modify the white balance of the OpenCV image?
  3. Is there an algorithm available (again, preferably in Java) to convert RGB colours to an alternative colour space that would better match the human-perceived distance between colours?

Thanks

Upvotes: 5

Views: 3289

Answers (3)

Dangthrimble
Dangthrimble

Reputation: 91

My answer to question 1 (meaning I didn't require an answer to question 2) was to use Using the OpenCV Tutorial 3 code (Camera Control) (see OpenCV4Android samples) and modify the colour effects methods to allow the setting of white balance:

public List<String> getWhiteBalanceList() {
    return mCamera.getParameters().getSupportedWhiteBalance();
}

public boolean isWhiteBalanceSupported() {
    return (mCamera.getParameters().getWhiteBalance() != null);
}

public String getWhiteBalance() {
    return mCamera.getParameters().getWhiteBalance();
}

public void setWhiteBalance(String whiteBalance) {
    Camera.Parameters params = mCamera.getParameters();
    params.setWhiteBalance(whiteBalance);
    mCamera.setParameters(params);
}

Once I knew that worked I was able to add a call to my main thread to set the correct white balance:

mOpenCvCameraView.setWhiteBalance(Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT);

In answer to question 3, I used the Java answer in Color Logic Algorithm.

Upvotes: 1

Hasan
Hasan

Reputation: 300

To identify different LED colors from black background, I propose the following:

  1. First, Using OpenCV's cvtColor function convert your RGB into HSV color space. Here HSV stands for Hue(such as red,green,yellow, blue etc.), Saturation(how saturated the color is), Value (lightness). After conversion for each RGB value you will have HSV value. Like RGB HSV has three channels H, S and V.

  2. Second, since you are interested in the LED's color, please analyze only the hue component to decide LED's Color. For example, you can threshold the Hue channel and decide which part of the image contains hue from lets say 10 to 25 and so on ... Plot/print the thresholded image to check your code.

Upvotes: 0

Andre Ahmed
Andre Ahmed

Reputation: 1889

  1. I think that's related to the device driver of the camera whether you can control it or not through Android's Camera API.
  2. This is useful for you: Detecting colors

I like electronics, so a simple Light dependant resistor would do that without any code :).

Upvotes: 0

Related Questions