使用Python统计相同像素值个数
更新时间:2025年01月24日 08:53:57 作者:AI算法网奇
这篇文章主要为大家详细介绍了如何使用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")最大值附近的值
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统计相同像素值个数的文章就介绍到这了,更多相关Python像素值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
已安装Pytorch却提示no moudle named 'torch'(没有名称为torch
这篇文章主要给大家介绍了关于已安装Pytorch却提示no moudle named 'torch'(没有名称为torch的模块)的相关资料,当提示"No module named 'torch'"时,可能是由于安装的Pytorch版本与当前环境不匹配导致的,需要的朋友可以参考下2023-11-11
yolov5训练时参数workers与batch-size的深入理解
最近再学习YOLOv3与YOLOv5训练数据集的具体步骤,几经波折终于实现了很好的效果,这篇文章主要给大家介绍了关于yolov5训练时参数workers与batch-size的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2022-03-03


最新评论