OpenCV Histogram

Ref: https://docs.opencv.org/master/

์˜์ƒ ํžˆ์Šคํ† ๊ทธ๋žจ

์˜์ƒ ํžˆ์Šคํ† ๊ทธ๋žจ์€ X ์ถ• - ํ”ฝ์…€ ๊ฐ’, Y ์ถ• - ํ”ฝ์…€ ์ˆ˜๋กœ ๋งŒ๋“  ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค.

ํžˆ์Šคํ† ๊ทธ๋žจ์„ ๋ณด๋ฉด ๋™์ผํ•œ ํ”ฝ์…€ ๊ฐ’์„ ๊ฐ–๋Š” ํ”ฝ์…€์ด ๋ช‡๊ฐœ๋‚˜ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , ํ”ฝ์…€ ๊ฐ’์ด ๊ณจ๊ณ ๋ฃจ ๋ถ„ํฌ๋˜์–ด์žˆ๋Š”์ง€ ๋˜๋Š” ์ง‘์ค‘๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

hist = cv.calcHist(images, channels, mask, histSize, ranges, hist=None, accumulate=False)
  • @param images: list [img1,] ๊ฐ ์ด๋ฏธ์ง€์˜ (๊ฐ€๋กœ, ์„ธ๋กœ) ํฌ๊ธฐ๊ฐ€ ๋™์ผํ•ด์•ผํ•จ
  • @param channels: list [ch1,] ๋ˆ„์  ์ฑ„๋„ ๋ฒˆํ˜ธ, 0 ๋ถ€ํ„ฐ ์‹œ์ž‘๋จ
  • @param mask: np.ndarray
    • None์ธ ๊ฒฝ์šฐ ์ด๋ฏธ์ง€ ์ „์ฒด ๋ถ„์„
    • mask = np.zeros(img.shape[:2], dtype=np.uint8)
    • mask[x:x+w, y:y+h] = 255
  • @param histSize: list [x์ถ•,] ํ”ฝ์…€ ๊ฐ’ ๋‹จ๊ณ„
    • np.uint8 ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฏธ์ง€์˜ 256//8 ๋กœ ์„ค์ •ํ•˜๋ฉด 0~7, 8~15, ... ์ด๋ ‡๊ฒŒ ๋ฌถ์–ด์„œ 256//8 ๋‹จ๊ณ„๋กœ ๊ณ„์‚ฐ๋จ
  • @param ranges: list [min1, max1,] ๋ถ„์„ ๋ฒ”์œ„, np.uint8 ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ ์ตœ๋Œ€ ๋ถ„์„ ๋ฒ”์œ„ [0,256]
  • @param accumulate: bool True๋กœ ์„ค์ •ํ•˜๊ณ  ์—ฌ๋Ÿฌ ์ด๋ฏธ์ง€๋ฅผ ๋ถ„์„ํ•˜๋ฉด ํ•˜๋‚˜์˜ ํžˆ์Šคํ† ๊ทธ๋žจ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋ˆ„์ ๋จ
caution

์ด๋ฏธ์ง€๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๋ถ„์„ํ•˜๋Š” ๊ฒฝ์šฐ ์ž…๋ ฅํ•œ ๋ชจ๋“  ์ด๋ฏธ์ง€๊ฐ€ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์ด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 3 ์ฑ„๋„ ์ด๋ฏธ์ง€ 2 ๊ฐœ๋ฅผ ์„ค์ •ํ•˜๋ฉด ํฌ๊ธฐ๊ฐ€ (๊ฐ€๋กœ, ์„ธ๋กœ, 6)๋กœ ์ธ์‹ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฏธ์ง€ 1 ์˜ 1 ๋ฒˆ ์ฑ„๋„๊ณผ ์ด๋ฏธ์ง€ 2 ์˜ 1 ๋ฒˆ ์ฑ„๋„์„ ์„ ํƒํ•˜๋ ค๋ฉด [0, 3+0]์„ channels๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

hist = cv.calcHist([img], [0, 1], None, [256, 256], [0, 256, 0, 256]) ์˜ ๊ฒฐ๊ณผ๋Š” ๊ฐ ์ฑ„๋„ ๋ณ„ ํžˆ์Šคํ† ๊ทธ๋žจ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” (256, 256)์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. x์ถ• 1 ๋ฒˆ ์ฑ„๋„, y์ถ• 2 ๋ฒˆ ์ฑ„๋„, z์ถ• ํ”ฝ์…€ ์ˆ˜๋กœ ๊ตฌ์„ฑ๋œ 3์ฐจ์› ํžˆ์Šคํ† ๊ทธ๋žจ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import cv2 as cv
from matplotlib import pyplot as plt
bgr = cv.imread('image.png')
hist1 = cv.calcHist([bgr], [0], None, [256], [0, 256])
hist2 = cv.calcHist([bgr], [1], None, [256], [0, 256])
hist3 = cv.calcHist([bgr], [2], None, [256], [0, 256])
rgb = cv.cvtColor(bgr, cv.COLOR_BGR2RGB)
plt.subplot(1, 2, 1)
plt.imshow(rgb)
plt.subplot(1, 2, 2)
plt.plot(hist1, 'b')
plt.plot(hist2, 'g')
plt.plot(hist3, 'r')
plt.show()

์ด์น˜ํ™”

cv.threshold

retval, dst = cv.threshold(src, thresh, maxval, type, dst=None)
  • @param src: np.ndarray 1 ์ฑ„๋„ ์ด๋ฏธ์ง€, ์ฃผ๋กœ grayscale
  • @param thresh: float ์—ญ์น˜
  • @param maxval: float ์—ญ์น˜๋ฅผ ๋„˜์œผ๋ฉด ์ ์šฉํ•  ๊ฐ’
  • @param type
    • cv.THRESH_BINARY, thresh๋ฅผ ๋„˜์œผ๋ฉด ๋ฐฑ(maxval), ์•„๋‹ˆ๋ฉด ํ‘(0)
    • cv.THRESH_BINARY_INV
    • cv.THRESH_TRUNC, thresh๋ฅผ ๋„˜์œผ๋ฉด ๋ฐฑ(thresh), ๋‚˜๋จธ์ง€ ๊ทธ๋Œ€๋กœ
    • cv.THRESH_TOZERO, thresh๋ฅผ ๋ชป ๋„˜์œผ๋ฉด ํ‘(0), ๋‚˜๋จธ์ง€ ๊ทธ๋Œ€๋กœ
    • cv.THRESH_TOZERO_INV
    • cv.THRESH_OTSU, Otsu ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ thresh ๊ฒฐ์ •
    • cv.THRESH_TRIANGLE, Triangle ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ thresh ๊ฒฐ์ •
  • @return retval cv.THRESH_OTSU ๋˜๋Š” cv.THRESH_TRIANGLE์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ, ์ ์šฉ๋œ ์—ญ์น˜ ๊ฐ’
import cv2 as cv
from matplotlib import pyplot as plt
gray = cv.imread('image.png', cv.IMREAD_GRAYSCALE)
retval, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
plt.subplot(1, 2, 1)
plt.imshow(gray, 'gray')
plt.subplot(1, 2, 2)
plt.imshow(thresh, 'gray')
plt.show()

cv.adaptiveThreshold

dst = cv.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C, dst=None)
  • @param src: np.ndarray grayscale
  • @param maxValue: float ์—ญ์น˜๋ฅผ ๋„˜์œผ๋ฉด ์ ์šฉํ•  ๊ฐ’
  • @param adaptiveMethod
    • cv.ADAPTIVE_THRESH_MEAN_C
    • cv.ADAPTIVE_THRESH_GAUSSIAN_C
  • @param thresholdType
    • cv.THRESH_BINARY, thresh๋ฅผ ๋„˜์œผ๋ฉด ๋ฐฑ(maxValue), ์•„๋‹ˆ๋ฉด ํ‘(0)
    • cv.THRESH_BINARY_INV
  • @param blockSize: int ์—ญ์น˜๋ฅผ ๊ตฌํ•  ๋•Œ ์‚ฌ์šฉํ•  kernel์˜ ๊ฐ€๋กœ ๋˜๋Š” ์„ธ๋กœ ๊ธธ์ด, 3 ์ด์ƒ ํ™€์ˆ˜
  • @param C: int ์—ฐ์‚ฐ์— ํ•„์š”ํ•œ ์ƒ์ˆ˜, ์ผ๋ฐ˜์ ์œผ๋กœ ์–‘์ˆ˜
import cv2 as cv
from matplotlib import pyplot as plt
gray = cv.imread('image.jpg', cv.IMREAD_GRAYSCALE)
thresh = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 45, 2)
plt.subplot(2, 1, 1)
plt.imshow(gray, 'gray')
plt.subplot(2, 1, 2)
plt.imshow(thresh, 'gray')
plt.show()

ํ‰ํ™œํ™”

dst = cv.equalizeHist(src, dst=None)
class cv.createCLAHE(clipLimit=40.0, tileGridSize=(8, 8))
dst = cv.CLAHE.apply(src, dst=None)
Last updated on