1.修改彩色图的某个像素值
# -*- coding:utf-8 -*-
import cv2
#读取图片,注意imread是以BRG格式读取图像的
img = cv2.imread(r'C:\Users\thorne\PycharmProjects\biyesheji\image.jpeg', cv2.IMREAD_UNCHANGED)
#获得指定位置的像素值
test = img[88,142]
print (test)
#修改像素值
img[88,142] = [255, 255, 255]
print (test)
#分别查看BGR通道像素
blue = img[88,142,0]
print (blue)
green = img[88,142,1]
print (blue)
red = img[88,142,2]
print (blue)
#显示图像
cv2.imshow("Demo", img)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
运行结果
[212 162 116]
[255 255 255]
255
255
255
2.修改灰度图的某个像素值
# -*- coding:utf-8 -*-
import cv2
#读取图片,注意imread是以BRG格式读取图像的
img = cv2.imread(r'C:\Users\thorne\PycharmProjects\biyesheji\image.jpeg', cv2.IMREAD_GRAYSCALE)
#获得指定位置的像素值
test = img[88,142]
print (test)
#修改像素值
img[88,142] = 0
#获取修改后的像素值
print (img[88,142])
#显示图像
cv2.imshow("Demo", img)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
154
0
3.修改指定区域的像素值
# -*- coding:utf-8 -*-
import cv2
#读取图片
img = cv2.imread(r'C:\Users\thorne\PycharmProjects\biyesheji\image.jpeg')
#该区域设置为白色
img[100:125, 150:250] = [255,255,255]
#显示图像
cv2.imshow("Demo", img)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
效果图
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)