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))
相关推荐
Python私教33 分钟前
AI Agent 上生产要不要开写权限?我把执行链拆成 4 道闸门
人工智能·后端·python
天才测试猿41 分钟前
软件测试知识总结(基础篇)
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
崔子末1 小时前
某影视库剧集列表以及查询接口逆向
爬虫·python
gogogo出发喽1 小时前
v3 admin
python
陈嘿萌3 小时前
ECCV 2026 | 南开&OPPO开源 ExpoMotion:首个大规模动态多曝光融合数据集
人工智能·计算机视觉·图像融合·南开大学·新数据集·expomotion·多曝光融合
weixin_BYSJ19873 小时前
springboot酒店管理系统--附源码27436
java·spring boot·python·随机森林·贪心算法·eclipse·django
xcLeigh4 小时前
编程语言的 AI 友好度排名:Python、JavaScript、TypeScript 谁更适合 AI 辅助
javascript·人工智能·python·ai·typescript·ai编程
Albart5754 小时前
【已解决】ModuleNotFoundError_ No module named ‘xxx’ 模块导入失败终极解决
python·pip·环境配置·后端开发·bug解决·模块报错·python导包失败
花酒锄作田5 小时前
Repository 模式在 FastAPI 中的应用
python·fastapi