源代码:
import cv2 as cv
import numpy as num
def access_pixels(image):
print(image.shape)
height=image.shape[0]
width=image.shape[1]
channels=image.shape[2]
print('height:%s width:%s channels:%s'%(height,width,channels))
for row in range(height):
for col in range(width):
for c in range(channels):
pv=image[height,width,channels]
image[row,col,c]= 255 - pv
cv.imshow('pixels_demo',image)
src=cv.imread('D:\girl.jpg')
src1=cv.resize(src,(1,200),)
cv.namedWindow('input image',cv.WINDOW_AUTOSIZE)
cv.imshow('input image',src1)
access_pixels(src1)
cv.waitKey(0)
cv.destroyAllWindows()
运行后,出现的问题:
C:\ProgramData\Anaconda3\python.exe D:/pythonProject3/main.py
(200, 1, 3)
height:200 width:1 channels:3
Traceback (most recent call last):
File "D:/pythonProject3/main.py", line 21, in <module>
access_pixels(src1)
File "D:/pythonProject3/main.py", line 13, in access_pixels
pv=image[height,width,channels]
IndexError: index 200 is out of bounds for axis 0 with size 200
Process finished with exit code 1
即IndexError,200这个数值怎么调整都不行。
如果你想修改像素值为原来的与255的差, 那么13行应该改成pv = img[row, col, c]
不过提醒一下, 三个for循环速度非常慢, 而且也影响观感
广播能很好的解决这个机制, 整个函数可以写成
def access_pixels(image):
image = 255-image
cv.imshow('pixels_demo',image)
不是很理解你pv=img[height, width, channels]想干嘛