简体   繁体   English

如何使用Android手机的相机和OpenCV最好地检测不同的LED颜色?

[英]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. 我试图在Android手机上使用OpenCV来识别和区分LED的颜色与黑色背景,但我目前正在努力。 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. 使用OpenCV,相机似乎默认为自动白平衡,这使得难以区分某些颜色。 Using the native Android camera the best images appear to be produced with a white balance set to "Cloudy". 使用原生Android相机,最佳图像似乎是在白平衡设置为“阴天”的情况下生成的。
  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 ). OpenCV在RGB颜色空间中提供图像,但RGB与人类感知的颜色之间的距离不匹配,这意味着欧几里德RGB距离度量不是最佳解决方案(请参阅如何比较两种颜色 )。

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? 在Android Java或OpenCV中是否有办法设置摄像机的白平衡,以便影响OpenCV返回的结果图像?
  2. If not, is there an algorithm available (preferably in Java) to modify the white balance of the OpenCV image? 如果没有,是否有可用的算法(最好用Java)来修改OpenCV图像的白平衡?
  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? 是否有可用的算法(再次,最好是用Java)将RGB颜色转换为可以更好地匹配人类感知颜色之间距离的替代颜色空间?

Thanks 谢谢

  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. 我认为这与相机的设备驱动程序有关,无论你是否可以通过Android的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 :). 我喜欢电子产品,因此一个简单的光源相关电阻器可以在没有任何代码的情

To identify different LED colors from black background, I propose the following: 为了从黑色背景中识别不同的LED颜色,我提出以下建议:

  1. First, Using OpenCV's cvtColor function convert your RGB into HSV color space. 首先,使用OpenCV的cvtColor函数将RGB转换为HSV颜色空间。 Here HSV stands for Hue(such as red,green,yellow, blue etc.), Saturation(how saturated the color is), Value (lightness). 这里HSV代表Hue(例如红色,绿色,黄色,蓝色等),饱和度(颜色是多么饱和),值(亮度)。 After conversion for each RGB value you will have HSV value. 转换每个RGB值后,您将获得HSV值。 Like RGB HSV has three channels H, S and V. 像RGB HSV有三个通道H,S和V.

  2. Second, since you are interested in the LED's color, please analyze only the hue component to decide LED's Color. 其次,由于您对LED的颜色感兴趣,请仅分析色调组件以确定LED的颜色。 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. 例如,您可以对Hue通道进行阈值处理,并确定图像的哪个部分包含色调,例如10到25,依此类推......绘制/打印阈值图像以检查代码。

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: 我对问题1的回答(意思是我不需要回答问题2)是使用OpenCV Tutorial 3代码(Camera Control)(参见OpenCV4Android样本 )并修改颜色效果方法以允许设置白平衡:

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 . 在回答问题3时,我在Color Logic Algorithm中使用了Java答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM