python统计RGB图片某像素的个数案例
作者:概率问题 时间:2021-09-30 11:25:56
1.对于RGB三通道图片,直接用两层for循环的话,效率比较低
2.可以先将RGB图片转为灰度图片,再利用numpy.where的广播机制统计像素个数。这里有一个前提是提前知道与灰度图片的像素值相对应RGB颜色。
代码如下:
from PIL import Image
import numpy as np
import cv2
img_L = np.array(Image.open('test.png').convert("L"))
img_RGB = np.array(Image.open('test.png').convert("RGB"))
# temp = {}
# for i in range(img_L.shape[0]):
# for j in range(img_L.shape[1]):
# if not temp.get(int(img_L[i][j])):
# temp[int(img_L[i][j])] = list(img_RGB[i][j])
# print(temp)
#这里得到灰度像素值0对应(0,0,0),62对应(19,69,139)
color_0_0_0 = np.where(img_L == 0)[0].shape[0]
color_19_69_139 = np.where(img_L == 62)[0].shape[0]
pixel_sum = img_L.shape[0] * img_L.shape[1]
print("0_0_0 像素个数:{} 占比:%{}".format(color_0_0_0,color_0_0_0/pixel_sum*100))
print("19_69_139 像素个数:{} 占比:%{}".format(color_19_69_139,color_19_69_139/pixel_sum*100))
补充:OpenCV---如何统计图像的像素分布值个数(6)
代码如下:
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
def statistics():
src = cv.imread("D:/matplotlib/0.jpg")
cv.imshow("q",src)
h,w,ch = np.shape(src)
gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY)
cv.imshow("gray",gray)
hest = np.zeros([256],dtype = np.int32)
for row in range(h):
for col in range(w):
pv = gray[row,col]
hest[pv] +=1
plt.plot(hest,color = "r")
plt.xlim([0,256])
plt.show()
cv.waitKey(0)
cv.destroyAllWindows()
statistics()
运行效果:
像素分布统计图
代码解释:
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
def statistics():
src = cv.imread("D:/matplotlib/0.jpg")
cv.imshow("q",src)
h,w,ch = np.shape(src)
#读取图像属性
gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY)
#将图像转换成灰度图,
cv.imshow("gray",gray)
hest = np.zeros([256],dtype = np.int32)
#建立空白数组
for row in range(h):
for col in range(w):
pv = gray[row,col]
hest[pv] +=1
#统计不同像素值出现的频率
plt.plot(hest,color = "r")
plt.xlim([0,256])
plt.show()
#画出统计图
cv.waitKey(0)
cv.destroyAllWindows()
statistics()
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/qq_33768643/article/details/105724834
标签:python,RGB图片,像素
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
设计师和美工
2008-10-27 13:43:00
![](https://img.aspxhome.com/file/UploadPic/200810/27/2008102713452199s.jpg)
对python3.4 字符串转16进制的实例详解
2022-03-29 16:15:17
Python中的装饰器使用
2023-05-25 13:14:09
详解pandas.DataFrame中删除包涵特定字符串所在的行
2023-08-23 23:37:45
![](https://img.aspxhome.com/file/2023/6/76966_0s.png)
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
2023-11-17 02:40:14
Python读取配置文件-ConfigParser的二次封装方法
2023-06-15 09:19:58
![](https://img.aspxhome.com/file/2023/1/59651_0s.png)
谈点关于checkbox的事情
2010-09-28 14:49:00
对python列表里的字典元素去重方法详解
2023-09-20 13:45:47
Centos7.4环境安装lamp-php7.0教程
2023-11-16 03:07:24
![](https://img.aspxhome.com/file/2023/5/103415_0s.png)
MySQL 定时器EVENT学习
2010-10-14 13:55:00
JSP实现客户信息管理系统
2023-06-30 05:32:36
![](https://img.aspxhome.com/file/2023/9/61349_0s.png)
Python3自动安装第三方库,跟pip说再见
2022-03-12 04:34:15
![](https://img.aspxhome.com/file/2023/6/76896_0s.png)
asp如何显示自定义随机信息?
2010-06-08 09:39:00
Python爬虫教程知识点总结
2023-10-01 15:27:16
![](https://img.aspxhome.com/file/2023/7/76807_0s.png)
python神经网络Batch Normalization底层原理详解
2021-01-28 12:50:08
![](https://img.aspxhome.com/file/2023/2/75292_0s.png)
asp中的rs.open与conn.execute的区别说明
2011-02-24 10:56:00
asp连接access数据库表代码实例
2008-04-13 06:18:00
三种SQL分页查询的存储过程代码
2012-01-05 19:31:32
Oracle 数据库中创建合理的数据库索引
2009-07-02 12:31:00
Linux下Resin+JSP+MySQL安装和配置
2009-09-01 10:38:00