Python OpenCv学习基础知识五

Posted hhh江月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python OpenCv学习基础知识五相关的知识,希望对你有一定的参考价值。

Python OpenCv学习基础知识五

文章目录

一、图像的加法

"""
图像中的运算一、1、
"""


"""
图像加法(first...)
"""

import numpy as np

import cv2


x = np.uint8([250])

y = np.uint8([10])

print(cv2.add(x,y))



# adding, 加法。

# 250+10=260>=255

# 结果为[[255]]

print(x+y) 


# 250+10=260%255=4

# 结果为[4]

"""
[[255]]
[4]
"""

二、图像混合

"""
图像中的运算二、2、
"""


"""
图像混合(second...)
"""

import numpy as np

import cv2




img1=cv2.imread('E:\\\\\\\\1\\\\\\\\Documents\\\\\\\\PyTorch\\\\\\\\pytorch_learning\\\\\\\\others\\\\\\\\opencv_cv_2\\\\\\\\test1.jpg')
# 读取文件,绝对路径。

img2=cv2.imread('E:\\\\\\\\1\\\\\\\\Documents\\\\\\\\PyTorch\\\\\\\\pytorch_learning\\\\\\\\others\\\\\\\\opencv_cv_2\\\\\\\\test2.jpg')
# 读取文件,注意仍然是绝对路径。


row, col = img1.shape[:2]
# 获取图片一的大小

# 大小重置。



img2 = cv2.resize(img2, (col, row), interpolation=cv2.INTER_CUBIC)
# 重置大小
# 把,图片二的大小调整为与图片一大小相同,从而才可以进行合并

"""

rows, cols = sky.shape[:2] #获取sky的高度、宽度
#print(sky.shape[:2]) #(800, 1200)
#print(bear.shape[:2]) #(224, 224)
bear_dst = cv2.resize(bear,(cols,rows),interpolation=cv2.INTER_CUBIC) #放大图像
add_img = cv2.addWeighted(bear_dst,0.6,sky,0.4,0) #图像融合

# 显示图片
————————————————
版权声明:本文为CSDN博主「HONGYUAN~」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_38814578/article/details/83184742
"""

# 合并图像。



dst = cv2.addWeighted(img1,0.45,img2,0.55,0)
# 合并图像的操作。



# 进行合并,实际上就是两个图片的线性组合。


cv2.imshow('dst',dst)
# 展示

cv2.waitKey(0)
cv2.destroyAllWindows()
# 关闭



三、按位运算

"""
图像中的运算三、3、
"""


"""
按位运算(third...)
"""

import numpy as np

import cv2


img1=cv2.imread('E:\\\\\\\\1\\\\\\\\Documents\\\\\\\\PyTorch\\\\\\\\pytorch_learning\\\\\\\\others\\\\\\\\opencv_cv_2\\\\\\\\test1.jpg')
# 读取文件,绝对路径。

img2=cv2.imread('E:\\\\\\\\1\\\\\\\\Documents\\\\\\\\PyTorch\\\\\\\\pytorch_learning\\\\\\\\others\\\\\\\\opencv_cv_2\\\\\\\\test2.jpg')
# 读取文件,注意仍然是绝对路径。

# 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=不能忽略
img1_bg = cv2.bitwise_and(roi,roi,mask=mask)
#取roi中与mask_inv中不为零的值对应的像素的值,其他值为0
# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask=mask_inv)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows,0:cols] =dst

cv2.imshow('res',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()




"""

import cv2
import numpy as np
img1=cv2.imread('45.jpg')
img2=cv2.imread('messigray.png')

# 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=不能忽略
img1_bg = cv2.bitwise_and(roi,roi,mask=mask)
#取roi中与mask_inv中不为零的值对应的像素的值,其他值为0
# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask=mask_inv)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows,0:cols] =dst

cv2.imshow('res',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

"""

'''
import cv2
import numpy as np
img1=cv2.imread('45.jpg')
img2=cv2.imread('messigray.png')

# 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=不能忽略
img1_bg = cv2.bitwise_and(roi,roi,mask=mask)
#取roi中与mask_inv中不为零的值对应的像素的值,其他值为0
# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask=mask_inv)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows,0:cols] =dst

cv2.imshow('res',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
'''

四、总结

最后,谢谢大家的阅读与支持了啦。

以上是关于Python OpenCv学习基础知识五的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV学习笔记——图像平滑处理(滤波)

热视觉2:使用Python和OpenCV从图像中测量温度

Python OpenCV 图像的基础操作

OpenCV学习笔记3基础:图像基本操作

OpenCV学习笔记3基础:图像基本操作

OpenCV学习笔记3基础:图像基本操作