主頁(yè) > 知識(shí)庫(kù) > python統(tǒng)計(jì)RGB圖片某像素的個(gè)數(shù)案例

python統(tǒng)計(jì)RGB圖片某像素的個(gè)數(shù)案例

熱門(mén)標(biāo)簽:廈門(mén)crm外呼系統(tǒng)如何 ai地圖標(biāo)注 西藏快速地圖標(biāo)注地點(diǎn) 地圖標(biāo)注推廣單頁(yè) 女王谷地圖標(biāo)注 如何在地圖標(biāo)注文字 長(zhǎng)春人工智能電銷機(jī)器人官網(wǎng) 百應(yīng)ai電銷機(jī)器人鄭州 n400電話申請(qǐng)多少錢(qián)

1.對(duì)于RGB三通道圖片,直接用兩層for循環(huán)的話,效率比較低

2.可以先將RGB圖片轉(zhuǎn)為灰度圖片,再利用numpy.where的廣播機(jī)制統(tǒng)計(jì)像素個(gè)數(shù)。這里有一個(gè)前提是提前知道與灰度圖片的像素值相對(duì)應(yīng)RGB顏色。

代碼如下:

from PIL import Image
import numpy as np
import cv2

img_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對(duì)應(yīng)(0,0,0),62對(duì)應(yīng)(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 像素個(gè)數(shù):{} 占比:%{}".format(color_0_0_0,color_0_0_0/pixel_sum*100))
print("19_69_139 像素個(gè)數(shù):{} 占比:%{}".format(color_19_69_139,color_19_69_139/pixel_sum*100))

補(bǔ)充:OpenCV---如何統(tǒng)計(jì)圖像的像素分布值個(gè)數(shù)(6)

代碼如下:

import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
def 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()

運(yùn)行效果:

像素分布統(tǒng)計(jì)圖

代碼解釋:

import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
def 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)
  #將圖像轉(zhuǎn)換成灰度圖,
  cv.imshow("gray",gray)
  hest = np.zeros([256],dtype = np.int32)
  #建立空白數(shù)組
  for row in range(h):
    for col in range(w):
      pv = gray[row,col]
      hest[pv] +=1
      #統(tǒng)計(jì)不同像素值出現(xiàn)的頻率
  plt.plot(hest,color = "r")
  plt.xlim([0,256])
  plt.show()
  #畫(huà)出統(tǒng)計(jì)圖
  cv.waitKey(0)
  cv.destroyAllWindows()
statistics()

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • Python 統(tǒng)計(jì)數(shù)據(jù)集標(biāo)簽的類別及數(shù)目操作
  • Python統(tǒng)計(jì)可散列的對(duì)象之容器Counter詳解
  • Python 統(tǒng)計(jì)列表中重復(fù)元素的個(gè)數(shù)并返回其索引值的實(shí)現(xiàn)方法
  • Python實(shí)戰(zhàn)之單詞打卡統(tǒng)計(jì)
  • python之cur.fetchall與cur.fetchone提取數(shù)據(jù)并統(tǒng)計(jì)處理操作
  • python自動(dòng)統(tǒng)計(jì)zabbix系統(tǒng)監(jiān)控覆蓋率的示例代碼
  • python 統(tǒng)計(jì)代碼耗時(shí)的幾種方法分享
  • Python統(tǒng)計(jì)列表元素出現(xiàn)次數(shù)的方法示例
  • Python jieba 中文分詞與詞頻統(tǒng)計(jì)的操作
  • 利用Python3實(shí)現(xiàn)統(tǒng)計(jì)大量單詞中各字母出現(xiàn)的次數(shù)和頻率的方法
  • 使用Python 統(tǒng)計(jì)文件夾內(nèi)所有pdf頁(yè)數(shù)的小工具
  • python 統(tǒng)計(jì)list中各個(gè)元素出現(xiàn)的次數(shù)的幾種方法
  • python調(diào)用百度AI接口實(shí)現(xiàn)人流量統(tǒng)計(jì)
  • Python代碼覆蓋率統(tǒng)計(jì)工具coverage.py用法詳解
  • python 爬蟲(chóng)基本使用——統(tǒng)計(jì)杭電oj題目正確率并排序
  • 利用python匯總統(tǒng)計(jì)多張Excel
  • python統(tǒng)計(jì)mysql數(shù)據(jù)量變化并調(diào)用接口告警的示例代碼
  • 用python實(shí)現(xiàn)監(jiān)控視頻人數(shù)統(tǒng)計(jì)

標(biāo)簽:廊坊 渭南 拉薩 興安盟 黔東 內(nèi)江 亳州 綿陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python統(tǒng)計(jì)RGB圖片某像素的個(gè)數(shù)案例》,本文關(guān)鍵詞  python,統(tǒng)計(jì),RGB,圖片,某,像素,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python統(tǒng)計(jì)RGB圖片某像素的個(gè)數(shù)案例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于python統(tǒng)計(jì)RGB圖片某像素的個(gè)數(shù)案例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章