技A 白黒の粗密濃淡表現
A-1 白黒の粗密で濃淡を表現する「ハーフトーニング」
from random import random
img_src = cv2.imread("data/img_A-1.bmp")
img_gray = cv2.cvtColor(img_src,cv2.COLOR_RGB2GRAY)
img_dst = np.empty_like(img_src)
#座標の大きさ
eight = img_gray.shape[0]
width = img_gray.shape[1]
k = 0.1
for y in range(eight):
for x in range(width):
gray = img_gray[y,x]
T = (1.0 - k) * (1.0 - gray/255.0)
P = random()
if P > T:
img_dst[y,x] = [255,255,255]
else:
img_dst[y,x] = [0,0,0]
#描画する
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)