OpenCV study notes 4: core operations -- arithmetic operations

Go directly to the code, with comments:

 

import cv2

'''
Arithmetic operations on images
'''
img1 = cv2.imread('messi.jpg')
img2 = cv2.imread('opencv_logo.jpg')
img3 = cv2.imread('robot.jpg')

#Image Add
#image addition
addedImage = cv2.add(img2, img3)
cv2.imshow('Added', addedImage)
cv2.imwrite('Added.jpg', addedImage)

#Image Blending
#image blending, size must be the same
blending = cv2.addWeighted(img3, 0.8, img2, 0.2, 0)
cv2.imshow('Blending', blending)
cv2.imwrite('Blending.jpg', blending)

#Image bitwise
# Image bit operations
# I want to put logo on top-left corner, So I create a ROI
rows, cols, channels = img2.shape
roi = img1[0:rows, 0:cols]

#Now create a mask of logo and create its invers mask also
#Grayscale
#Grayscale image: The color of the image is composed of black (0) to white (255) colors, with gray in the middle. An effect similar to a black and white photo.
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# Threshold the grayscale image
# The color value of the pixel is > 200, set to 255 (white), and the others are set to 0 (black) => black and white image
ret, mask = cv2.threshold(img2gray, 200, 255, cv2.THRESH_BINARY)
#Reverse, convert black and white with mask
mask_inv = cv2.bitwise_not(mask)

#Now black-out the area of logo in roi
#When the color value of the mask pixel is not 0, roi[i] ^ roi[i]; when the color value of the mask pixel is 0, output 0.
#So the performance is to add the black part of the mask to the roi
img1_bg = cv2.bitwise_and(roi, roi, mask = mask)
#Similarly, get the image that turns the white part into black on img2
img2_fg = cv2.bitwise_and(img2, img2, mask = mask_inv)

#Add the non-white part of the logo to the ROI
bitwise = cv2.add(img1_bg, img2_fg)
#replace ROI
img1[0:rows, 0:cols] = bitwise
cv2.imshow('img2gray', img2gray)
cv2.imshow('mask', mask)
cv2.imshow('mask_inv', mask_inv)
cv2.imshow('img1_bg', img1_bg)
cv2.imshow('img2_fg', img2_fg)
cv2.imshow('BitWise-ROI', bitwise)
cv2.imshow('BitWise-Result', img1)

cv2.imwrite('img2gray.jpg', img2gray)
cv2.imwrite('mask.jpg', mask)
cv2.imwrite('mask_inv.jpg', mask_inv)
cv2.imwrite('img1_bg.jpg', img1_bg)
cv2.imwrite('img2_fg.jpg', img2_fg)
cv2.imwrite('BitWise-ROI.jpg', bitwise)
cv2.imwrite('BitWise-Result.jpg', img1)

cv2.waitKey(0)
cv2.destroyAllWindows()

 

 

-----Original image

1.messi



 

2.opencv-logo



 

3.robot



 

-----process result

1. Image addition



 

2. Image Blending



 

 

3. Image bit operations

3-1. Grayscale image



 

3-2.mask



 

3-3.mask negation



 

3-4.img1_bg



 

3-5.img2_fg



 

3-6. ROI bit operation



 

3-7. Final result



 

 

 

reference:

1.http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html#image-arithmetics

2.https://docs.opencv.org/2.4.13.5/modules/core/doc/operations_on_arrays.html#bitwise-and

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326217728&siteId=291194637