opencv 两种方法实现像素取反--python

admin | 世界杯足球赛事

源码:

方法一:

import cv2 as cv

import numpy as np

# 像素取反

def get_img_info(img):

height = img.shape[0] # 高

width = img.shape[1] #宽

channels = img.shape[2] #通道数

#将图像的每个像素点进行反选操作

for row in range(height):

for col in range(width):

for c in range(channels):

pv = img[row, col, c]

img[row, col, c] = 255 - pv

cv.imshow("reserve", img)

cv.waitKey() cv.destroyAllWindows()

方法二:

import cv2 as cv

import numpy as np

# 像素取反

def get_img_reserve(img):

#直接调用反选函数

dst = cv.bitwise_not(img)

cv.imshow("reserve",dst)

cv.waitKey() cv.destroyAllWindows()

原图:

运行结果: