38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Fri Apr 1 18:14:57 2022
|
|
|
|
@author: Kenan
|
|
Messen wie lange konvertierung in numpy dauert
|
|
"""
|
|
import numpy as np
|
|
import time
|
|
|
|
image_width=192
|
|
image_heigth = 160
|
|
number_of_colorchannels=3
|
|
|
|
def convert_rgb_to_grayscale_average(image_bgr):
|
|
"""This function converts the RGB image into an grayscale image.
|
|
Algorithm: Average: Y = (R+G+B)/3"""
|
|
|
|
# convert dtype to prevent integer overflow while addition
|
|
image_bgr = image_bgr.astype(np.uint16, copy=False)
|
|
|
|
image_gray = (image_bgr[:,:,0]+image_bgr[:,:,1]+image_bgr[:,:,2])/3 # add values / do conversion
|
|
image_gray = image_gray.astype(np.uint8, copy=False) # convert back to uint8
|
|
|
|
return image_gray
|
|
|
|
int_black = 120 # integer for black color/ no color
|
|
image_bgr= np.full(\
|
|
(image_heigth,image_width, number_of_colorchannels), \
|
|
int_black, dtype=np.uint8)
|
|
|
|
start_m1 = time.perf_counter()
|
|
image_gray = convert_rgb_to_grayscale_average(image_bgr)
|
|
end_m1 = time.perf_counter()
|
|
time_processing = end_m1-start_m1
|
|
time_processing = time_processing*1000
|
|
time_processing = round(time_processing, 2)
|
|
print(f'processing time conversion: {time_processing} ms') |