3

I want to implement a Histogram Equalization filter in my android app with opengl es.I traverse all texture coordinates in my fragment shader to find out the mapping array.But seems like all the value in these arrays are 1 which cause the wrong output fragment color.Does someone know why?

fragment shader code

precision mediump float;

uniform sampler2D inputTexture;
uniform float texelWidth;
uniform float texelHeight;
uniform int width;
uniform int height;

in mediump vec2 textureCoordinate;

out vec4 fragColor;

float sR[256];
float sG[256];
float sB[256];

void main() {

if(textureCoordinate.xy==vec2(0.,0.)){
    int nR[256];
    int nG[256];
    int nB[256];
    int i,j;
    for (i=0;i<=height;i++){
        float currentHeight=texelHeight*float(i);
        for(j=0;j<=width;j++){
            vec3 current=texture(inputTexture,vec2(texelWidth*float(j),currentHeight)).rgb;
            int r=int(round(current.r*255.));
            int g=int(round(current.g*255.));
            int b=int(round(current.b*255.));
            nR[r]+=1;
            nG[g]+=1;
            nB[b]+=1;
        }
    }
    /*
    calculateS(nR,sR);
    calculateS(nG,sG);
    calculateS(nB,sB);*/

    int k,nSum;
     for(k=0,nSum=0;k<256;k++){
         nSum+=nR[k];
     }

     float p[256];

     for(k=0;k<256;k++){
         p[k]=float(nR[k])/float(nSum);
     }

     int l;
     for(k=0;k<256;k++){
         sR[k]=0.;
         for(l=0;l<=k;l++){
             sR[k]+=p[l];
         }
     }

    //
    for(k=0,nSum=0;k<256;k++){
        nSum+=nG[k];
    }

    float q[256];

    for(k=0;k<256;k++){
        q[k]=float(nG[k])/float(nSum);
    }

    for(k=0;k<256;k++){
        sG[k]=0.;
        for(l=0;l<=k;l++){
            sG[k]+=q[l];
        }
    }


    //
     for(k=0,nSum=0;k<256;k++){
         nSum+=nB[k];
     }

     float h[256];

     for(k=0;k<256;k++){
         h[k]=float(nB[k])/float(nSum);
     }

     for(k=0;k<256;k++){
         sB[k]=0.;
         for(l=0;l<=k;l++){
             sB[k]+=h[l];
         }
     }
}

vec3 current=texture(inputTexture,textureCoordinate.xy).rgb;

int r=int(round(current.r*255.));
int g=int(round(current.g*255.));
int b=int(round(current.b*255.));

fragColor=vec4(sR[r],sG[g],sB[b],1.);

}

Community
  • 1
  • 1
吴高翔
  • 31
  • 1

0 Answers0