<p>Here's an example on how to do this with Python, Numpy and Matplotlib.
Image on the right has been averaged (row by row), while the left is the original.</p>

<p><a href="https://i.sstatic.net/V4wFH.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/V4wFH.png" alt="Before-After-Edited"></a></p>

<p><strong>EDITED</strong>: As suggested by @Nathan Reed, I avoided flipping colors when row average is 0. Rows with an average of 0 now get a value of 0.5 (128). The dark image encountered previously was due to values higher than 1 (255) due to scaling, that were represented as white in matplotlib's image renderer. I fixed this by setting the max value in any pixel to be 1.</p>

<pre class="lang-python prettyprint-override"><code>import numpy as np
from matplotlib import image as mpimg
from matplotlib import pyplot as plt

def rgb2gray(rgb):
    return np.dot( rgb[...,:3], [0.299, 0.587, 0.144] )

imgPath = "C:/myImg.jpg"

im = mpimg.imread( imgPath )
gs = rgb2gray( im )

averaged = gs.copy()

for row in averaged:
    rowAvg = sum( row ) / len( row )
    if rowAvg == 0:
        row = 0.5
    else:
        # Scale each row by ratio between 128 and current row avg
        row *= 0.5 / rowAvg

    # Make sure that no pixel has a higher value than 1
    if rowAvg &gt; 0: row[row&gt;1] = 1

plt.subplot( 1, 2, 1 ), plt.imshow( gs,       'gray' )
plt.subplot( 1, 2, 2 ), plt.imshow( averaged, 'gray' )

plt.show()
</code></pre>

<p>Here's another test with a simple gradient image (left is the original):
<a href="https://i.sstatic.net/DhczR.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/DhczR.png" alt="Before-After2"></a></p>

CC BY-SA 3.0
2015-12-24T21:30:50.837Z
Something looks wrong with your first image pair...the result is mostly complete black, which doesn't seem correct. Also, it would be good to handle divide-by-zero so rows that are complete black in the input don't flip out. Maybe just replace the row by all 128s in that case?
Nathan Reed
CC BY-SA 3.0
2015-12-24T22:20:57.027Z
Same algorithm for 1st and 2nd img. Will check though, makes sense about the flipping, thanks.
TLousky
CC BY-SA 3.0
2016-01-01T07:45:53.923Z
The above solution seems correct at first glance but what if user enter the output image as input then it shows distorted output. The solution should be adaptive and learn when not to perform calculation as the output of first input is the solution so when output is again input no changes should be seen.
Sandiip
CC BY-SA 3.0
2016-01-01T18:16:25.310Z
@SandeepNeupane that makes sense, although that's not what the author of the question asked. I'm sure this answer can serve as the basis for a more adaptive, complete solution.
TLousky
CC BY-SA 3.0
2015-12-24T14:41:00.023Z
2015-12-25T22:48:07.470Z
2015-12-25T22:48:07.470Z
TLousky
<p>Suppose i have a 400*300 gray scale image.Now I want to scan each line horizontally and make the average of that line 128,such that the values get scaled as per their difference from mean(128) which will not cause any distortion to image.</p>

CC BY-SA 3.0
2015-12-24T13:13:16.813Z
Are you aiming for a solution with a specific programming language / library?
TLousky
CC BY-SA 3.0
2015-12-24T13:23:49.630Z
solution  can be in any language .... what is more important is algorithm
Abdul Kahrim
<image-processing>
Average intensity of an gray image
Abdul Kahrim
# Average intensity of an gray image

(Asked by: username_0 on 2015-12-24)

Suppose i have a 400\*300 gray scale image.Now I want to scan each line horizontally and make the average of that line 128,such that the values get scaled as per their difference from mean(128) which will not cause any distortion to image.

**Tags:** image-processing

> **Comments:**
> **username_1**: Are you aiming for a solution with a specific programming language / library?  
> **username_0**: solution  can be in any language .... what is more important is algorithm  

---

## 1 answers

### Answer by username_1 on 2015-12-24. Score: 3

Here's an example on how to do this with Python, Numpy and Matplotlib.
Image on the right has been averaged (row by row), while the left is the original.

[![Before-After-Edited](https://i.sstatic.net/V4wFH.png)](https://i.sstatic.net/V4wFH.png)

**EDITED**: As suggested by @username_2, I avoided flipping colors when row average is 0. Rows with an average of 0 now get a value of 0.5 (128). The dark image encountered previously was due to values higher than 1 (255) due to scaling, that were represented as white in matplotlib's image renderer. I fixed this by setting the max value in any pixel to be 1.

```python
import numpy as np
from matplotlib import image as mpimg
from matplotlib import pyplot as plt

def rgb2gray(rgb):
    return np.dot( rgb[...,:3], [0.299, 0.587, 0.144] )

imgPath = "C:/myImg.jpg"

im = mpimg.imread( imgPath )
gs = rgb2gray( im )

averaged = gs.copy()

for row in averaged:
    rowAvg = sum( row ) / len( row )
    if rowAvg == 0:
        row = 0.5
    else:
        # Scale each row by ratio between 128 and current row avg
        row *= 0.5 / rowAvg

    # Make sure that no pixel has a higher value than 1
    if rowAvg > 0: row[row>1] = 1

plt.subplot( 1, 2, 1 ), plt.imshow( gs,       'gray' )
plt.subplot( 1, 2, 2 ), plt.imshow( averaged, 'gray' )

plt.show()

```

Here's another test with a simple gradient image (left is the original):
[![Before-After2](https://i.sstatic.net/DhczR.png)](https://i.sstatic.net/DhczR.png)

> **Comments:**
> **username_2**: Something looks wrong with your first image pair...the result is mostly complete black, which doesn't seem correct. Also, it would be good to handle divide-by-zero so rows that are complete black in the input don't flip out. Maybe just replace the row by all 128s in that case?  
> **username_1**: Same algorithm for 1st and 2nd img. Will check though, makes sense about the flipping, thanks.  
> **username_3**: The above solution seems correct at first glance but what if user enter the output image as input then it shows distorted output. The solution should be adaptive and learn when not to perform calculation as the output of first input is the solution so when output is again input no changes should be seen.  
> **username_1**: @SandeepNeupane that makes sense, although that's not what the author of the question asked. I'm sure this answer can serve as the basis for a more adaptive, complete solution.  

---

