代码如下:
#encoding:utf-8
#
#图像运算
#
import cv2
import numpy as np
image = cv2.imread("H:\\img\\lena.jpg")
cv2.imshow("Original",image)
cv2.waitKey(0)
#图像image各像素加100
M = np.ones(image.shape,dtype="uint8")*100#与image大小一样的全100矩阵
added = cv2.add(image,M)#将图像image与M相加
cv2.imshow("Added",added)
cv2.waitKey(0)
代码如下:
#encoding:utf-8
#
#图像运算
#
import cv2
import numpy as np
image = cv2.imread("H:\\img\\lena.jpg")
cv2.imshow("Original",image)
cv2.waitKey(0)
#图像image各像素减去50
M = np.ones(image.shape,dtype="uint8")*50#与image大小一样的全50矩阵
subtracted = cv2.subtract(image,M)#将图像image与M相减
cv2.imshow("Subtracted", subtracted)
cv2.waitKey(0)
代码如下:
#encoding:utf-8
#
#图像的逻辑运算
#
import numpy as np
import cv2
#画矩形
Rectangle = np.zeros((300,300),dtype="uint8")
cv2.rectangle(Rectangle,(25,25),(275,275),255,-1)
cv2.imshow("Rectangle",Rectangle)
cv2.waitKey(0)
#画圆形
Circle = np.zeros((300,300),dtype="uint8")
cv2.circle(Circle,(150,150),150,255,-1)
cv2.imshow("Circle",![这里写图片描述](http://img.blog.csdn.net/20150729200613202)Circle)
cv2.waitKey(0)
#图像的交
bitwiseAnd = cv2.bitwise_and(Rectangle,Circle)
cv2.imshow("AND",bitwiseAnd)
cv2.waitKey(0)
代码如下:
#encoding:utf-8
#
#图像的逻辑运算
#
import numpy as np
import cv2
#画矩形
Rectangle = np.zeros((300,300),dtype="uint8")
cv2.rectangle(Rectangle,(25,25),(275,275),255,-1)
cv2.imshow("Rectangle",Rectangle)
cv2.waitKey(0)
#画圆形
Circle = np.zeros((300,300),dtype="uint8")
cv2.circle(Circle,(150,150),150,255,-1)
cv2.imshow("Circle",Circle)
cv2.waitKey(0)
#图像的或
bitwiseOr = cv2.bitwise_or(Rectangle,Circle)
cv2.imshow("OR",bitwiseOr)
cv2.waitKey(0)
代码如下:
#encoding:utf-8
#
#图像的逻辑运算
#
import numpy as np
import cv2
#画矩形
Rectangle = np.zeros((300,300),dtype="uint8")
cv2.rectangle(Rectangle,(25,25),(275,275),255,-1)
cv2.imshow("Rectangle",Rectangle)
cv2.waitKey(0)
#画圆形
Circle = np.zeros((300,300),dtype="uint8")
cv2.circle(Circle,(150,150),150,255,-1)
cv2.imshow("Circle",Circle)
cv2.waitKey(0)
#图像的异或
bitwiseXor = cv2.bitwise_xor(Rectangle,Circle)
cv2.imshow("XOR",bitwiseXor)
cv2.waitKey(0)
代码如下:
#encoding:utf-8
#
#图像的逻辑运算
#
import numpy as np
import cv2
#画圆形
Circle = np.zeros((300,300),dtype="uint8")
cv2.circle(Circle,(150,150),150,255,-1)
cv2.imshow("Circle",Circle)
cv2.waitKey(0)
#圆形的非运算
bitwiseNot = cv2.bitwise_not(Circle)
cv2.imshow("NOT",bitwiseNot)
cv2.waitKey(0)