๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[Python ์œผ๋กœ ์˜์ƒ์ฒ˜๋ฆฌ (5)] ํžˆ์Šคํ† ๊ทธ๋žจ ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป 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()

(r, g, b) ๋ณ„๋กœ์˜ histogram

y์ถ•์€ ํ”ฝ์…€์˜ ๊ฐœ์ˆ˜

x์ถ•์€ 0~255 rgb ์ƒ‰์ƒ

728x90
๋ฐ˜์‘ํ˜•
Comments