<jupyter_start><jupyter_text>Intro to very basic OpenCV So, what is openCV?OpenCV is essentially a python library for computer vision (real time, image processing / altering, image read/write, etc) which builds off numpy.<jupyter_code>import cv2 #import statement<jupyter_output><empty_output><jupyter_text>This is a function called from the cv2 module called `imread` - this reads the image located at the directory specified inside the command. In this example, it looks inside the imgs folder for the Tino.png fileThe img is saved as a matrix (w x l x 3) - width pixels by length pixels by 3 channels for R, G, and B.Because the img is saved as a matrix, we can call many functions on it later in the program.<jupyter_code>img = cv2.imread("imgs/Tino.png")<jupyter_output><empty_output><jupyter_text>Now, let's show the code using cv2's imshow method. This method shows the `img` in a new window with the name 'image' (which can really be anything, even something like 'banana' or 'jeffrey' or something. If you wanted to change the image being shown or the name, edit the fields.Here is what the code on the bottom does.1. This will display the image in a new window with the name 'image'.2. Then, it will wait until some key is pressed from the user side.3. Once that key is pressed, it goes to the next step, which is destroying all open windows.This is a necessary trio in cv2. If you want to show an image, you can have the imshow method as many times as you like before a waitKey and destroyAllWindows command.<jupyter_code>cv2.imshow('image',img) # Showing the image in a new window labeled 'image'
cv2.waitKey(0) # Wait until some key is pressed
cv2.destroyAllWindows() # Then, destroy all the open windows<jupyter_output><empty_output><jupyter_text>Now, we will read the same image again, but with a flag which specifies __how__ the image should be read. This is passed as a second parameter to the cv2.imread function - it is optional, but alters how the image is read.Here are some examples of different flags.* cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.* cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode* cv2.IMREAD_UNCHANGED : Loads image as such including alpha channelThere are many, many more flags for reading images which we will use later, but these 3 are the essentials.Let's try to read the tino image in grayscale, which is one of the popular flags (cv2.IMREAD_GRAYSCALE), and save it to an image called grayscale_img<jupyter_code>grayscaled_img = cv2.imread("imgs/Tino.png", cv2.IMREAD_GRAYSCALE) # Reading the image as grayscale<jupyter_output><empty_output><jupyter_text>Here, we run the same code as we did previously, but with a different title and image so we can see the grayscaled image<jupyter_code># Same display code but with the grayscaled image
cv2.imshow('grayscaled',grayscaled_img) # Showing the grayscaled image in a new window labeled 'grayscaled'
cv2.waitKey(0) # Wait until some key is pressed
cv2.destroyAllWindows() # Then, destroy all the open windows<jupyter_output><empty_output><jupyter_text>It is possible to display multiple images at one time in different windows. Let's take a look. You can also display multiple images at once in cv2, assuming you keep the end 2 commands to detect when the user presses a key (waitKey) and then destroy the windows when the user presses a key(destroyAllWindows() )<jupyter_code>cv2.imshow('image',img) ## Original image
cv2.imshow('grayscaled',grayscaled_img) # Grayscaled image
cv2.waitKey(0) # Wait until some key is pressed
cv2.destroyAllWindows() # Then, destroy all the open windows<jupyter_output><empty_output><jupyter_text>We can also write openCV images to the os directory using imwrite, which accepts a path and an image to write. Outputs if the operation was successful or not<jupyter_code>#Writing the grayscale image of this github
cv2.imwrite("imgs/gray_Tino.PNG", grayscaled_img)<jupyter_output><empty_output><jupyter_text>Now, let's move to something complicatedMouse events in OpenCV are quite fun to use and useful in some cases.Now, let's make a function which will allow us to draw circles on our image and clear those circles if wanted.<jupyter_code>import cv2
import numpy as np # Extremely useful library

windowname='Drawing' # Defining the window name beforehand in a constant

def drawcircle(event, x, y, flags, param): 
    '''
    This is a drawCircle function. A lot of the parameters here are only necessary because of the fact 
    that we are mapping it to the mouse.
    The only actual necessary parameters are event, which is the mouse event which is performed, and x and y, the location
    
    '''
    if event == cv2.EVENT_LBUTTONDOWN:  # if left button is clicked once
        # draw a circle at x and y on image with the radius 20 and RGB Color (0, 255, 0).
        # The last value, -1, means that the circle will be filled. 
        #If the value is 1, the circle will not be filled.
        # If the value is 0, the circle will not be drawn at all.
        cv2.circle(img, (x,y), 20, (0,255,0), 1)
    elif event == cv2.EVENT_LBUTTONDBLCLK: # if double clicked of left button
        # draw a circle at x and y on image with the radius 40 and RGB Color (255, 0, 0).
        cv2.circle(img, (x,y), 40, (255,0,0), -1)
    elif event == cv2.EVENT_RBUTTONDOWN: # If right clicked
        # draw a circle at x and y on image with the radius 60 and RGB Color (0, 0, 255).
        cv2.circle(img, (x,y), 60, (0,0,255), -1)
    elif event == cv2.EVENT_MBUTTONDOWN: # If middle button clicked
        # Wipe entire screen (matrix operations come in handy here)
        img[:] = [0,0,0]
            
    
# binding this mouse function to the previously defined window
cv2.setMouseCallback(windowname,drawcircle)

# Main method
def main():
    # Until the user presses a key, show the image in the window
    # Allows the user to use the mouse functions binded to the window by displaying it
    cv2.imshow(windowname, img) #Shows the tino image if you have not altered the program
    cv2.waitKey(0)
    # Destroy all windows once a key is pressed
    cv2.destroyAllWindows()
        
# Run main method
if __name__ == "__main__":
    main()<jupyter_output><empty_output><jupyter_text>Using trackbarsWant to create your own custom RGB Palette?<jupyter_code># importing needed libraries
# An empty unneeded function for when the trackbar is used. DW about this
def emptyFunction(self):
    pass

def main():
    #Creating a black img, 512 x 512 with 3 dimensions for R, G, and B
    img = np.zeros((512, 512, 3), np.uint8)
    
    #Creating a window and assigning a universal window name (this is unnecessary, but convenient)
    windowName = 'OpenCV BGR Color Palette'
    cv2.namedWindow(windowName)
    
    # Creating the trackbars (this uses the createTrackbar method from openCV)
    # This function accepts 4 parameters:
    # 1. The name of the window ('R', 'G', or 'B' in our case)
    # 2. The name of the window which we have created
    # 3. The lower end of our trackbar(min value)
    # 4. The higher end of our trackbar (max value)
    # 5. A function to be called when the user uses the trackbar (in our case, we do nothing)
    cv2.createTrackbar('R', windowName, 0, 255, emptyFunction)
    cv2.createTrackbar('G', windowName, 0, 255, emptyFunction)
    cv2.createTrackbar('B', windowName, 0, 255, emptyFunction)
    
    # Actual function
    # We need to use a while loop here because we cannot display and get values for the trackbars
    # with our other method. We need a while loop to do multiply actions and break if something's pressed
    while(True):
        # Show the window
        cv2.imshow(windowName, img)
        
        # If any key is hit, break
        if cv2.waitKey():
            break
        
        #getting RGB values based on trackbar locations
        # Getting the trackbar position (and hence our b, g, or r values) based on their location 
        # in the window assigned earlier.
        blue = cv2.getTrackbarPos('B', windowName)
        green = cv2.getTrackbarPos('G', windowName)
        red = cv2.getTrackbarPos('R', windowName)
        
        
        # Setting RGB Values into image (matrix operations - I told you images being saved as matrices would be helpful...)
        img[:] = [red, green, blue]
        print(img)
        
        
    # Close window    
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07011156782149149, 'fraction_numerical': 0.016559013505578393, 'mean_word_length': 3.6997792494481234, 'pattern_counts': {'":': 2, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '648486', 'license': 'MIT', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2449, 'n_words': 1162, 'path': 'Week_01/Intro to basic opencv.ipynb', 'repo_name': 'PranavEranki/Computer-Vision-Robotics', 'size': '8515'}