python 统计相同像素值个数

目录

[python 统计相同像素值个数](#python 统计相同像素值个数)

最大值附近的值


python 统计相同像素值个数

python 复制代码
import cv2
import numpy as np
import time
from collections import Counter

# 读取图像
image = cv2.imread('mask16.jpg')


# 将图像转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

area=gray_image.shape[0]*gray_image.shape[1]
print('shape',gray_image.shape)
# 将灰度图像展平为一维数组
pixels = gray_image.flatten()

start=time.time()
# 使用 Counter 统计每个像素值的出现次数
pixel_counts = Counter(pixels)

most_commons = pixel_counts.most_common(10)

print('time',time.time()-start)
count=0
for most_common in most_commons:
    count+=most_common[1]
    print(most_common,count,count/len(pixels))
print(count,count/len(pixels))
# 打印每个像素值及其出现次数
# for pixel_value, count in pixel_counts.items():
#     print(f"Pixel value {pixel_value}: {count} times")

最大值附近的值

python 复制代码
import cv2
import numpy as np
import time
from collections import Counter

# 读取图像
image = cv2.imread('mask16.jpg')


# 将图像转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

area=gray_image.shape[0]*gray_image.shape[1]
print('shape',gray_image.shape)
# 将灰度图像展平为一维数组
pixels = gray_image.flatten()

start=time.time()
# 使用 Counter 统计每个像素值的出现次数
pixel_counts = Counter(pixels)

most_commons = pixel_counts.most_common(10)

print('time',time.time()-start)
count=0
max_pixel=int(most_commons[0][0])
for ii, most_common in enumerate(most_commons):
    if abs(max_pixel- int(most_common[0]))<5:
        count+=most_common[1]
        print(most_common,count,count/len(pixels))
    else:
        print('ffffff',most_common[0])
print(count,count/len(pixels))
相关推荐
爱分享的飘哥12 分钟前
第八十三章:实战篇:文 → 图:Prompt 控制图像生成系统构建——从“咒语”到“神作”的炼成!
人工智能·计算机视觉·prompt·文生图·stablediffusion·diffusers·text-to-image
麻辣清汤1 小时前
结合BI多维度异常分析(日期-> 商家/渠道->日期(商家/渠道))
数据库·python·sql·finebi
钢铁男儿1 小时前
Python 正则表达式(正则表达式和Python 语言)
python·mysql·正则表达式
audyxiao0011 小时前
为了更强大的空间智能,如何将2D图像转换成完整、具有真实尺度和外观的3D场景?
人工智能·计算机视觉·3d·iccv·空间智能
钢铁男儿1 小时前
Python 正则表达式实战:解析系统登录与进程信息
开发语言·python·正则表达式
前端小趴菜052 小时前
python - range
python
☺����2 小时前
实现自己的AI视频监控系统-第一章-视频拉流与解码1
人工智能·python·音视频
前端小趴菜052 小时前
python - 元组常用操作
python
前端小趴菜052 小时前
python - 列表方法
python
前端小趴菜052 小时前
组合数据类型
python