Drawing Histogram in OpenCV-Python

后端 未结 1 1920
自闭症患者
自闭症患者 2021-02-01 23:18

I was just trying to draw histogram using new OpenCV Python interface ( cv2 ).

Below is the code i tried:

import cv2
import numpy as np
import time

img          


        
1条回答
  •  臣服心动
    2021-02-01 23:23

    You should copy the array:

    b,g,r = img[:,:,0].copy(), img[:,:,1].copy(), img[:,:,2].copy()
    

    But, since calcHist() can accept channels parameter, you need not to split your img to three array.

    import cv2
    import numpy as np
    
    img = cv2.imread('zzzyj.jpg')
    h = np.zeros((300,256,3))
    
    bins = np.arange(256).reshape(256,1)
    color = [ (255,0,0),(0,255,0),(0,0,255) ]
    
    for ch, col in enumerate(color):
        hist_item = cv2.calcHist([img],[ch],None,[256],[0,255])
        cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
        hist=np.int32(np.around(hist_item))
        pts = np.column_stack((bins,hist))
        cv2.polylines(h,[pts],False,col)
    
    h=np.flipud(h)
    
    cv2.imshow('colorhist',h)
    cv2.waitKey(0)
    

    0 讨论(0)
提交回复
热议问题