Through digital image processing, we can adjust white balance to our liking. Thi
ID: 3595785 • Letter: T
Question
Through digital image processing, we can adjust white balance to our liking. This is done by selecting a reference point (a pixel) that is meant to be grey and adjusting the red, green and blue values of pixels accordingly. Wrie a whiteBalance function according to the four steps below:
a.
Data input – First you must ask the user to select the file that needs to be adjusted. Then you must ask the user to enter the X and Y values of the reference point using the requestInteger function (works like pickAFile).
b.
Calculation of grey value at reference point – The grey value of the pixel is the average of its red, green and blue values. For instance, in an example the RGB values of the reference point at this stage would be 151, 161 and 137. Therefore, the grey value should be 149.66...
c.
Calculation of R, G, B adjustment factors – We want the RGB values of the reference point to be turned in to the grey value calculated above. For this, we divide the grey value by the individual red, green, blue values at the reference point and get the adjustment factor. For an example,
Red adjustment factor is 149.66…/151… = 0.99116…
Green adjustment factor is 149.66…/161… = 0.92960…
Blue adjustment factor is 149.66…/137… = 1.09245…
d.
Adjustment of entire image – In this final step, we use the three values calculated in the previous step and multiply the RGB values of every pixel in the image. This will perform the overall final white balance adjustment. For example, if a particular pixel had the original RGB values as R=20,G=30,B=40, the adjustment would result in a:
Adjusted red value of 20 * 0.99116… = 19.8… = 19 (after turned in to integer) Adjusted green value of 30 * 0.92960… = 27.8… = 27 (after turned in to integer) Adjusted blue value of 40 * 1.09245… = 43.6 = 43 (after turned in to integer)
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
def whiteBalance():
Explanation / Answer
def whiteBalance():
infile = pickAFile()
img = makePicture(file)
explore(img)
# Pick an image and ask for a pixel in the image.
x = requestInteger("Enter reference point x")
y = requestInteger("Enter reference point y")
px = getPixelAt(img, x, y)
r = getRed(px)
g = getGreen(px)
b = getBlue(px)
# The grey value of the pixel is the average of its red, green and blue.
avg = (r+g+b)/3.00
# Calculation of R, G, B adjustment factors.
adjr = avg / r
adjg = avg / g
adjb = avg / b
# Adjustment of the entire image.
for p in getPixels(img):
newR = getRed(p)* adjr
newB = getBlue(p)* adjb
newG = getGreen(p)* adjg
setRed(p, newR)
setGreen(p, newG)
setBlue(p, newB)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.