首先是一段获取自己电脑摄像头的代码,可以按esc退出
def video_demo():
capture=cv.VideoCapture(0)
while True:
ret,frame=capture.read()
frame=cv.flip(frame,-1)
cv.imshow("video",frame)
c=cv.waitKey(50)
if c==27:
break
0为笔记本电脑摄像头,如果想要读取视频,可以换为视频目录,但是无声
接着获得图片的大小,像素点数,位数,opencv读取图片后的格式,遍历图片像素点获取BGR值
def get_image(image):
print(image.shape)
print(image.size)
print(image.dtype)
print(type(image))
for x in range(image.shape[0]):
for y in range(image.shape[1]):
for z in range(image.shape[2]):
pr=image[x,y]
# 得到BGR值
print(pr)
结果为
(512, 510, 3)
783360
uint8
<class ‘numpy.ndarray’>
[ 97 102 133]
此方法不能用于灰度图,因为灰度图只有一个通道
但是如果直接读取灰度图,调用此函数
grey=cv.imread("./patrick_grey.jpg")
get_image(grey)
输出为
(512, 510, 3)
783360
uint8
<class ‘numpy.ndarray’>
[111 111 111]
此灰度图也是3通道的,原因在于imread 的flag参数
//! Imread flags
enum ImreadModes {
IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image.
IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL = 8, //!< If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8 = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
IMREAD_IGNORE_ORIENTATION = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
};
默认为1时,会读取3通道BGR格式,可设置为-1,就会按原样返回读取图片的格式
grey=cv.imread("./patrick_grey.jpg",-1)
get_image(grey)
(512, 510)
261120
uint8
<class ‘numpy.ndarray’>
遍历像素点并取反,并且计算程序运行时间
def get_image(image):
print(image.shape)
print(image.size)
print(image.dtype)
print(type(image))
for x in range(image.shape[0]):
for y in range(image.shape[1]):
for z in range(image.shape[2]):
image[x,y,z]=255-image[x,y,z]
cv.imshow("pixel_image",image)
t1=cv.getTickCount()
get_image(src)
t2=cv.getTickCount()
# 获取运行时间
time=(t2-t1)/cv.getTickFrequency()*1000
print("time:",time)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果:
(512, 510, 3)
783360
uint8
<class ‘numpy.ndarray’>
time: 2813.6195000000002
原图:
像素变化后:
其实像素点的取反可以直接使用cv2.bitwise_not(image)进行