Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
OpenCV (4.1.1)
1, read and save the picture
import numpy as np
import cv2
from matplotlib import pyplot as plt
# img = cv2.imread("test.jpg",0) # 灰度图
img = cv2.imread("opencv_test_picture/read.jpg")
# cv2.imshow('image',img) 显示不出来
# plt.imshow(img)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()
cv2.imwrite("opencv_test_picture/write.jpg", img)
print(img.shape)
print(img.size) # 返回图像的像素数目
print(img.dtype)# 图像的数据类型
2, add a logo
# encoding: utf-8
"""
@author: sunxianpeng
@file: operation.py
@time: 2019/10/25 15:22
"""
import numpy as np
import cv2
from matplotlib import pyplot as plt
class Main():
def __init__(self):
pass
def read_pic(self,path):
# img = cv2.imread("test.jpg",0) # 灰度图
img = cv2.imread(path)
return img
def show_pic(self,img):
# cv2.imshow('image',img) 显示不出来
# plt.imshow(img)
plt.imshow(img, cmap='gray', interpolation='bicubic')
# plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()
def write_pic(self,img, path):
cv2.imwrite(path, img)
def add_logo(self):
# 加载图像
img1 = m.read_pic(path1)
img2 = m.read_pic(path2)
# 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 inverse mask also
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 175, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
# Now black-out the area of logo in ROI
# 取 roi 中与 mask 中不为零的值对应的像素的值,其他值为 0
# 注意这里必须有 mask=mask 或者 mask=mask_inv, 其中的 mask= 不能忽略
print(roi.dtype)
print(mask.dtype)
print(mask.shape)
print(roi.shape)
img1_bg = cv2.bitwise_and(roi, roi, mask=mask)
# img1_bg = cv2.bitwise_and(roi, roi)
# # 取 roi 中与 mask_inv 中不为零的值对应的像素的值,其他值为 0。
# # Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2, img2, mask=mask_inv)
# img2_fg = cv2.bitwise_and(img2, img2)
# # Put logo in ROI and modify the main image
dst = cv2.add(img1_bg, img2_fg)
img1[0:rows, 0:cols] = dst
m.show_pic(img1)
if __name__ == '__main__':
m = Main()
path1 = r"F:\PythonProjects\python_study\xiaohulu\image_process\data\ball.jpg"
path2 = r"F:\PythonProjects\python_study\xiaohulu\image_process\data\opencv_logo3.jpg"
m.add_logo()
problem
error: OpenCV(4.1.1) /io/opencv/modules/core/src/arithm.cpp:663: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'arithm_op'
Solution:
logo image must be less than the number of rows and columns of non-logo picture
1, view images are the same size
2, the image data types are consistent view (.dtype view .uint8 issued type and other types are the same)
3, see the image mask meets the requirements (to be grayscale image)