2

画像の N 個の支配的な色を調べる必要があります。これに触発された関数を作成しました:http://bubble.ro/How_to_create_the_histogram_of_an_image_using_PHP.html

絵は、絵と同じ表面を持つ隠しキャンバスの中に描かれています。

これが私のコードです:

lwf.image.getDominantColors = function(args) {

    // args.imageData is the ImageData object got from the canvas 2d context.
    // canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height)

    if(!args.imageData) alert('lwf::image::getDominantColors() : no ImageData provided.');

    // args.sampling defines the percentage of pixels to be tested (0.01 for example).

    var sampling = undefined != args.sampling && 0 > args.sampling && args.sampling <= 1
      ? args.sampling
      : lwf.image.DFLT_PX_SAMPLING_RATIO; // Default value

    var w = args.imageData.width,
        h = args.imageData.height,
        total = w * h,
        mat = args.imageData.data;

    // Interval allows me to skip pixels on X and Y axis to make the computation faster.
    // The top-left pixel of each interval * interval square is tested.

    var interval = Math.round(Math.sqrt(1 / sampling));

    var histogram = new Array();

    var x = 0, y = 0, // Coordinates of the tested pixel.
        i = 0, j = 0; // Use to get the number of tested pixels.

    for(i = 0, x = 0; x < w; ++i, x += interval) {

        for(j = 0, y = 0; y < h; ++j, y += interval) {

            var start = (y * w + x) << 2;

            var r = mat[start    ],
                g = mat[start + 1],
                b = mat[start + 2];

            var value = Math.round((r + g + b) / 3);

            // TODO
        }
    }

    var tested = i * j;

    // TODO

    return histogram;

}; // lwf::image::getDominantColors()

見つかった色ごとに、その存在を表す値を示す PHP 配列に相当するものを返すように、これを完了する方法がわかりません。

そして別の質問: 次の式は何を表していますか?

var value = Math.round((r + g + b) / 3);

r = 200、g = 100、b = 100 と r = 100、g = 100、b = 200 がある場合、両方とも同じ値になりますが、2 つの異なる色になります。私が見つけたチュートリアルには、これに関する説明はありません。

誰にもアイデアはありますか?前もって感謝します :)

4

0 に答える 0