ご質問は、具体的なレベル2程度のレベルしかし、どのような2以上について尋ねているようです。だから私は任意のレベルの色のposterizeすることができます下のコードを追加しました。
import numpy as np
import cv2
im = cv2.imread('messi5.jpg')
n = 2 # Number of levels of quantization
indices = np.arange(0,256) # List of all colors
divider = np.linspace(0,255,n+1)[1] # we get a divider
quantiz = np.int0(np.linspace(0,255,n)) # we get quantization colors
color_levels = np.clip(np.int0(indices/divider),0,n-1) # color levels 0,1,2..
palette = quantiz[color_levels] # Creating the palette
im2 = palette[im] # Applying palette on image
im2 = cv2.convertScaleAbs(im2) # Converting image back to uint8
cv2.imshow('im2',im2)
cv2.waitKey(0)
cv2.destroyAllWindows()
このコードは、ピクセルを反復処理するよりも、本当に速いですnumpyのでパレット法と呼ばれる方法を使用しています。あなたはここにコードをスピードアップするために使用することができる方法の詳細を見つけることができます。
オリジナル画像::Fast Array Manipulation in Numpy以下
は、私はさまざまなレベルで得られた結果である
![enter image description here](https://i.stack.imgur.com/CcZCp.jpg)
レベル2:
![enter image description here](https://i.stack.imgur.com/pVlma.png)
レベル4:
![enter image description here](https://i.stack.imgur.com/gXncy.png)
レベル8:
![enter image description here](https://i.stack.imgur.com/h4OxO.png)
のように...
こんにちは、すべてのレベルのための一般的な答えを追加しました。 –