Interface2017年05月号新画像処理101を読む

A-3 濃淡を網目の2値パターンで表現する「ベイヤー型ハーフトーニング」

from random import random
img_src = cv2.imread("data/img_A-3.bmp")
img_dst = np.empty_like(img_src)
#座標の大きさ
eight = img_src.shape[0]
width = img_src.shape[1]
patt = np.array([[0,8,2,10],
                        [12,4,14,6],
                        [3,11,1,9],
                        [15,7,13,5]])

for y in range(0,eight-1,4):
    for x in range(0,width-1,4):
        for j in range(4):
            for i in range(4):
                if img_src[y+j,x+i,0] < patt[i,j] * 16 + 8:
                    img_dst[y+j,x+i] = [0,0,0]
                else:
                    img_dst[y+j,x+i] = [255,255,255]

#描画する
cv2.imshow("img_src",img_src)
cv2.imshow("img_dst",img_dst)

cv2.waitKey(0)
cv2.destroyAllWindows()

#ヒストグラム
fig = plt.figure()
ax1 = fig.add_subplot(211) #総行数,総列数、サブプロット番号
ax2 = fig.add_subplot(212)

color_list = ["gray"]
ax2.set_ylim(0,5000)
for i,j in enumerate(color_list):
    hist = cv2.calcHist([img_src],[i],None,[256],[0,256])
    ax1.plot(hist,color = j)

color_list = ["gray"]
ax2.set_ylim(0,5000)
for i,j in enumerate(color_list):
    hist = cv2.calcHist([img_dst],[i],None,[256],[0,256])
    ax2.plot(hist,color = j)

f:id:bitop:20180212062412p:plain

f:id:bitop:20180215055630p:plain

f:id:bitop:20180215055714p:plain