简体   繁体   English

使用 OpenCV.JS 进行颜色检测

[英]Color detection with OpenCV.JS

I use OpenCV for developing in Javascript.我使用 OpenCV 在 Javascript 中进行开发。

I am trying to detect variations of orange RGB color between (255, 80, 0) and (255, 170, 0) in any image.我正在尝试检测任何图像中橙色 RGB 颜色在 (255, 80, 0) 和 (255, 170, 0) 之间的变化。

I was trying to do the same as here https://docs.opencv.org/3.3.1/db/d64/tutorial_js_colorspaces.html , but it not working when I changed from black detection to orange detection.我试图在这里做同样的事情https://docs.opencv.org/3.3.1/db/d64/tutorial_js_colorspaces.html ,但是当我从黑色检测更改为橙色检测时它不起作用。

I tried the following code and many others combinations, but have got unexpected.我尝试了以下代码和许多其他组合,但出乎意料。

So, how to choose the limits for ORANGE color?那么,如何选择橙色的界限呢?

Here goes the javascript code:这是 javascript 代码:

let lower = [230, 155, 0, 0];
let higher = [255, 195, 25, 255];
let src = cv.imread('canvasInput');
let dst = new cv.Mat();
let low = new cv.Mat(src.rows, src.cols, src.type(), lower);
let high = new cv.Mat(src.rows, src.cols, src.type(), higher);
cv.inRange(src, low, high, dst);
cv.imshow('canvasOutput', dst);
src.delete(); dst.delete(); low.delete(); high.delete(); 

This is the unexpected result这是意想不到的结果

If orange is about [255, 170, 0] in RGB space I would suggest to widen your range around that:如果橙色在 RGB 空间中约为 [255, 170, 0],我建议扩大您的范围:

let lower = [230, 155, 80, 0];
let higher = [255, 195, 120, 255];

In your settings your orange needs to have exactly R=255 and B=0, that is not going to happen in real images.在您的设置中,您的橙色需要恰好具有 R=255 和 B=0,这在真实图像中是不会发生的。 I do not know what the fourth parameter selects (alpha channel?) so it is all in range.我不知道第四个参数选择什么(alpha 通道?)所以它都在范围内。

Here is a demo with OpenCV JS where you can select a target color range from an image you like. 这是OpenCV JS的演示,您可以从喜欢的图像中选择目标颜色范围。

https://alexcpn.github.io/opencvjsdemo.html https://alexcpn.github.io/opencvjsdemo.html

Update : This link is broken 更新:此链接已断开

Output below 输出如下

在此处输入图片说明

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

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