# -*- coding: utf-8 -*- """ Created on Mon Mar 21 17:49:58 2022 @author: Kenan """ import cv2 as cv import numpy as np from matplotlib import pyplot as plt path = r"U:\bwsyncshare\image.png" image_bgr = cv.imread(path, cv.IMREAD_COLOR) # load original image image_b,image_g,image_r = cv.split(image_bgr) # Split colour channels and get grayscale images image_r = image_r.astype(np.uint16) image_g = image_g.astype(np.uint16) image_b = image_b.astype(np.uint16) img_rgb=image_bgr[:,:,::-1] image_grayscale = cv.cvtColor(image_bgr, cv.COLOR_BGR2GRAY) # Y = 0.299 R + 0.587 G + 0.114 B image_grayscale_KG = (image_r+image_g+image_b)/3 image_grayscale_KG = image_grayscale_KG.astype(int) # quick and dirty way to convert to uint8 array image_column = 6 image_row = 243 image_bgr_b = image_bgr[image_column, image_row, 0] image_b_b = image_b[image_column, image_row] plt.imshow(img_rgb) # get pixel info from specific color channel print(image_b.shape[0]) # 416 columns print(image_b.shape[1]) # 320 rows #image_b[row, column] print(image_b[230, 20]) #image_bgr[row, column, dim] print(image_bgr[230,20,0]) # Test for addition of colors row = 245 column = 70 print('Color addition:') blue = image_bgr[row,column, 0] green = image_bgr[row,column, 1] red = image_bgr[row,column, 2] print(f'blue: {blue}') print(f'green: {green}') print(f'red: {red}') grayscale = image_grayscale[row, column] grayscale_AVG = image_grayscale_KG[row, column] print(f'gray Y: {grayscale}') print(f'gray AVG: {grayscale_AVG}') image_r = image_r.astype(np.uint16) image_g = image_g.astype(np.uint16) image_b = image_b.astype(np.uint16) res = image_r[row, column]+image_g[row, column]+image_b[row, column] print(f"addition of r g b: {res}")