Random BGR Color Generator

  • Share this:

Code introduction


This function uses numpy and random libraries to generate a random BGR color.


Technology Stack : numpy, random

Code Type : Function

Code Difficulty :


                
                    
import cv2
import numpy as np
import random

def random_color():
    """
    This function generates a random color in BGR format.
    """
    # Define the range for each channel of the color
    lower_bound = 0
    upper_bound = 255
    
    # Generate a random color
    b = random.randint(lower_bound, upper_bound)
    g = random.randint(lower_bound, upper_bound)
    r = random.randint(lower_bound, upper_bound)
    
    return (b, g, r)

# Example usage
color = random_color()
print(f"Random color: {color}")                
              
Tags: