Java – How to best detect different LED colors using Android phone’s camera and OpenCV?

How to best detect different LED colors using Android phone’s camera and OpenCV?… here is a solution to the problem.

How to best detect different LED colors using Android phone’s camera and OpenCV?

I’m trying to identify and distinguish the colors of LEDs on a black background using OpenCV on my Android phone, but I’m struggling at the moment. So far, my investigation has pointed to two different questions:

  1. With OpenCV, the camera seems to default to auto white balance, which makes it difficult to distinguish certain colors. When using the native Android camera, the best images seem to be produced with the white balance set to “cloudy”.
  2. OpenCV provides images in the RGB color space, but the distance between RGB and human-perceived colors does not match, which means that the Euclidean RGB distance metric is not the optimal solution (see How to compare two colors)。

So I have three questions:

  1. Is there a way in Android Java or OpenCV to set the camera’s white balance so that it affects the resulting image returned by OpenCV?
  2. If not, is there an algorithm available (preferably Java) to modify the white balance of OpenCV images?
  3. Is there an algorithm available (again, preferably in Java) to convert RGB colors to an alternative color space that is more in line with the distance between colors perceived by humans?

Thanks

Solution

My answer to question

1 (meaning I don’t need to answer question 2) is using the OpenCV Tutorial 3 code (camera control) (see ). OpenCV4Android samples) and modify the color effect method to allow setting the 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 know this works, I can 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

Related Problems and Solutions