OpenCV in Python. Part 3

Hello, Habr! This is a continuation of the tutorial on the opencv library in python. For those who have not read the first and second parts, here: Part 1 and Part 2 , and everyone else - pleasant reading!













Introduction



In this part we will look at image arithmetic, channel splitting and merging, various blurring methods.







Image arithmetic



, , .

, RGB , [0,255]. , , 250 30 70 100? , 280 -30 . , RGB , 8- , 280 -30 . , , , :







print("opencv addition: {}".format(cv2.add(np.uint8([250]), 
                                                   np.uint8([30]))))
print("opencv subtract: {}".format(cv2.subtract(np.uint8([70]), 
                                                    np.uint8([100]))))
print("numpy addition: {}".format(np.uint8([250]) + np.uint8([30])))
print("numpy subtract: {}".format(np.uint8([70]) - np.uint8([71])))
      
      





, opencv add subtract , numpy. :







opencv addition: 255
opencv subtract: 0
numpy addition: 24
numpy subtract: 255
      
      





OpenCV , [0,255]. numpy . , 60 255. , 255 0, , 0 ( ) 255.









, RGB , . , ? opencv โ€” split():







image = cv2.imread('rectangles.png')
b, g, r = cv2.split(image)
cv2.imshow('blue', b)
cv2.imshow('green', g)
cv2.imshow('red', r)
      
      





. :













. :













, . :













, . , . , , . , .

, , merge(), :







merge_image = cv2.merge([g,b,r])
cv2.imshow('merge_image', merge_image)
cv2.imshow('original', image)
cv2.waitKey(0)
      
      











, , , .









โ€” , . opencv : averaging(), gaussian() median().







Averaging



, โ€” , . โ€” , , . , blur(), , :







def averaging_blurring():
    image = cv2.imread('girl.jpg')
    img_blur_3 = cv2.blur(image, (3, 3))
    img_blur_7 = cv2.blur(image, (7, 7))
    img_blur_11 = cv2.blur(image, (11, 11))
      
      





, :













Gaussian



, , , , , ยซยป . , :













opencv GaussianBlur(), , . 0, opencv , :







def gaussian_blurring():
    image = cv2.imread('girl.jpg')
    img_blur_3 = cv2.GaussianBlur(image, (3, 3), 0)
    img_blur_7 = cv2.GaussianBlur(image, (7, 7), 0)
    img_blur_11 = cv2.GaussianBlur(image, (11, 11), 0)
      
      





Median



A median blur replaces the center pixel of an image with the median of all pixels in the core region, making the blur most effective at removing salt-style noise. In order to apply this type of blur, you need to call the medianBlur () function and pass two parameters there: the image and the kernel size:







def median_blurring():
    image = cv2.imread('girl.jpg')
    img_blur_3 = cv2.medianBlur(image, 3)
    img_blur_7 = cv2.medianBlur(image, 7)
    img_blur_11 = cv2.medianBlur(image, 11)
      
      





As a result, we get the following:













This concludes this part. The code, as always, is available on github . See you soon:)








All Articles