Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 366
Dangthrimble Avatar asked Nov 12 '22 08:11

Dangthrimble


1 Answers

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.

like image 91
Dangthrimble Avatar answered Nov 14 '22 21:11

Dangthrimble