该图像色彩均衡算法逻辑错误

Faulty Logic in this image color equalization algorithm

本文关键字:错误 算法 图像 色彩      更新时间:2023-09-26

在我的代码的某个地方,我似乎做错了什么,我不能告诉哪一部分出错了。我已经打印到控制台,我从各种数组中得到的值,它似乎匹配。然后,当我运行均衡函数(维基百科-直方图均衡)时,我的输出图像接近全黑。我试图解释这个家伙的php到javascript只是为了测试一些东西,并发现我做了一个体面的工作。但我不是专家。

相关部分:

        function imageLoaded(ev) {
            element = document.getElementById("canvas1");
            c = element.getContext("2d");
            im = ev.target; // the image
            // read the width and height of the canvas
            width = element.width;
            height = element.height;
            // stamp the image on the left of the canvas:
            c.drawImage(im, 0, 0);
            // get all canvas pixel data
            imageData = c.getImageData(0, 0, width, height);
            w2 = width / 2;
            var reds = new Array();
            var greens = new Array();
            var blues = new Array();
            var freqR = new Array();
            var freqG = new Array();
            var freqB = new Array();
            if (imageData){
                buildHistograms(reds, greens,blues);
                buildFrequencies(reds, greens, blues, freqR, freqG, freqB);
            }
            var alpha = 255/(w2*height);
             // run through the image
            for (y = 0; y < height; y++) {
                inpos = y * width * 4; // *4 for 4 ints per pixel
                outpos = inpos + w2 * 4;
                for (x = 0; x < w2; x++) {

                    //reads pixel data(of img c)to each channel of rgb
                    r = imageData.data[inpos++];  
                    g = imageData.data[inpos++];
                    b = imageData.data[inpos++];
                    a = imageData.data[inpos++]; 
                    //using histogram eqalization formula: 
                    adjR = freqR[r]*alpha;
                    adjG = freqG[g]*alpha;
                    adjB = freqB[b]*alpha;

            //assigns pixel data of output image
                    imageData.data[outpos++] = adjR;
                    imageData.data[outpos++] = adjG;
                    imageData.data[outpos++] = adjB;
                    imageData.data[outpos++] = a;
                }
            }
            // put pixel data on canvas
            c.putImageData(imageData, 0,0);
        }
        im = new Image();
        im.onload = imageLoaded;
        im.src = "Lenna.png"; 

        function buildHistograms(reds,greens,blues){
            //run through image building histogram      
            for (y=0; y < height; y++){
                inpos = y * width *4;
                for (x=0; x < w2; x++){
                rd = imageData.data[inpos++]; 
                    g = imageData.data[inpos++];
                    b = imageData.data[inpos++];
                    a = imageData.data[inpos++];
                    // Add counts to our histogram arrays for each color.
                    reds.push(rd);
                    greens.push(g);
                    blues.push(b);
                }
            }
            // Sort them by keys into order
            reds.sort(function(a,b){return a-b});
            greens.sort(function(a,b){return a-b});
            blues.sort(function(a,b){return a-b});

        }
        function buildFrequencies(reds, greens, blues, freqR, freqG, freqB){
        // Build frequency charts for all colors: takes REDS GREENS BLUES from buildHistograms and places them on top of each other accordingly
            for(i=0; i<=255; i++){
                sumR=0;
                sumG=0;
                sumB=0;
                for(j=0; j<= i; j++){
                    if (reds[j]){sumR+=reds[j];}
                    if (greens[j]){sumG+=greens[j];}
                    if (blues[j]){sumB+=blues[j];}
                }
                freqR[i] = sumR;
                freqG[i] = sumG;
                freqB[i] = sumB;
            }
        }

看起来我的构建频率部分都错了。我这样修改它:

var len = reds.length;

            for (j=0; j < len; j++) {
                    var rCurrVal = reds[j];
                    var gCurrVal = greens[j];
                    var bCurrVal = blues[j];
                    if (freqR.hasOwnProperty(rCurrVal)) {
                        freqR[rCurrVal] += 1;
                    }   else {
                       freqR[rCurrVal] = 1;
                    } 
                    if (freqG.hasOwnProperty(gCurrVal)) {
                        freqG[gCurrVal] += 1;
                    }   else {
                       freqG[gCurrVal] = 1;
                    }
                    if (freqB.hasOwnProperty(bCurrVal)) {
                        freqB[bCurrVal] += 1;
                    }   else {
                       freqB[bCurrVal] = 1;
                    }
            }

            for (i=0; i<255; i++){   
                if ($.inArray(i,reds)===-1){freqR[i]=0;}
                if ($.inArray(i,greens)=== -1){freqG[i]=0;}
                if ($.inArray(i,blues)=== -1){freqB[i]=0;}
                if (i>0){
                    freqR[i]+=freqR[i-1];
                    freqG[i]+=freqG[i-1];
                    freqB[i]+=freqB[i-1];
                }
            }