<p>Yes you can. There are basically two approaches:</p>
<ul>
<li>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.</li>
</ul>
<p>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:</p>
<ul>
<li>You apply compute shaders directly to image.</li>
</ul>
<p>For the first approach, see <a href="https://learnopengl.com/Advanced-OpenGL/Framebuffers" rel="nofollow noreferrer">Learn OpenGL</a>, for compute shaders, see <a href="https://antongerdelan.net/opengl/compute.html" rel="nofollow noreferrer">Anton's tutorial</a></p>

CC BY-SA 4.0
2021-05-22T17:46:25.067Z
2021-05-22T17:46:25.067Z
Kaan E.
<pre><code>unsigned int histogram[256]={0};
for(int i=0;i&lt;width*height;i++)  //to iterate over pixels
  ++histogram[image[i]];

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

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

if(histAccu &lt;= lowerpercentile){
lowerbound = h;
continue;
}

if(histAccu &gt;= upperpercentile){
upperbound = h;
break;
}
}
</code></pre>
<p>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.</p>

CC BY-SA 4.0
2021-05-12T18:10:45.613Z
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.
pmw1234
<opengl><shader><c++><image-processing><fragment-shader>
Can I write histogram equalization, blurring, sharpening grayscale image etc.. as shader programs with openGL?
Meganathan
# Can I write histogram equalization, blurring, sharpening grayscale image etc.. as shader programs with openGL?

(Asked by: username_0 on 2021-05-11)

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

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;
continue;
}

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

```

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.

**Tags:** opengl, shader, c++, image-processing, fragment-shader

> **Comments:**
> **username_1**: 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.  

---

## 1 answers

### Answer by username_2 on 2021-05-22. Score: 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](https://learnopengl.com/Advanced-OpenGL/Framebuffers), for compute shaders, see [Anton's tutorial](https://antongerdelan.net/opengl/compute.html)


---

