<jupyter_start><jupyter_text>OpenCV tutorial on Jupyter Notebook A few examples of OpenCV installation include:* `pip install opencv-python`* `conda install -c conda-forge opencv`  Import modules<jupyter_code>import cv2
print(cv2.__version__)
import os
import numpy as np
print(np.__version__)<jupyter_output>3.4.3
1.14.3<jupyter_text>Handling Images<jupyter_code>image_path = os.path.join('..','images','opencv_SeanProfile.jpg')
img = cv2.imread(image_path,1)<jupyter_output><empty_output><jupyter_text>Loading, processing, writing<jupyter_code># loading image
img = cv2.imread(image_path,1)
# displaying image
cv2.imshow('image',img) #Image appears and disappear automatically
# display the image indefinitely until the keyboard input (any key)
cv2.waitKey(0)
# destroy all opened windows
cv2.destroyAllWindows()
cv2.waitKey(1) # in order to close the opened window --> does not close 
# Convert the image to gray
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# saving image
cv2.imwrite(os.path.join('..','images','opencv_SeanProfile_gray.png'),img)<jupyter_output><empty_output><jupyter_text>Obtain properties of the image<jupyter_code>img = cv2.imread(image_path,1)
rows,cols,channel = img.shape
print(img.shape) # height (rows), width (cols), channel
print('image height (rows): {}\nimage width (cols): {}\nimage channel: {}'.format(rows,cols,channel))<jupyter_output>(717, 537, 3)
image height (rows): 717
image width (cols): 537
image channel: 3<jupyter_text>Resizing<jupyter_code>img = cv2.imread(image_path,1)
img = cv2.resize(img,(700,400)) # width (cols) and height (rows)
print(img.shape)
cv2.imshow('image',img) 
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.imwrite(os.path.join('..','images','opencv_resized_example.png'),img)<jupyter_output>(400, 700, 3)<jupyter_text>Insert geometric shapes on the image<jupyter_code>img = cv2.imread(image_path,1)
cv2.line(img,(0,0),(100,100),(255,0,255),2)
cv2.circle(img,(300,300),50,(0,255,255),5)
cv2.circle(img,(100,450),30,(100,100,255),-1)
cv2.rectangle(img,(200,600),(450,700),(255,100,100),5)
cv2.arrowedLine(img,(300,200),(500,200),(0,0,255),6)
cv2.putText(img,"Sean!!!", (100,150), cv2.FONT_HERSHEY_SIMPLEX,3,(255,255,255),cv2.LINE_AA)
cv2.imshow('image',img) 
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.imwrite(os.path.join('..','images','opencv_shapes_example.png'),img)<jupyter_output><empty_output><jupyter_text>Find pixel coordinate of the image<jupyter_code>def on_click(c_event,x,y,flags,param):

    if c_event == cv2.EVENT_LBUTTONDOWN:
        text = '({},{})'.format(x,y)
        cv2.circle(img,(x,y),5,(0,0,255),-1)
        cv2.line(img,(x,0),(x,rows),(0,0,255),1,4)
        cv2.line(img,(0,y),(cols,y),(0,0,255),1,4)
        cv2.putText(img,text,(x+10,y),cv2.FONT_HERSHEY_PLAIN,1.5,(0,0,255),2,cv2.LINE_AA)
        cv2.imshow('image',img)
        
img = cv2.imread(image_path,1)
cv2.namedWindow('image')
clean_img = img.copy()
cv2.setMouseCallback('image',on_click)

while True:
    cv2.imshow('image',img)  
    key = cv2.waitKey(1) & 0xFF    
    if key == ord('r'):
        img = clean_img.copy()
    if key == ord('q'):
        break
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.imwrite(os.path.join('..','images','opencv_getting_coord_example.png'),img)<jupyter_output><empty_output><jupyter_text>Simple processing - blurring to remove noise<jupyter_code>## other types include: Gaussian blurring, median blurring
img = cv2.imread(image_path,1)
# blurring by averaging
img_blur = cv2.blur(img,(5,5))
# blurring while preserving the edge
img_bf = cv2.bilateralFilter(img,9, 75, 75)
cv2.imshow('image',img) 
cv2.waitKey(0)
cv2.imshow('image_blur',img_blur) 
cv2.waitKey(0)
cv2.imshow('image_bf',img_bf) 
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.imwrite(os.path.join('..','images','opencv_blur_example.png'),img_blur)
cv2.imwrite(os.path.join('..','images','opencv_bf_example.png'),img_bf)<jupyter_output><empty_output><jupyter_text>Simple processing - edge detection<jupyter_code>## other types include: sobel edge detection, Laplacian edge detection
img = cv2.imread(image_path,1)
img_canny = cv2.Canny(img,50,100)
cv2.imshow('image',img) 
cv2.waitKey(0)
cv2.imshow('image_canny',img_canny) 
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.imwrite(os.path.join('..','images','opencv_canny_edge_example.png'),img_canny)<jupyter_output><empty_output><jupyter_text>Simple processing - threshold filtering<jupyter_code># the source image should be a grayscale image
# other types of global th include: cv2.THRESH_BINARY_INV and cv2.THRESH_TOZERO_INV
# other types of adaptive th include: cv2.ADAPTIVE_THRESH_GAUSSIAN_C

img = cv2.imread(image_path,0)
# 0 for below th and 1 for above th
ret,th_binary = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
# keep the same value up to th and set th for all the higher values --> similar to low-pass filter
ret,th_trunc = cv2.threshold(img,100,255,cv2.THRESH_TRUNC)
# 0 for below th and keep the same value for above th
ret,th_tozero = cv2.threshold(img,100,255,cv2.THRESH_TOZERO)
# adaptive filtering calculates th for small regions of the image --> better result for an image with varying illuminations
th_adap_mean = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)

cv2.imshow('image_binary',th_binary) 
cv2.waitKey(0)
cv2.imshow('image_trunc',th_trunc) 
cv2.waitKey(0)
cv2.imshow('image_tozero',th_tozero) 
cv2.waitKey(0)
cv2.imshow('image_adap',th_adap_mean) 
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.imwrite(os.path.join('..','images','opencv_th_binary_example.png'),th_binary)
cv2.imwrite(os.path.join('..','images','opencv_th_trunc_example.png'),th_trunc)
cv2.imwrite(os.path.join('..','images','opencv_th_tozero_example.png'),th_tozero)
cv2.imwrite(os.path.join('..','images','opencv_th_adap_mean_example.png'),th_adap_mean)<jupyter_output><empty_output><jupyter_text>Optimizing the parameters<jupyter_code>img = cv2.imread(image_path,1)
cv2.namedWindow('image_canny')
img_canny = cv2.Canny(img,50,100)

cv2.createTrackbar('th1','image_canny',100,255,lambda x: None)
cv2.createTrackbar('th2','image_canny',200,255,lambda x: None)

while True:
    cv2.imshow('image_canny',img_canny)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
        
    th1 = cv2.getTrackbarPos('th1','image_canny')
    th2 = cv2.getTrackbarPos('th2','image_canny')
    img_canny = cv2.Canny(img,th1,th2)
cv2.destroyAllWindows()
cv2.waitKey(1)
print('chosen th1: {}\nchosen th2: {}'.format(th1,th2))
cv2.imwrite(os.path.join('..','images','opencv_canny_edge_optimized_example.png'),img_canny)<jupyter_output>chosen th1: 100
chosen th2: 200
{'fraction_non_alphanumeric': 0.16227608008429925, 'fraction_numerical': 0.05946108685834713, 'mean_word_length': 7.053333333333334, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 44, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '95854', 'license': 'MIT', 'n_tokens_mistral': 2860, 'n_tokens_neox': 2668, 'n_words': 406, 'path': 'Code appendix/OpenCV_tutorial.ipynb', 'repo_name': 'sungsujaing/ML_DL_articles_resources', 'size': '6643'}