10/26/2018

python opencv, example code for image pixel access

The code is example to access every pixel in opencv python.
More detail, just see below code.
Thank you.

import cv2

def OpenCV_Access_RGBValue(inMat):
#input is supposed as color
# grab the image dimensions
h = inMat.shape[0]
w = inMat.shape[1]
# loop over the image
for y in range(0, h):
for x in range(0, w):
# threshold the pixel
b = inMat[y, x, 0]
g = inMat[y, x, 1]
r = inMat[y, x, 2]
b = 255 - b
g = 255 - g
r = 255 - r

inMat[y, x, 0] = b
inMat[y, x, 1] = g
inMat[y, x, 2] = r

#image[y, x] = 255 if image[y, x] >= 128T else 0
# return the thresholded image
return inMat


#read image
inMat = cv2.imread('iu.jpg')

#test value access
OpenCV_Access_RGBValue(inMat)

#display
cv2.namedWindow('test',0)
cv2.imwrite('output.jpg', inMat)
cv2.imshow('test',inMat)
cv2.waitKey(0)






No comments:

Post a Comment