Skip to content
Search

Cv2.calcHist

lastmod 2024-03-05created 2024-03-052 edits

A histogram is a way of representing the brightness distribution of an image as a graph.

About Histogram

A histogram shows the overall brightness distribution of an image and its saturation (how light or dark the colors are).

Cambridgeincolor_in_Color.jpg

In the lower graph of the figure above, the X axis is the color intensity (0 ~ 255) and the Y axis is the number of colors at each X value. From the graph alone, you can tell that the image's brightness distribution has almost no midtones and a lot of dark and bright colors. Looking at the actual original image, it matches what the graph alone suggested.

BINS

The interval of the X axis of the histogram graph. In the figure above, 0 ~ 255 is represented, so the BINS value is 256. If the BINS value is 16, the X axis is represented as 16 intervals: 0 ~ 15, 16 ~ 31..., 240 ~ 255. In OpenCV, BINS is called histSize.

Synopsis

cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])

Parameters:

  • image – image to analyze (uint8 or float32 type). In array form.
  • channels – channels to analyze (the X axis target). [0] if the image is graysacle; [0], [0,1] form for a color image (1 : Blue, 2: Green, 3: Red)
  • mask – region of the image to analyze. If None, the entire image.
  • histSize – the #BINS value. [256]
  • ranges – Range value. [0,256]

HSV Example

roi = frame[y:y+h, x:x+w]
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

channels = [0, 1]  # 분석할 채널 Index. HSV 에서 [0, 1] 이면 H 와 S 채널 이다.
hist_size = [90, 128]  # 히스토그램 x축 간격
ranges = [0, 180, 0, 256]  # 측정하고자하는 값의 범위. 즉, 여기선 채널당 값의 범위 OpenCV 의 cv2.cvtColor 로 넘어오 H 는 0~179, S 는 0~255 이다.
hist = cv2.calcHist([roi_hsv], channels, None, hist_size, ranges)

See also

Favorite site

author Zer0 sitemap