关于第三章3.6节的错误
3.6节中的对cameo.py的修改中,突然出现了filters.BGRPortraCurveFilter()让人措手不及。。。前面并没有写这个函数,后面也没有,查看原书英文版发现原书就是这样,坑啊,然后找来第一版,终于把坑补上了,废话不说,贴代码:
先把utils.py改为:
import cv2,numpy,scipy.interpolate
def createCurveFunc(points):
"""Return a function derived from control points."""
if points is None:
return None
num_points = len(points)
if num_points < 2:
return None
xs, ys = zip(*points)
if num_points < 4:
kind = 'linear'
# 'quadratic' is not implemented.
else:
kind = 'cubic'
return scipy.interpolate.interp1d(xs, ys, kind,bounds_error = False)
def createLookupArray(func, length = 256):
"""Return a lookup for whole-number inputs to a function. The lookup values are clamped to [0, length - 1]."""
if func is None:
return None
lookup_array = numpy.empty(length)
i = 0
while i < length:
func_i = func(i)
lookup_array[i] = min(max(0, func_i), length - 1)
i += 1
return lookup_array
def applyLookupArray(lookup_array, src, dst):
"""Map a source to a destination using a lookup."""
if lookup_array is None:
return
dst[:] = lookup_array[src]
def createCompositeFunc(func0, func1):
"""Return a composite of two functions."""
if func0 is None:
return func1
if func1 is None:
return func0
return lambda x: func0(func1(x))
def createFlatView(array):
"""Return a 1D view of an array of any dimensionality."""
flat_view = array.view()
flat_view.shape = array.size
return flat_view
然后,再在filters.py中加入三个类:
class BGRFuncFilter(object):
def __init__(self, vFunc = None, bFunc = None, gFunc = None, rFunc = None,dtype = numpy.uint8) :
length = numpy.iinfo(dtype).max + 1
self._bLookupArray = utils.createLookupArray(utils.createCompositeFunc(bFunc, vFunc), length)
self._gLookupArray = utils.createLookupArray(utils.createCompositeFunc(gFunc, vFunc), length)
self._rLookupArray = utils.createLookupArray(utils.createCompositeFunc(rFunc, vFunc), length)
def apply(self, src, dst) :
"""Apply the filter with a BGR source/destination."""
b, g, r = cv2.split(src)
utils.applyLookupArray(self._bLookupArray, b, b)
utils.applyLookupArray(self._gLookupArray, g, g)
utils.applyLookupArray(self._rLookupArray, r, r)
cv2.merge([ b, g, r ], dst)
class BGRCurveFilter(BGRFuncFilter):
def __init__(self, vPoints = None, bPoints = None,gPoints = None, rPoints = None, dtype = numpy.uint8):
BGRFuncFilter.__init__(self, utils.createCurveFunc(vPoints),
utils.createCurveFunc(bPoints),
utils.createCurveFunc(gPoints),
utils.createCurveFunc(rPoints), dtype)
def __init__(self, dtype = numpy.uint8):
BGRCurveFilter.__init__(self,
vPoints = [ (0, 0), (23, 20), (157, 173), (255, 255) ],
bPoints = [ (0, 0), (41, 46), (231, 228), (255, 255) ],
gPoints = [ (0, 0), (52, 47), (189, 196), (255, 255) ],
rPoints = [ (0, 0), (69, 69), (213, 218), (255, 255) ],
dtype = dtype)```
这样,运行修改后的cameo.py就不会报错了
3.10章节缺失代码
import cv2
import numpy as np
img = cv2.pyrDown(cv2.imread("hammer.jpg", cv2.IMREAD_UNCHANGED))
ret, thresh = cv2.threshold(cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY) , 127, 255, cv2.THRESH_BINARY)
black = cv2.cvtColor(np.zeros((img.shape[1], img.shape[0]), dtype=np.uint8), cv2.COLOR_GRAY2BGR)
image, contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
epsilon = 0.01 * cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
hull = cv2.convexHull(cnt)
cv2.drawContours(black, [cnt], -1, (0, 255, 0), 2)
cv2.drawContours(black, [approx], -1, (255, 255, 0), 2)
cv2.drawContours(black, [hull], -1, (0, 0, 255), 2)
cv2.imshow("hull", black)
cv2.waitKey()
cv2.destroyAllWindows()