在图像中查找特定颜色

时间:2017-12-02 14:24:15

标签: javascript jquery html image

我有这样的图像:

Image 1

并希望在图片中找到特定的颜色(#ffa400 / #ffffff)并获取其百分比出现值(橙色可能为:75%,白色:25%

  

注意:请注意,我不想像Color Thief那样获得平均颜色(在这种情况下:橙色),但是我喜欢找到SPECIFIC颜色。

下面是一个片段,所以这就是我到目前为止所做的一切,但代码似乎并不起作用。我的想法似乎也不是一个有效的& 快速方式。



function extract_colors(img) {
    var canvas = document.createElement("canvas");
    var c = canvas.getContext('2d');
    c.width = canvas.width = img.width;
    c.height = canvas.height = img.height;
    c.clearRect(0, 0, c.width, c.height);
    c.drawImage(img, 0, 0, img.width, img.height);
    return getColors(c);

}

function getColors(c) {
    var col, colors = {};
    var pixels, r, g, b, a;
    r = g = b = a = 0;
    pixels = c.getImageData(0, 0, c.width, c.height);
    for (var i = 0, data = pixels.data; i < data.length; i + = 4) {
        r = data[i];
        g = data[i + 1];
        b = data[i + 2];
        a = data[i + 3];
        if (a < (255 / 2))
            continue;
        col = rgbToHex(r, g, b);
        if (col == 'ffa400') { // find color #ffa400
            if (!colors[col])
                colors[col] = 0;
            colors[col] + +;
        }
    }
    return colors;
}


function rgbToHex(r, g, b) {
    return ((r << 16) | (g << 8) | b).toString(16);
}

var img = document.getElementById('img');
out.innerHTML = extract_colors(img)
&#13;
<img id='img' width=100 src='https://i.stack.imgur.com/EPDlQ.png'>

<p id='out'>...</p>
&#13;
&#13;
&#13;

  

编辑1.0

我试图使用这样的代码,但它似乎也没有用。

var orangeMatches=0, whiteMatches=0
        Jimp.read("https://i.stack.imgur.com/EPDlQ.png").then(function (image) {
            image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, idx) {
              var red   = this.bitmap.data[ idx + 0 ];
              var green = this.bitmap.data[ idx + 1 ];
              var blue  = this.bitmap.data[ idx + 2 ];
              var alpha = this.bitmap.data[ idx + 3 ];

              if (red == 255 && green == 164 && blue == 0 && alpha == 1){
                orangeMatches++;
              }
              if (red == 255 && green == 255 && blue == 255 && alpha == 1){
                whiteMatches++;
              }
          });
          console.log(orangeMatches, whiteMatches)
        });
  

编辑2.0

我希望有一些宽容价值(10%),这样就不会计算每种颜色,但匹配在一起的颜色(例如#ffa400#ffa410)会一起计算。< / p>

1 个答案:

答案 0 :(得分:5)

一些注释

图片中的主色实际上是ffa500,而不是ffa400。实际上根本没有ffa400的出现。

另请注意,这是一个消除锯齿的位图图形。在视觉上我们只看到两种颜色,但是为了使从一种颜色到另一种颜色的过渡平滑,产生了一组“中间颜色”。换句话说,ffffff和ffa500的总和不会完全是100%。 (这就是为什么下面我的函数的输出将显示比所讨论的两个颜色更多的颜色)

解决方案1 ​​

此解决方案的基本思想是获取图片中的每种颜色,将这些颜色存储在一个共同的对象中,在该对象中计算每种颜色的出现次数。返回此对象后,我们可以稍后计算输出中某些特定颜色的出现次数。

这将返回如下内容:

{
    ffa500: 7802,
    ffa501: 4,
    ffa502: 2,
    ...,
    ffffff: 1919,
    total: 10000
}

现在您可以通过这种方式访问​​给定颜色的出现位置:

var imageData = extract_colors(img),
    white = imageData.ffffff,
    total = imageData.total;

要显示给定颜色覆盖的图像的百分比,请执行以下操作(toFixed()只是将值限制为两位小数):

whitePct = (white / total * 100).toFixed(2);

工作样本

function getColors(ctx) {

		// Get the canvas data
    var pixels = ctx.getImageData(0, 0, ctx.width, ctx.height),
        data = pixels.data,
        
        // Set up our output object to collect color data
        output = {};
    
    // For each color we encounter, check the
    // output object. If the color already exists
    // there, simply increase its counted value.
    // If it does not, create a new key.
    for (var i = 0; i < data.length; i+=4) {
        var r = data[i],
            g = data[i + 1],
            b = data[i + 2],
            col = rgbToHex(r, g, b);

        if( output[col] )
        	output[col]++
        else
        	output[col] = 1
    }
    
    // Count total
    var total = 0;
    for(var key in output) {
    	total = total + parseInt(output[key])
    }
    output.total = total;
    
    // Return the color data as an object
    return output;
}

// Our elements
var img = document.getElementById('img'),
	  out = document.getElementById('out');

// Retrieve the image data
var imageData = extract_colors(img),

    // Count our given colors
    white = imageData.ffffff,
    orange = imageData.ffa500,
    total = imageData.total,

    // Calculate percentage value
    whitePct = (white / total * 100).toFixed(2),
    orangePct = (orange / total * 100).toFixed(2);
  
out.innerHTML = `White: ${whitePct}%<br/>Orange: ${orangePct}%`;

// See the console for all colors identified
console.log(extract_colors(img))

// ----- These functions are left untouched ----- \\

function extract_colors(img) {
    var canvas = document.createElement("canvas");
    var c = canvas.getContext('2d');
    c.width = canvas.width = img.width;
    c.height = canvas.height = img.height;
    c.clearRect(0, 0, c.width, c.height);
    c.drawImage(img, 0, 0, img.width, img.height);
    return getColors(c);
}

function rgbToHex(r, g, b) {
    return ((r << 16) | (g << 8) | b).toString(16);
}
p, img {
 margin: 10px 5px;
 float: left
}
<!-- Image converted to base64 to keep it locally availible -->
<img id='img' width=100 src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJlAEFkb2JlIEltYWdlUmVhZHlxyWU8AAADImlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4wLWMwNjAgNjEuMTM0Nzc3LCAyMDEwLzAyLzEyLTE3OjMyOjAwICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbn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uSgaji1FvdXh6hBBZ/GRB1dY1mqaFgJGiE+nIaQEnS3TEk1GAKFLDBEgtB8h8t1yBcluKGZK7E3Mo2pkHu0XQ9b/HVh5F/2vfjg3yGjhfyRj2G1Aj7GRsQiyKqhnA7I6qIdICiEhAYMHhASlwMgOpEeIeCioYPwI8F5EOtB9FN0O9rF2SS+o6/aGHi0r7sG2rjyLoL03LbT61ZxjZMwHpEeIIFKyjItPQffejxsLCeE5wiwrYAgJCRhCQgKGkJCAISQkUAD+C1sX7Gj8A6l6AAAAAElFTkSuQmCC'>
<p id='out'>...</p>

解决方案2

(考虑这个解决方案草案。如果要用于实时网站,可能需要进行一些调整,但至少它应该有效!)

这里的想法是用我们想要映射为参数的颜色调用extract_colors()。构建一个公共对象来保存输出,并为每种颜色填充一个对象。颜色对象由计数器和rgb值组成。使用isNeighborColor()函数,rgb值不仅用于匹配特定颜色,还用于匹配与其相近的任何颜色。根据需要调整公差等级。 (理想情况下,这是我猜的另一个论点)。

调用指定图像的函数以及计算出现次数的颜色:

var imageData = extract_colors(img, 'ffffff', 'ffa500'),

这将输出每种颜色的对象:

{
  "ffffff": {
    "counter": 1979,
    "rgb": {
      "r": 255,
      "g": 255,
      "b": 255
    }
  },
  "ffa500": {
    "counter": 7837,
    "rgb": {
      "r": 255,
      "g": 165,
      "b": 0
    }
  },
  "total": 9816
}

我们想要的是访问counter值:

var white = imageData.ffffff.counter;

工作样本

// Added a rest parameter for unlimited color input
function extract_colors(img, ...colors) {
    var canvas = document.createElement("canvas");
    var c = canvas.getContext('2d');
    c.width = canvas.width = img.width;
    c.height = canvas.height = img.height;
    c.clearRect(0, 0, c.width, c.height);
    c.drawImage(img, 0, 0, img.width, img.height);
    return getColors(c, ...colors);
}

// Unchanged
function rgbToHex(r, g, b) {
    return ((r << 16) | (g << 8) | b).toString(16);
}

// From: https://stackoverflow.com/a/5624139/2311559
function hexToRgb(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}

// From: https://stackoverflow.com/a/11506531/2311559
// Slightly modified
function isNeighborColor(color1, color2, tolerance) {
    tolerance = tolerance || 32;
    return Math.abs(color1.r - color2.r) <= tolerance
        && Math.abs(color1.g - color2.g) <= tolerance
        && Math.abs(color1.b - color2.b) <= tolerance;
}

function getColors(ctx, ...colors) {

		var pixels = ctx.getImageData(0, 0, ctx.width, ctx.height),
        data = pixels.data,
        output = {};
    
    // Build an 'output' object. This will hold one object for
    // each color we want to check. The color objects will 
    // each consist of a counter and an rgb value. The counter 
    // is used to count the occurrence of each color. The rgb 
    // value is used to match colors with a certain tolerance 
    // using the 'isNeighborColor()' function above.
    for (var i = 0; i < colors.length; i++) {
        output[colors[i]] = {
        	counter: 0,
          rgb: hexToRgb(colors[i])
        };
    }

    // For each pixel, match its color against the colors in our
    // 'output' object. Using the 'isNeighborColor()' function 
    // we will also match colors close to our input, given the 
    // tolerance defined.
    for (var i = 0; i < data.length; i+=4) {
        var r = data[i],
            g = data[i + 1],
            b = data[i + 2],
            colobj = {r, g, b},
            col = rgbToHex(r, g, b);

        Object.keys(output).map(function(objectKey, index) {
            var rgb = output[objectKey].rgb,
            		count = output[objectKey].counter;
            if(isNeighborColor(rgb, colobj)) {
            	output[objectKey].counter = count+1;
            }
        });

    }
    
    // Count total
    var total = 0;
    for(var key in output) {
    	total = total + parseInt(output[key].counter)
    }
    output.total = total;
    
    // Return the color data as an object
    return output;
}

// Our elements
var img = document.getElementById('img'),
	  out = document.getElementById('out');

// Retrieve the image data
var imageData = extract_colors(img,'ffffff','ffa500'),

    // Count our given colors
    white = imageData.ffffff.counter,
    orange = imageData.ffa500.counter,
    total = imageData.total,

    // Calculate percentage value
    whitePct = (white / total * 100).toFixed(2),
    orangePct = (orange / total * 100).toFixed(2);

out.innerHTML = `White: ${whitePct}%<br/>Orange: ${orangePct}%`;

// Optional console output
console.log(extract_colors(img,'ffffff','ffa500'));
p, img {
 margin: 10px 5px;
 float: left
}
<!-- Image converted to base64 to keep it locally availible -->
<img id='img' width=100 src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJlAEFkb2JlIEltYWdlUmVhZHlxyWU8AAADImlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4wLWMwNjAgNjEuMTM0Nzc3LCAyMDEwLzAyLzEyLTE3OjMyOjAwICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M1IE1hY2ludG9zaCIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDo4QUUwNzk4N0I2QjIxMUUzQUQzMkIzMTA4REQ0Nzc1MiIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDo4QUUwNzk4OEI2QjIxMUUzQUQzMkIzMTA4REQ0Nzc1MiI+IDx4bXBNTTp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