...........
代码和现象
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
#os.chdir('C:/Users/lenovo/Pictures/')
# 1. 读取图片
img1 = cv2.imread("./pic/moon.jpg") #读取沙漠图片
img2 = cv2.imread("./pic/logo2.jpg") #读取logo图片
#img2 = cv2.imread('opencv_logo.jpg',0) #也可以读取logo的时候直接灰度化
# 2. 根据logo大小提取感兴趣区域roi
# 把logo放在左上角,提取原图中要放置logo的区域roi
rows, cols = img2.shape[:2]
roi = img1[:rows, :cols]
# 3. 创建掩膜mask
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) #将图片灰度化,如果在读取logo时直接灰度化,该步骤可省略
#cv2.THRESH_BINARY:如果一个像素值低于200,则像素值转换为255(白色色素值),否则转换成0(黑色色素值)
#即有内容的地方为黑色0,无内容的地方为白色255.
#白色的地方还是白色,除了白色的地方全变成黑色
ret, mask = cv2.threshold(img2gray, 175, 255, cv2.THRESH_BINARY)#阙值操作
mask_inv = cv2.bitwise_not(mask) #与mask颜色相反,白色变成黑色,黑变白
# 4. logo与感兴趣区域roi融合
# 保留除logo外的背景
img1_bg = cv2.bitwise_and(roi, roi, mask=mask)
img2_fg = cv2.bitwise_and(img2,img2,mask=mask_inv)
dst = cv2.add(img1_bg, img2_fg) # logo与感兴趣区域roi进行融合
img1[:rows, :cols] = dst # 将融合后的区域放进原图
img_new_add = img1.copy() #对处理后的图像进行拷贝
# 5. 显示每步处理后的图片
'''
# 显示图片,调用opencv展示
cv2.imshow('logo',img2)
cv2.imshow('logo_gray',img2gray)
cv2.imshow('logo_mask',mask)
cv2.imshow('logo_mask_inv',mask_inv)
cv2.imshow('roi',roi)
cv2.imshow('img1_bg',img1_bg)
cv2.imshow('img2_fg',img2_fg)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
'''
# cv2与matplotlib的图像颜色模式转换,cv2是BGR格式,matplotlib是RGB格式
def img_convert(cv2_img):
# 灰度图片直接返回
if len(cv2_img.shape) == 2:
return cv2_img
# 3通道的BGR图片
elif len(cv2_img.shape) == 3 and cv2_img.shape[2] == 3:
b, g, r = cv2.split(cv2_img) #分离原图像通道
return cv2.merge((r, g, b)) #合并新的图像通道
# 4通道的BGR图片
elif len(cv2_img.shape) == 3 and cv2_img.shape[2] == 4:
b, g, r, a = cv2.split(cv2_img)
return cv2.merge((r, g, b, a))
# 未知图片格式
else:
return cv2_img
# 显示图片,调用matplotlib展示
titles = ['logo','logo_gray','logo_mask','logo_mask_inv','roi','img1_bg','img2_fg','dst']
imgs = [img2,img2gray,mask,mask_inv,roi,img1_bg,img2_fg,dst]
for i in range(len(imgs)):
plt.subplot(2,4,i+1),plt.imshow(img_convert(imgs[i]),'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
# 显示并保存加logo的图片
cv2.imshow('img_new_add',img_new_add)
cv2.imwrite('img_new_add.jpg',img_new_add)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
import matplotlib.pyplot as plt
imgcat = cv2.imread("./pic/moon.jpg")
imglogo = cv2.imread("./pic/sspu55.png")
moon_cut = cv2.imread("./pic/moon_cut.png")
width,height = imglogo.shape[0:2]
#cv2.imshow('原图', imgcat)
#胡椒噪音,中值滤波
imgcat = cv2.medianBlur(imgcat, 5)
moon_cut = cv2.medianBlur(moon_cut, 5)
#cv2.imshow('lvbo', imgcat)
#灰度图
imglogotr = cv2.cvtColor(imglogo,cv2.COLOR_BGR2GRAY)
imgcattr1 = cv2.cvtColor(imgcat,cv2.COLOR_BGR2GRAY)
moon_g = cv2.cvtColor(moon_cut,cv2.COLOR_BGR2GRAY)
#cv2.imshow('imglogotr',imglogotr)
#cv2.imshow('imgcattr1',imgcattr1)
#二值化(返回值,阈值和图)
ret,imglogoms = cv2.threshold(imglogotr,96,255,cv2.THRESH_BINARY_INV)
ret,imgcatms = cv2.threshold(imgcattr1,96,200,cv2.THRESH_BINARY_INV)
ret,moon_b= cv2.threshold(moon_g,96,200,cv2.THRESH_BINARY_INV)
#cv2.imshow('moon',moon_b)
#cv2.imshow('imglogoms',imglogoms)
#cv2.imshow('imgcatms',imgcatms)
#cv2.imshow('two',imgcatms)
imglogotr = cv2.add(imglogo,imglogo,mask=imglogoms)
imglogoms = cv2.bitwise_not(imglogoms)
#cv2.imshow('imglogoms',imglogoms)
#cv2.imshow('imglogotr0',imglogotr)
imgcattr = imgcat[0+130:160+130, 0+170:160+170]
imgcatzero = np.zeros((width,height,3),dtype=np.uint8)
imgcattr = cv2.add(imgcattr,imgcatzero,mask=imglogoms)
#cv2.imshow('imgcattr',imgcattr)
#cv2.imshow('imgcatzero',imgcatzero)
#查找轮廓,绘制,计算面积,周长
contours, hierarchy = cv2.findContours(moon_b, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cir = cv2.drawContours(moon_cut, contours,1, (0, 0, 255), 2)
area = cv2.contourArea(contours[1])
primeter = cv2.arcLength(contours[1], closed=True)
cv2.imshow('cir', cir)
#把计算结果写道图片上
cv2.putText(imgcat,'moon area: 20065.5',(30,30), cv2.FONT_HERSHEY_COMPLEX_SMALL,1,[0,255,0])
cv2.putText(imgcat,'moon primeter: 530.114785',(30,50), cv2.FONT_HERSHEY_COMPLEX_SMALL,1,[0,255,0])
print ('area',primeter)
imgadd = cv2.add(imgcattr,imglogotr)
imgcat[0+130:160+130, 0+170:160+170] = imgadd
cv2.imshow('final',imgcat)
cv2.waitKey(0)
cv2.destroyAllWindows()
四.系列文章,其他模块可直接参考开头的链接目录