使用Python統(tǒng)計相同像素值個數(shù)
python 統(tǒng)計相同像素值個數(shù)
import cv2
import numpy as np
import time
from collections import Counter
# 讀取圖像
image = cv2.imread('mask16.jpg')
# 將圖像轉(zhuǎn)換為灰度圖像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
area=gray_image.shape[0]*gray_image.shape[1]
print('shape',gray_image.shape)
# 將灰度圖像展平為一維數(shù)組
pixels = gray_image.flatten()
start=time.time()
# 使用 Counter 統(tǒng)計每個像素值的出現(xiàn)次數(shù)
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))
# 打印每個像素值及其出現(xiàn)次數(shù)
# 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')
# 將圖像轉(zhuǎn)換為灰度圖像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
area=gray_image.shape[0]*gray_image.shape[1]
print('shape',gray_image.shape)
# 將灰度圖像展平為一維數(shù)組
pixels = gray_image.flatten()
start=time.time()
# 使用 Counter 統(tǒng)計每個像素值的出現(xiàn)次數(shù)
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))到此這篇關(guān)于使用Python統(tǒng)計相同像素值個數(shù)的文章就介紹到這了,更多相關(guān)Python像素值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pandas combine_first函數(shù)處理兩個數(shù)據(jù)集重疊和缺失
combine_first是pandas中的一個函數(shù),它可以將兩個DataFrame對象按照索引進(jìn)行合并,用一個對象中的非空值填充另一個對象中的空值,這個函數(shù)非常適合處理兩個數(shù)據(jù)集有部分重疊和缺失的情況,可以實現(xiàn)數(shù)據(jù)的補全和更新,本文介紹combine_first函數(shù)的語法及一些案例應(yīng)用2024-01-01
Python如何基于rsa模塊實現(xiàn)非對稱加密與解密
這篇文章主要介紹了Python如何基于rsa模塊實現(xiàn)非對稱加密與解密,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
Python sklearn對文本數(shù)據(jù)進(jìn)行特征化提取
這篇文章主要介紹了Python sklearn對文本數(shù)據(jù)進(jìn)行特征化提取,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04
Python調(diào)用OpenAI?Agents?SDK打造一個多智能體系統(tǒng)
還在手搓Agent通信邏輯嗎,OpenAI官方SDK讓你用純Python代碼構(gòu)建生產(chǎn)級多智能體系統(tǒng),本文從零到一,基于Python調(diào)用OpenAI?Agents?SDK打造你的第一個多智能體系統(tǒng),需要的朋友可以參考下2026-04-04
查看django執(zhí)行的sql語句及消耗時間的兩種方法
今天小編就為大家分享一篇查看django執(zhí)行的sql語句及消耗時間的兩種方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
關(guān)于scipy.optimize函數(shù)使用及說明
這篇文章主要介紹了關(guān)于scipy.optimize函數(shù)使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

