简体   繁体   中英

algorithm from cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)

I want to make a program to convert RGB images to grayscale in large quantities and I use this code

files = [f for f in listdir(path) if isfile(join(path,f))] 
for image in files:
    try:
        img = cv2.imread(os.path.join(path,image))
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        dstPath = join(dstpath,image)
        cv2.imwrite(dstPath,gray)
    except:
        print ("{} is not converted".format(image))

can you guys explain to me how does the work or algorithm of cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)

or more precisely how the program code snippet can convert RGB images to grayscale? how is the calculation?

does the algoritm use the formula R + G + B / 3 for all pixels in an image so its can convert to grayscale images??

The method is slightly more complicated, but you are broadly correct. The human eye is most sensitive to green light, then red, then blue. The greyscale value is calculated as the weighted average of the three channel values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM