国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python統計RGB圖片某像素的個數案例

瀏覽:3日期:2022-06-24 15:51:02

1.對于RGB三通道圖片,直接用兩層for循環的話,效率比較低

2.可以先將RGB圖片轉為灰度圖片,再利用numpy.where的廣播機制統計像素個數。這里有一個前提是提前知道與灰度圖片的像素值相對應RGB顏色。

代碼如下:

from PIL import Imageimport numpy as npimport cv2img_L = np.array(Image.open(’test.png’).convert('L'))img_RGB = np.array(Image.open(’test.png’).convert('RGB'))# temp = {}# for i in range(img_L.shape[0]):# for j in range(img_L.shape[1]):# if not temp.get(int(img_L[i][j])):# temp[int(img_L[i][j])] = list(img_RGB[i][j])# print(temp)#這里得到灰度像素值0對應(0,0,0),62對應(19,69,139)color_0_0_0 = np.where(img_L == 0)[0].shape[0]color_19_69_139 = np.where(img_L == 62)[0].shape[0]pixel_sum = img_L.shape[0] * img_L.shape[1]print('0_0_0 像素個數:{} 占比:%{}'.format(color_0_0_0,color_0_0_0/pixel_sum*100))print('19_69_139 像素個數:{} 占比:%{}'.format(color_19_69_139,color_19_69_139/pixel_sum*100))

補充:OpenCV---如何統計圖像的像素分布值個數(6)

代碼如下:

import cv2 as cvimport matplotlib.pyplot as pltimport numpy as npdef statistics(): src = cv.imread('D:/matplotlib/0.jpg') cv.imshow('q',src) h,w,ch = np.shape(src) gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY) cv.imshow('gray',gray) hest = np.zeros([256],dtype = np.int32) for row in range(h): for col in range(w): pv = gray[row,col] hest[pv] +=1 plt.plot(hest,color = 'r') plt.xlim([0,256]) plt.show() cv.waitKey(0) cv.destroyAllWindows()statistics()運行效果:

python統計RGB圖片某像素的個數案例

python統計RGB圖片某像素的個數案例

像素分布統計圖

代碼解釋:

import cv2 as cvimport matplotlib.pyplot as pltimport numpy as npdef statistics(): src = cv.imread('D:/matplotlib/0.jpg') cv.imshow('q',src) h,w,ch = np.shape(src) #讀取圖像屬性 gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY) #將圖像轉換成灰度圖, cv.imshow('gray',gray) hest = np.zeros([256],dtype = np.int32) #建立空白數組 for row in range(h): for col in range(w): pv = gray[row,col] hest[pv] +=1 #統計不同像素值出現的頻率 plt.plot(hest,color = 'r') plt.xlim([0,256]) plt.show() #畫出統計圖 cv.waitKey(0) cv.destroyAllWindows()statistics()

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 济宁市| 乡城县| 田阳县| 江阴市| 弥勒县| 汶川县| 彭阳县| 博湖县| 溧水县| 井陉县| 洛浦县| 乌兰县| 玉屏| 阿勒泰市| 河源市| 辽宁省| 偏关县| 青海省| 灵璧县| 城口县| 房山区| 松江区| 罗山县| 正安县| 嘉义县| 霍林郭勒市| 伊金霍洛旗| 繁峙县| 民丰县| 荃湾区| 察隅县| 合川市| 永顺县| 昔阳县| 桑日县| 九台市| 镇原县| 靖宇县| 三河市| 偃师市| 甘洛县|