Splitting RGB image into R, G, B channels - python

I am working on image processing, I want to know if this code will split the color image into different channels and give me an average value. Because when I tried to give it the image that I am reading, it gives me blue, green, red values, as well as the average value. When I try to add it to the list and try to print it, then the list contains only zeros.

This is my code:

b, g, r = cv2.split(re_img1)
ttl = re_img1.size
B = sum(b) / ttl
G = sum(g) / ttl
R = sum(r) / ttl
B_mean1.append(B)
G_mean1.append(G)
R_mean1.append(R)

re_img1is the resized image (i.e. 256x256). The image can be any. And I use the same code in 2 different functions, and I ran into the same problem.

Any suggestions are welcome! Thanks in advance!

+4
source share
2

, RGB. 2 :

  • ttl 3, , X (: 256X256 RGB, 196608).
  • b, g r numpy.ndarray, , .. ndarray.sum. float, , 2 ints int.

    import cv2
    import numpy as np
    re_img1 = cv2.imread('re_img1.png')
    b, g, r = cv2.split(re_img1)
    
    ttl = re_img1.size / 3 #divide by 3 to get the number of image PIXELS
    
    """b, g, and r are actually numpy.ndarray types,
    so you need to use the appropriate method to sum
    all array elements"""
    B = float(np.sum(b)) / ttl #convert to float, as B, G, and R will otherwise be int
    G = float(np.sum(g)) / ttl
    R = float(np.sum(r)) / ttl
    B_mean1 = list()
    G_mean1 = list()
    R_mean1 = list()
    B_mean1.append(B)
    G_mean1.append(G)
    R_mean1.append(R)
    

, . !

+6

, :

ttl = re_img1.size

:

ttl = re_img1.size[0] #This only works correctly if the img is a perfect square

img.size (x, y), ( ), .

0

Source: https://habr.com/ru/post/1537393/


All Articles