unsigned int histogram[256]={0};
for(int i=0;i<width*height;i++)  //to iterate over pixels

const float cutoffpercentage = 0.05;  //To cut off lower and upper values
unsigned char lowerbound,upperbound;
unsigned int histAccu =0;  //pixel entries < 5% =lower bound
const unsigned int lowerpercentile = cutoffpercentage * width*height;
const unsigned int upperpercentile = (1-cutoffpercentage)*width*height;

for(int h =0;h <256;h++){
histAccu += histogram[h];

if(histAccu <= lowerpercentile){
lowerbound = h;

if(histAccu >= upperpercentile){
upperbound = h;

Then assign new gray values. Can I write this program to equalize the histogram of an image on a gpu shader memory and programs for image segmentation, blurring, sharpening etc...as a shader program using openGL ?? Can we write shader programs just like a normal application program in C ?? Kindly help me on this, as I'm new to shader programming.

  • $\begingroup$ Yes. But it isn't a direct mapping of C code to shader code. Everything you want to do is reasonably straight forward. Going through one of the tutorials for opengl/directx on the web is the best way to get started. That will tell what's involved and how to get everything setup. This site is best for specific questions. Doing a google search for "opengl tutorial" turns up good sites that will get you going. $\endgroup$
    – pmw1234
    May 12, 2021 at 18:10

1 Answer 1


Yes you can. There are basically two approaches:

  • You draw everything to a framebuffer object. Then you apply image processing fragment shaders to resulting texture of the previous draw call, where the shaders take the pervious texture with a sampler.

or if you have a particular image and you would like to have more control on the numeric precision, the number of neighbours available to you while working on pixel etc:

  • You apply compute shaders directly to image.

For the first approach, see Learn OpenGL, for compute shaders, see Anton's tutorial


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.