Using python-interface for OpenCV, one can easily access the pixel of image using [] operator,like this:
img = cv.LoadImage('test.jpg')
pixel = img[10,10]
variable pixel here is a python tuple object like (10,20,30) (3 channels for example), it's not very convenient to process computation since tuple type does not support operator '-' or '+'. If I hope to do a substruction on a pixel like 255 - (10,20,30), I must code like this:
import numpy as np
pixel = tuple( np.array([255,255,255]) - np.array(pixel) )
is there a faster and easier solution?
Another qustion : is there a way to make a substraction on all pixels, like usin开发者_C百科g a matrix substraction in Matlab: 255 - img (don't use OpenCV build-in function).You could use cv2array()/array2cv()
functions from adaptors.py in the opencv source distribution and perform all your computation using numpy
arrays. 255 - imgarr
works in this case. Example (stripped-down version of cv2array()
for read-only arrays):
assert isinstance(img, cv.iplimage) and img.depth == cv.IPL_DEPTH_8U
a = np.frombuffer(img.tostring(), dtype=np.uint8)
a.shape = img.height, img.width, img.nChannels
print 255 - a
精彩评论