473,804 Members | 3,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

average of PIL images

hi
i have a set of RGB images of diff faces (of people )as a 2 dim
numpyarray
...something like
threefaces=arra y([[xa1,xa2,xa3],
[xb1,xb2,xb3],
[xc1,xc2,xc3]])
where xa1,xa2,xa3 are tuples each representing rgb values of a pixel
of first image ..

i need to create the average face image and display it.problem is i
need to calculate (xa1+xb1+xc1)/3 etc to calculate avearge value of
each pixel.how can i do this calculation.do i need to convert the
r,g,b in to a single value for each pixel? the average value of a
pixel will be a float isn't it? how can i create a PIL image from
this?
any help,directive greatly appreciated
eric
Feb 18 '08 #1
13 4908
vaneric wrote:
hi
i have a set of RGB images of diff faces (of people )as a 2 dim
numpyarray
..something like
threefaces=arra y([[xa1,xa2,xa3],
[xb1,xb2,xb3],
[xc1,xc2,xc3]])
where xa1,xa2,xa3 are tuples each representing rgb values of a pixel
of first image ..

i need to create the average face image and display it.problem is i
need to calculate (xa1+xb1+xc1)/3 etc to calculate avearge value of
each pixel.how can i do this calculation.
threefaces.mean (axis=0)
do i need to convert the
r,g,b in to a single value for each pixel?
It depends on the problem that you are trying to solve. If monochrome images are
acceptable for your problem, then it is probably best to convert all your images
to monochrome to do the averaging. At least for a first cut. Averaging color
images is tricky; you really shouldn't do it in the RGB colorspace. There are a
couple of colorspaces which you could choose; different problems require
different colorspaces.
the average value of a
pixel will be a float isn't it?
Yes.
how can i create a PIL image from
this?
# Cast the floating point data to bytes.
avgface = avgface.astype( numpy.uint8)

# Create the PIL image from the numpy data.
img = Image.fromstrin g('L', (width, height), avgface.tostrin g())

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Feb 18 '08 #2
vaneric wrote:
hi
i have a set of RGB images of diff faces (of people )as a 2 dim
numpyarray
..something like
threefaces=arra y([[xa1,xa2,xa3],
[xb1,xb2,xb3],
[xc1,xc2,xc3]])
where xa1,xa2,xa3 are tuples each representing rgb values of a pixel
of first image ..

i need to create the average face image and display it.problem is i
need to calculate (xa1+xb1+xc1)/3 etc to calculate avearge value of
each pixel.how can i do this calculation.do i need to convert the
r,g,b in to a single value for each pixel? the average value of a
pixel will be a float isn't it? how can i create a PIL image from
this?
any help,directive greatly appreciated
eric
Take a look at ImageChops.diff erence. I've used it to calculate a
difference value as follows:

diff=ImageChops .difference(im1 , im2)
totaldiff=sum(I mageStat.Stat(d iff)._getmedian ())

Maybe at least this will point you in the right direction.

-Larry Bates
Feb 18 '08 #3
On Feb 18, 10:18*am, vaneric <vaneric...@gma il.comwrote:
hi
i have a set of RGB images of diff faces (of people )as a 2 dim
numpyarray
..something like
threefaces=arra y([[xa1,xa2,xa3],
* * * *[xb1,xb2,xb3],
* * * *[xc1,xc2,xc3]])
where xa1,xa2,xa3 are *tuples each representing rgb values of a pixel
of first image ..

i need to create the average face image and display it.problem is i
need to calculate (xa1+xb1+xc1)/3 *etc to calculate avearge value of
each pixel.how can i do this calculation.do i need to convert the
r,g,b in to a single value for each pixel? the average value of a
pixel will be a float isn't it? how can i create a PIL image from
this?
any help,directive greatly appreciated
eric
import Numeric

arr = Numeric.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
])

col1 = arr[0:,0]
print col1

sum = 0
count = 0
for num in col1:
sum += num
count += 1

avg = (sum * 1.0)/count #convert one term to a float to get float
result
print avg
print round(avg)
print int(round(avg))
print arr[0]

size = len(arr[0])
for i in range(size):
col = arr[0:, i]
sum = 0
count = 0

for num in col:
sum += num
count += 1

result = (sum * 1.0) /count
print result,
result = int(round(resul t))
print result

--output:--
[ 1 4 7 10]
5.5
6.0
6
[1 2 3]
5.5 6
6.5 7
7.5 8
Feb 18 '08 #4
On Feb 18, 1:58*pm, 7stud <bbxx789_0...@y ahoo.comwrote:
On Feb 18, 10:18*am, vaneric <vaneric...@gma il.comwrote:
hi
i have a set of RGB images of diff faces (of people )as a 2 dim
numpyarray
..something like
threefaces=arra y([[xa1,xa2,xa3],
* * * *[xb1,xb2,xb3],
* * * *[xc1,xc2,xc3]])
where xa1,xa2,xa3 are *tuples each representing rgb values of a pixel
of first image ..
i need to create the average face image and display it.problem is i
need to calculate (xa1+xb1+xc1)/3 *etc to calculate avearge value of
each pixel.how can i do this calculation.do i need to convert the
r,g,b in to a single value for each pixel? the average value of a
pixel will be a float isn't it? how can i create a PIL image from
this?
any help,directive greatly appreciated
eric

import Numeric

arr = Numeric.array([
* * [1, 2, 3],
* * [4, 5, 6],
* * [7, 8, 9],
* * [10, 11, 12]
])

col1 = arr[0:,0]
print col1

sum = 0
count = 0
for num in col1:
* * sum += num
* * count += 1

avg = (sum * 1.0)/count * #convert one term to a float to get float
result
print avg
print round(avg)
print int(round(avg))

print arr[0]

size = len(arr[0])
for i in range(size):
* * col = arr[0:, i]
* * sum = 0
* * count = 0

* * for num in col:
* * * * sum += num
* * * * count += 1

* * result = (sum * 1.0) /count
* * print result,
* * result = int(round(resul t))
* * print result

--output:--
[ 1 *4 *7 10]
5.5
6.0
6
[1 2 3]
5.5 6
6.5 7
7.5 8
In this statement:
col1 = arr[0:,0]
the first term is the row or row range, and the second term is the
column or column range. If you write this:

num = arr[0,0]

you get the element in row 0, column 0. But you can also specify
ranges for each col or row:

num = arr[1:, 2:]

That says to get all elements from row 1 to the bottom that are in
from column 2 to the end of the row.
Feb 18 '08 #5
On Feb 18, 2:05*pm, 7stud <bbxx789_0...@y ahoo.comwrote:
num = arr[1:, 2:]

That says to get all elements from row 1 to the bottom that are in
from column 2 to the end of the row.
err..

That says to get all elements from row 1 to the last row which are in
column 2, column 3, etc. to the end of the row.

Feb 18 '08 #6
On Feb 19, 1:38 am, Robert Kern <robert.k...@gm ail.comwrote:
>Averaging color
images is tricky; you really shouldn't do it in the RGB colorspace.
hi,
thanx for the guidance and detailed replies..I tried to pack the
r,g,b into a single value like below(something a member posted in the
past)

def rgbTopixelvalue ((r,g,b)):
alpha=255
return unpack("l", pack("BBBB", b, g, r, alpha))[0]
if i apply this for all images i can represent each image as an array
of longs instead of tuples.then for a pixel i can calculate the
average value
after this if i do the casting as you advised and create the avg
image
avgface = avgface.astype( numpy.uint8)
img = Image.fromstrin g('L', (width, height), avgface.tostrin g())

is there something wrong with my approach? I am a newbie in PIL/
imageprocessing ..so i would greately appreciate feedback
eric
Feb 19 '08 #7
On Feb 19, 1:38 am, Robert Kern <robert.k...@gm ail.comwrote:
>Averaging color
images is tricky; you really shouldn't do it in the RGB colorspace.
hi,
thanx for the guidance and detailed replies..I tried to pack the
r,g,b into a single value like below(something a member posted in the
past)

def rgbTopixelvalue ((r,g,b)):
alpha=255
return unpack("l", pack("BBBB", b, g, r, alpha))[0]

if i apply this for all images i can represent each image as an array
of longs instead of tuples.then for a pixel i can calculate the
average value
after this if i do the casting as you advised and create the avg
image
avgface = avgface.astype( numpy.uint8)

here if i use these pixelvalues to create an imag
img =Image.fromstri ng('RGB', (width, height), avgface.tostrin g())
it will fail because of -'not enough image data'..is there an
alternative to create average rgb color image ?(i want to keep the rgb
so can't convert to greyscale)

is there something wrong with my approach? I am a newbie in PIL/
imageprocessing ..so i would greately appreciate feedback
eric
Feb 19 '08 #8
En Tue, 19 Feb 2008 04:01:04 -0200, vaneric <va********@gma il.com>
escribió:
On Feb 19, 1:38 am, Robert Kern <robert.k...@gm ail.comwrote:
>Averaging color
images is tricky; you really shouldn't do it in the RGB colorspace.

hi,
thanx for the guidance and detailed replies..I tried to pack the
r,g,b into a single value like below(something a member posted in the
past)

def rgbTopixelvalue ((r,g,b)):
alpha=255
return unpack("l", pack("BBBB", b, g, r, alpha))[0]
That's much worse than averaging the R,G,B components. First, you have to
omit the alfa value (or set it at the end). Then, consider this example:
(0,0,0)=black and (0,1,0)=almost black, average = (0,0,128) = dark blue, a
total nonsense.

As said above, try to compute using another color space, try HSL. The
colorsys module can transform from/to RGB.

--
Gabriel Genellina

Feb 19 '08 #9
def rgbTopixelvalue ((r,g,b)):
alpha=255
return unpack("l", pack("BBBB", b, g, r, alpha))[0]

That's much worse than averaging the R,G,B components.
oops!
the intention was to pack r,g,b components into a single value sothat
calculations like finding covariant matrix of a set of images etc can
be done..(i need to represent each image as an array of values(either
long or float)..i can't work with an array of tuples of ints..
As said above, try to compute using another color space, try HSL. The
colorsys module can transform from/to RGB.
even if i convert from rgb to hsl i will have a tuple(h,s,l) for each
pixel and again i will have to convert it into a single value which i
can use in matrix multipln etc

is there a workaround sothat rgb color images can be worked on? any
advice most welcome..

Feb 19 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4407
by: Sara | last post by:
I am trying to find the average number of products of the set that is on rows...dynamically. For example, if I had states on rows and was looking at the average number of products, I would see this IN 72 MI 66 AL 88
6
3140
by: J | last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller program. Heres's what is supposed to happen: Roll 2 6-sided dies. Add rolls together put total in rolls(d6total). Display the number of 2 or 12 rolled, then display those numbers and average rolls. I didn't know how to do it exactly, but I came up with this.
4
3413
by: Gary | last post by:
Hi, I have a temperature conversion program down pat, but I was told to add an average, meaning, i need to get the average temperature for as many times as it was entered. i do not know where to start, and my stpid book doesnt seem to help. here is my program #include <iostream.h> void main( ) {
3
2319
by: C++Geek | last post by:
I need to get this program to average the salaries. What am I doing wrong? //Program to read in employee data and calculate the average salaries of the emplyees.
3
4494
by: simpleeshelbee | last post by:
Hey guys, This is my second post and is URGENT!!!! My final assignment is due tonite for class and I have no idea how to write this program right! I am supposed to write a program that uses a two-dimensional array to store the highest and lowest temperatures (in farenheit) for each month of the year. The program is supposed to output the average high, average low and the highest and lowest temperatures of the year. I need it to print out as...
3
4145
by: mochatrpl | last post by:
I am looking for a way to make a query / report display the running average for total dollars. I have already set up a query to provide totals dollars per day from which a report graphly shows the dollars per week. How do I then take the dollars and get a running average for the year? - Randy
3
5312
by: Salad | last post by:
http://www.mathwords.com/w/weighted_average.htm At the above link gives an example of a weighted average. It uses the following example: Grades are often computed using a weighted average. Suppose that homework counts 10%, quizzes 20%, and tests 70%. If Pat has a homework grade of 92, a quiz grade of 68, and a test grade of 81, then
3
2002
by: vileoxidation | last post by:
Hello, and thanks for any help in advance! Basically, as the title says, I am looking for a way to print the number of times the user gave the program an input, and then also print the average of all the inputs. So far, I have initialized three variables as follows: int counter=0; int sum=0; float average=0;
5
3185
by: p3rk3le | last post by:
So, I'm about to do a sequential search on a table (n contents) of random numbers. I have to print the average between the number of comparisons and the contents of the table (n) and the average time for each sequential search. I wrote:
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.