opencv官方文档源码及注释
# -*- coding: UTF-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
# I = cv2.imread('C:/Users/the_moon/Desktop/image.bmp',0)#读取图片
# cv2.namedWindow('I',cv2.WINDOW_NORMAL) #重新指定窗口形状
# cv2.imshow('image',I) #image 为显示框名字,I为显示的图片
# h=I.shape[0]
# w=I.shape[1]
# I1=np.zeros((w,h))
# cv2.imshow('1',I)
##创建蓝色线条
# # Create a black image
# img = np.zeros((512,512,3), np.uint8)
# # Draw a diagonal blue line with thickness of 5 px
# img = cv2.line(img,(0,0),(511,511),(255,0,0),5)#img为图像矩阵,(0,0),(511,511)范围,(255,0,0)指定蓝色,5为厚度
# cv2.imshow('img',img)
# #随轨迹创建调色板
# def nothing(x):
# pass
# # Create a black image, a window
img = cv2.imread('C:/Users/the_moon/Desktop/image.bmp')
plt.subplot(131),plt.imshow(img),plt.title('ORIGINAL1')
# cv2.namedWindow('image')
# # create trackbars for color change
# cv2.createTrackbar('R','image',0,255,nothing)
# cv2.createTrackbar('G','image',0,255,nothing)
# cv2.createTrackbar('B','image',0,255,nothing)
# # create switch for ON/OFF functionality
# switch = '0 : OFF \n1 : ON'
# cv2.createTrackbar(switch, 'image',0,1,nothing)
# while(1):
# cv2.imshow('image',img)
# k = cv2.waitKey(1) & 0xFF
# if k == 27:
# break
# # get current positions of four trackbars
# r = cv2.getTrackbarPos('R','image')
# g = cv2.getTrackbarPos('G','image')
# b = cv2.getTrackbarPos('B','image')
# s = cv2.getTrackbarPos(switch,'image')
# if s == 0:
# img[:] = 0
# else:
# img[:] = [b,g,r]
img = cv2.imread('C:/Users/the_moon/Desktop/image.bmp')
# cv2.imshow('image3',img)
img.itemset((10,10,2),100)
# cv2.namedWindow('image3',cv2.WINDOW_NORMAL)
x,y,z=img.shape #图片矩阵的形状
print(x,y,z,img.size,img.dtype)#img.size是图片像素总和,什么类型
b,g,r=cv2.split(img)#单独提出B,G,R三通道
img=cv2.merge((b,g,r))
#img[:,:,1]=255#提取出单通道,并赋值新的颜色,0,1,2代表蓝,绿,红
#填充图片边框
"""
cv2.copyMakeBorder()
src :input image
top, bottom, left, right :border width in number of pixels in corresponding directions
borderType :Flag defining what kind of border to be added. It can be following types:
cv2.BORDER_CONSTANT :Adds a constant colored border. The value should be given as next argument.
cv2.BORDER_REFLECT : Border will be mirror reflection of the border elements, like this : fedcba|abcdefgh|hgfedcb
cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT : Same as above, but with a slight change, like this : gfedcb|abcdefgh|gfedcba
cv2.BORDER_REPLICATE : Last element is replicated throughout, like this: aaaaaa|abcdefgh|hhhhhhh
cv2.BORDER_WRAP : Can’t explain, it will look like this : cdefgh|abcdefgh|abcdefg
value : Color of border if border type is cv2.BORDER_CONSTANT
"""
# replicate=cv2.copyMakeBorder(img,100,100,100,100,cv2.BORDER_REPLICATE)
# reflect=cv2.copyMakeBorder(img,100,100,100,100,cv2.BORDER_REFLECT)
# wrap=cv2.copyMakeBorder(img,100,100,100,100,cv2.BORDER_WRAP)
# constant=cv2.copyMakeBorder(img,100,100,100,100,cv2.BORDER_CONSTANT)
# reflect101 = cv2.copyMakeBorder(img,100,100,100,100,cv2.BORDER_REFLECT_101)
# plt.subplot(231),plt.imshow(img),plt.title('ORIGINAL')
# plt.subplot(232),plt.imshow(replicate),plt.title('REPLICATE')
# plt.subplot(233),plt.imshow(reflect),plt.title('REFLECT')
# plt.subplot(234),plt.imshow(reflect101),plt.title('REFLECT_101')
# plt.subplot(235),plt.imshow(wrap),plt.title('WRAP')
# plt.subplot(236),plt.imshow(constant),plt.title('CONSTANT')
# plt.show()
# #两张图片叠加
img2=cv2.imread('C:/Users/the_moon/Desktop/lena.BMP')
# dst = cv2.addWeighted(img,0.7,img,0.3,0)
#按位操作对图片叠加
# I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img.shape
roi = img[0:rows, 0:cols ]
# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) #用这个函数把图像从RGB转到HSV夜色空间,注意是BGR2HSV,因为在opencv中默认的颜色空间是BGR。
ret, mask = cv2.threshold(img2gray, 150, 255, cv2.THRESH_BINARY)#threshold:固定阈值二值化,ret, dst = cv2.threshold(src, thresh, maxval,type)src: 输入图,只能输入单通道图像,通常来说为灰度图dst: 输出图thresh: 阈值maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值type:二值化操作的类型,包含以下5种类型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV
"""bitwise_and是对二进制数据进行“与”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“与”操作,1&1=1,1&0=0,0&1=0,0&0=0
bitwise_or是对二进制数据进行“或”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“或”操作,1|1=1,1|0=0,0|1=0,0|0=0
bitwise_xor是对二进制数据进行“异或”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“异或”操作,1^1=0,1^0=1,0^1=1,0^0=0
bitwise_not是对二进制数据进行“非”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“非”操作,~1=0,~0=1
"""
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)
img[0:rows, 0:cols ] = dst
plt.subplot(132),plt.imshow(img2),plt.title('ORIGINAL2')
plt.subplot(133),plt.imshow(dst),plt.title('Image Blending')
plt.show()
# cv2.imshow('image3_after',dst)
# cv2.namedWindow('image3_after',cv2.WINDOW_NORMAL)
#
# #结束条件
# k=cv2.waitKey(0)#等待时间,ms
# if k ==27: #当ESC按下时退出
# print('退出')
# cv2.destroyAllWindows()
# elif k==ord('s'):#如果按下键盘S则保存并退出
# cv2.imwrite('image2.bmp', I) # 储存图片,第一个参数为图片名字和格式,第二个为要存储图片的对象
# print('已保存图片')
# cv2.destroyAllWindows()#任意键盘键关闭所有窗口
#