图像的基本操作
1.可以通过下标直接访问对应的值,由于我们加载的图片大多都是 BGR的所以会反悔3个值
px=img[100,100]
blue=img[100,100,0] # 加通道
同样的我们也可以退通过numpy的函数进行访问毕竟他是优化过的
图像分割、合并
>>> b,g,r = cv2.split(img) 切割
>>> img = cv2.merge((b,g,r)) 合并
图像加法
cv2.add()
饱和加法,不会溢出
>>> x = np.uint8([250])
>>> y = np.uint8([10])
>>> print cv2.add(x,y) # 250+10 = 260 => 255
[[255]]
>>> print x+y # 250+10 = 260 % 256 = 4
[4]
图像按权重相加
cv2.addWeighted()
第一参数 第一张图片
第二个参数 :第一张图片的权重
第三个参数:第二张图片
第四个参数:第二张图片的权重
第五个参数: r值
img1 = cv2.imread('ml.png')
img2 = cv2.imread('opencv_logo.jpg')
dst = cv2.addWeighted(img1,0.7,img2,0.3,0)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Load two images
img1 = cv2.imread('messi5.jpg')
img2 = cv2.imread('opencv_logo.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, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)
# 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()
网友评论