๐ฉ๐ป IoT (Embedded)/Image Processing
[Python ์ผ๋ก ์์์ฒ๋ฆฌ (5)] ํ์คํ ๊ทธ๋จ
์ง์ง์ํ์นด
2023. 12. 28. 11:14
728x90
๋ฐ์ํ
๐ ํ์คํ ๊ทธ๋จ by. cv2
์ ํ์คํ ๊ทธ๋จ ๋ณด๋๊น 0์ ๊ทผ์ ํ ์๊ฐ ๋ง์ ๊ฑธ ๋ณด์ ์ด๋์ด ๊ทธ๋ฆผ์ธ๊ฑธ ํ์ธํ๋ค!
import cv2
img = cv2.imread("images/couple2.jpg", flags=cv2.IMREAD_GRAYSCALE)
import numpy as np
hist = np.zeros((256))
cv2.imshow('histogram', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
height, width = img.shape
for y in range(height):
for x in range(width):
hist[img[y,x]] = hist[img[y,x]] + 1
import matplotlib.pyplot as plt
plt.bar(x=range(256), height=hist, width=1)
plt.show()
๐ ํ์คํ ๊ทธ๋จ by. ํจ์ ๊ตฌํ
max(hist)
# 3639.0
hist_img = np.full((300+30,256), 255, dtype=np.uint8)
hist = hist.astype(int)
hist_max = max(hist)
for i, value in enumerate(hist):
height = -int(value * 300/hist_max)
if height<-1:
hist_img[height:,i] = 0
cv2.imshow('Histogram', hist_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
์ค cv2 ์์ ์ฌ์ฉํ ๊ฑฐ๋
ํจ์ ๊ตฌํํ๊ฑฐ๋
๋น์ทํ ํ์คํ ๊ทธ๋จ์ด ๋์ค๋ ๊ฒ์ ์ ์ ์๋ค!
๐ ์ฑ๋๋ณ ํต๊ณ
import cv2
lena = cv2.imread("images/lena.jpg")
plt.figure(figsize=(12,4))
for i in range(3):
plt.subplot(1,3,i+1)
hist = cv2.calcHist(images=[lena],
channels=[i], mask=None,
histSize=[256], ranges=[0,256])
plt.plot(hist.flatten())
plt.show()
y์ถ์ ํฝ์ ์ ๊ฐ์
x์ถ์ 0~255 rgb ์์
728x90
๋ฐ์ํ