python - RGB matrix of an image

后端 未结 5 1102
花落未央
花落未央 2020-12-24 13:02

Taking an image as input, how can I get the rgb matrix corresponding to it? I checked out the numpy.asarray function. Does that give me the rgb matrix or some other matrix?

5条回答
  •  生来不讨喜
    2020-12-24 13:31

    Also to add, if you or anyone else is using opencv.

     imgc=cv2.imread(file)
    

    or to read in as grayscale

     imgc=cv2.imread(file,0)
    

    If you will be doing some comparison between the images you may want to think about turning the array of pixels into histograms to normalise the data.

       hist = np.histogram(img.flatten(),256,[0,256])[0]
    

    The above line firstly flattens your img array so you do lose the dimensionality of your image. It then produces bins from 0 to 256 (for the grayscale image) and adds the counts from the img to these bins and returns them as hist which can then be plotted. For example, if the 100 bin has a value of 20 it means that 20 pixels in your image had a value of 100.

    Hope this adds another possiblity to think about or to anyone looking to get started in opencv.

提交回复
热议问题