Chapter 6 画像処理¶
# session 6-1
from cImage import *
p = Pixel(200, 100, 150) # create a Pixel object
p
p.getRed() # get the red component
p.setBlue(20) # set the bule component to 20
p
# session 6-2
from cImage import *
myWin = ImageWin("Butterfly")
butterfly = FileImage("butterfly.png")
butterfly.draw(myWin)
# session 6-3
butterfly.getWidth()
butterfly.getHeight()
butterfly.getPixel(124, 165)
# session 6-4
myImWin = ImageWin("Empty Image", 300, 300)
emptyIm = EmptyImage(300, 300)
emptyIm.draw(myImWin)
# session 6-5
from cImage import *
myImWin = ImageWin("Line Image", 300, 300)
lineImage = EmptyImage(300, 300)
whitePixel = Pixel(255, 255, 255)
for i in range(lineImage.getHeight()):
lineImage.setPixel(i, i, whitePixel)
lineImage.draw(myImWin)
lineImage.save("lineImage.gif")
# listing 6-1
def negativePixel(oldPixel):
newRed = 255 - oldPixel.getRed()
newGreen = 255 - oldPixel.getGreen()
newBlue = 255 - oldPixel.getBlue()
newPixel = Pixel(newRed, newGreen, newBlue)
return newPixel
# session 6-6
aPixel = Pixel(155, 23, 255)
negativePixel(aPixel)
# session 6-7
for num in range(3):
for ch in "cat":
print(num, ch)
# listing 6-2
def makeNegative(imageFile):
oldImage = FileImage(imageFile)
width = oldImage.getwidth()
height = oldImage.getHeight()
myImageWindow = ImageWin("Negative Image", width * 2, height)
oldImage.draw(myImageWindow)
newIm = EmptyImage(width, height)
for row in range(height):
for col in range(width):
oldPixel = oldImage.getPixel(col, row)
newPixel = negativePixel(oldPixel)
newIm.setPixel(col, row, newPixel)
newIm.setPosition(width + 1, 0)
newIm.draw(myImageWindow)
myImageWindow.exitOnclick()
# listing 6-3
def grayPixel(oldPixel):
intensitySum = oldPixel.getRed() + oldPixel.getGreen() \
+ oldPixel.getBlue()
aveRGB = intensitySum // 3
newPixel = Pixel(aveRGB, aveRGB, aveRGB)
return newPixel
# session 6-8
grayPixel(Pixel(34, 128, 74))
grayPixel(Pixel(200, 234, 165))
grayPixel(Pixel(23, 56, 77))
# listing 6-4
def makeGrayScale(imageFile):
oldImage = FileImage(imageFile)
width = oldImage.getWidth()
height = oldImage.getHeight()
myImageWindow = ImageWin("Grayscale", width * 2, height)
oldImage.draw(myImageWindow)
newIm = EmptyImage(width, height)
for row in range(height):
for col in range(width):
oldPixel = oldImage.getPixel(col, row)
newPixel = grayPixel(oldPixel)
newIm.setPixel(col, row, newPixel)
newIm.setPosition(width + 1, 0)
newIm.draw(myImageWindow)
myImageWindow.exitOnClick()
# session 6-9
def squareIt(n):
return n * n
squareIt(3)
squareIt(squareIt(3))
squareIt # evaluate name without parameters
z = squareIt # give squareIt another name
z(3)
z
# session 6-10
def squareIt(n):
return n * n
def test(functionParam, n):
return functionParam(n)
test(squareIt, 3) # pass name of function, no parentheses
test(squareIt, 5)
# test(squareIt(3), 5) # actually passing (9, 5)
# listing 6-5
def pixelMapper(fileImage, rgbFunction):
width = fileImage.getWidth()
height = fileImage.getHeight()
newIm = EmptyImage(width, height)
for row in range(height):
for col in range(width):
oldPixel = fileImage.getPixel(col, row)
newPixel = rgbFunction(oldPixel)
newIm.setPixel(col, row, newPixel)
return newIm
# listing 6-6
def generalTransform(imageFile):
oldImage = FileImage(imageFile)
width = oldImage.getWidth()
height = oldImage.getHeight()
myImageWindow = ImageWin("Grayscale", width * 2, height)
oldImage.draw(myImageWindow)
newImage = pixelMapper(oldImage, grayPixel)
newImage.setPosition(oldImage.getWidth() + 1, 0)
newImage.draw(myImageWindow)
myImageWindow.exitOnClick()
# listing 6-7
import math
def hypotenuse(a, b):
c = math.sqrt(a**2 + b**2)
return c
# session 6-11
import math
def hypotenuse(a, b):
c = math.sqrt(a**2 + b**2)
return c
hypotenuse(3, 4) # a receive 3;
side1 = 3 # a receives 3; b receives 4
side2 = 4
hypotenuse(side1, side2 * 2) # a receives 6; b receives 8
hypotenuse # evaluate the function itself
# session 6-12
import math
def hypotenuse(a, b):
c = math.sqrt(a**2 + b**2)
return c
side1 = 3
side2 = 4
dir()
__name__ # current namespace
math
dir(math)
math.__doc__
# session 6-13
from math import *
dir()
sqrt(9)
# session 6-14
def test1(a):
a = a + 5 # refers to a in the local namespace
print(a)
a = 6
test1(a)
a # refers to a in the main namespace
def test2(b):
print(b) # b is in the local namespace
print(a) # refers to a in the main namespace
test2(14)
a
# b # test2 local namespace is destroyed
# listing 6-8
def doubleImage(oldImage):
oldW = oldImage.getWidth()
oldH = oldImage.getHeight()
newIm = EmptyImage(oldW * 2, oldH * 2)
for row in range(oldH):
for col in range(oldW):
oldPixel = oldImage.getPixel(col, row)
newIm.setPixel(2 * col, 2 * row, oldPixel)
newIm.setPixel(2 * col + 1, 2 * row, oldPixel)
newIm.setPixel(2 * col, 2 * row + 1, oldPixel)
newIm.setPixel(2 * col + 1, 2 * row + 1, oldPixel)
return newIm
# listing 6-9
def makeDoubleImage(imageFile):
oldImage = FileImage(imageFile)
width = oldImage.getWidth()
height = oldImage.getHeight()
myWin = ImageWin("Double Size", width * 2, height * 3)
oldImage.draw(myWin)
newImage = doubleImage(oldImage)
newImage.setPosition(0, oldImage.getHeight() + 1)
newImage.draw(myWin)
myWin.exitOnClick()
# listing 6-10
def doubleImage2(oldImage):
oldW = oldImage.getWidth()
oldH = oldImage.getHeight()
newIm = EmptyImage(oldW * 2, oldH * 2)
for row in range(newIm.getHeight()):
for col in range(newIm.getWidth()):
originalCol = col // 2
originalRow = row // 2
oldPixel = oldImage.getPixel(originalCol. originalRow)
newIm.setPixel(col, row, oldPixel)
return newIm
# listing 6-11
def verticalFlip(oldImage):
oldW = oldImage.getWidth()
oldH = oldImage.getHeight()
newIm = EmptyImage(oldW, oldH)
maxP = oldW -1
for row in range(oldH):
for col in range(oldW):
oldPixel = oldImage.getPixel(maxP - col, row)
newIm.setPixel(col, row, oldPixel)
return newIm
# listing 6-12
def convolve(anImage, pixelRow, pixelCol, kernel):
kernelColumnBase = pixelCol - 1
kernelRowBase = pixelRow - 1
sum = 0
for row in range(kernelRowBase, kernelRowBase + 3):
for col in range(kernelColumnBase, kernelColumnBase + 3):
kColIndex = col - kernelColumnBase
kRowIndex = row - kernelRowBase
aPixel = anImage.getPixel(col, row)
intensity = aPixel.getRed()
sum = sum + intensity * kernel[kRowIndex][kColIndex]
return sum
# listing 6-13
import math
def edgeDetect(theImage):
grayImage = pixelMapper(theImage, grayPixel)
newIm = EmptyImage(grayImage.getWidth(), grayImage.getHeight())
black = Pixel(0, 0, 0)
white = Pixel(255, 255, 255)
XMask = [[1, 0, -1], [2, 0, -2], [1, 0, -1]]
YMask = [[-1, -2, -1], [0, 0, 0], [1, 2, 1]]
for row in range(1, grayImage.getHeight() - 1):
for col in range(1, grayImage.getWidth() - 1):
gX = convolve(grayImage, row, col, XMask)
gY = convolve(grayImage, row, col, YMask)
g = math.sqrt(gX**2 + gY**2)
if g > 175:
newIm.setPixel(col, row, black)
else:
newIm.setPixel(col, row, white)
return newIm