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

A-2 濃度パターンを用いる代表的な濃淡表現「ハーフトーン型ハーフトーニング」

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

for y in range(0,eight,4):
    for x in range(0,width,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:20180214060504p:plain

f:id:bitop:20180214060533p:plain

f:id:bitop:20180214060615p:plain