使用Python統(tǒng)計(jì)相同像素值個(gè)數(shù)
python 統(tǒng)計(jì)相同像素值個(gè)數(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)計(jì)每個(gè)像素值的出現(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))
# 打印每個(gè)像素值及其出現(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)計(jì)每個(gè)像素值的出現(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)計(jì)相同像素值個(gè)數(shù)的文章就介紹到這了,更多相關(guān)Python像素值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pandas combine_first函數(shù)處理兩個(gè)數(shù)據(jù)集重疊和缺失
combine_first是pandas中的一個(gè)函數(shù),它可以將兩個(gè)DataFrame對(duì)象按照索引進(jìn)行合并,用一個(gè)對(duì)象中的非空值填充另一個(gè)對(duì)象中的空值,這個(gè)函數(shù)非常適合處理兩個(gè)數(shù)據(jù)集有部分重疊和缺失的情況,可以實(shí)現(xiàn)數(shù)據(jù)的補(bǔ)全和更新,本文介紹combine_first函數(shù)的語法及一些案例應(yīng)用2024-01-01
Python如何基于rsa模塊實(shí)現(xiàn)非對(duì)稱加密與解密
這篇文章主要介紹了Python如何基于rsa模塊實(shí)現(xiàn)非對(duì)稱加密與解密,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Python sklearn對(duì)文本數(shù)據(jù)進(jìn)行特征化提取
這篇文章主要介紹了Python sklearn對(duì)文本數(shù)據(jù)進(jìn)行特征化提取,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04
Python+Appium實(shí)現(xiàn)自動(dòng)搶微信紅包
不知從何時(shí)開始微信紅包橫空出世,對(duì)于網(wǎng)速和手速慢的人只能在一旁觀望,做為python的學(xué)習(xí)者就是要運(yùn)用編程解決生活和工作上的事情。于是我用python解決我們的手速問題python實(shí)現(xiàn)自動(dòng)搶微信紅包,至于網(wǎng)速慢得那就只能自己花錢提升了。2021-05-05
Python調(diào)用OpenAI?Agents?SDK打造一個(gè)多智能體系統(tǒng)
還在手搓Agent通信邏輯嗎,OpenAI官方SDK讓你用純Python代碼構(gòu)建生產(chǎn)級(jí)多智能體系統(tǒng),本文從零到一,基于Python調(diào)用OpenAI?Agents?SDK打造你的第一個(gè)多智能體系統(tǒng),需要的朋友可以參考下2026-04-04
查看django執(zhí)行的sql語句及消耗時(shí)間的兩種方法
今天小編就為大家分享一篇查看django執(zhí)行的sql語句及消耗時(shí)間的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
關(guān)于scipy.optimize函數(shù)使用及說明
這篇文章主要介紹了關(guān)于scipy.optimize函數(shù)使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12

