使用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)文章
已安裝Pytorch卻提示no?moudle?named?'torch'(沒有名稱為torch
這篇文章主要給大家介紹了關(guān)于已安裝Pytorch卻提示no?moudle?named?'torch'(沒有名稱為torch的模塊)的相關(guān)資料,當(dāng)提示"No module named 'torch'"時,可能是由于安裝的Pytorch版本與當(dāng)前環(huán)境不匹配導(dǎo)致的,需要的朋友可以參考下2023-11-11yolov5訓(xùn)練時參數(shù)workers與batch-size的深入理解
最近再學(xué)習(xí)YOLOv3與YOLOv5訓(xùn)練數(shù)據(jù)集的具體步驟,幾經(jīng)波折終于實(shí)現(xiàn)了很好的效果,這篇文章主要給大家介紹了關(guān)于yolov5訓(xùn)練時參數(shù)workers與batch-size的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03淺談django框架集成swagger以及自定義參數(shù)問題
這篇文章主要介紹了淺談django框架集成swagger以及自定義參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python實(shí)現(xiàn)AI自動玩俄羅斯方塊游戲
提到《俄羅斯方塊》,那真是幾乎無人不知無人不曉。其歷史之悠久,可玩性之持久,能手輕輕一揮,吊打一大波游戲。本文將利用Python實(shí)現(xiàn)俄羅斯方塊進(jìn)階版—AI自動玩俄羅斯方塊,感興趣的可以學(xué)習(xí)一下2022-03-03Python實(shí)現(xiàn)監(jiān)控一個程序的運(yùn)行情況
這篇文章主要為大家介紹了Python如何實(shí)現(xiàn)監(jiān)控一個程序的運(yùn)行情況,然后視情況將進(jìn)程殺死并重啟,文中的示例代碼簡潔易懂,需要的可以參考一下2023-05-05Python3 導(dǎo)入上級目錄中的模塊實(shí)例
今天小編就為大家分享一篇Python3 導(dǎo)入上級目錄中的模塊實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02