Opencv安装包:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv
OpenCV3.3以上版本支持tensorflow
https://blog.csdn.net/qizongshuai/article/details/77531115?locationNum=8&fps=1
pip install XX.whl
cv2.imread(path)
path种不能有中文。
解决方法:
image=cv2.imdecode(np.fromfile('E:\F\图片\picture/10.jpg',dtype=np.uint8),-1)
image类型是ndarray
此时的image是一个三维数组。数组大小为:
img.shape=(height,width,channel)
当然可以反过来访问:
height=img.shape[0]
(h,w)=img.shape[:2] 意思是[0] [1]合并输出
…
如果想访问数组元素:
img[0,0,0]=(0,0)像素点R值
img[0,0]=(0,0)的RGB值 r,g,b=img[0,0] 或 (r,g,b)=img[0,0]
img[0]=第0行
数组部分截取:
img1=img[0:30,0:40]
数组部分赋值:
img[0:30,0:40]=(0,255,0)
imwrite
namedWindow
imshow
cv2.imshow(‘winsows name’,image)
第一个参数不能少,image类型是ndarray数组就行(2维或者3维)
cv2.destroyAllWindows()
cv2.waitKey(0)
cvtColor
imageOut=cv2.cvtcolot(image,cv2.COLOR_RGB2GRAY)
COLOR_BGR2GRAY cv2.COLOR_BGR2HSV cv2.COLOR_BGR2LAB
变灰度图实际上就是少一个维度,但是不是简单的从RGB中任取一个作为灰度图,是有计算公式的。
resize
resized = cv2.resize(image, (30,30), interpolation = cv2.INTER_AREA)#缩小到(30,30)方形图
dim = (150, int(150 * image.shape[0] / image.shape[1] ))
resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)#同比例缩小more easy:
resized = imutils.resize(image, width = 100)
line rectangle circle ellipse
cv2.line(img,(0,0),(50,50),(0,255,0),30) 30是线宽,如果是-1,意思是画实心图
cv2.rectangle(img,(40,40),(90,90),(255,0,0),20)
cv2.circle(img,(100,100),40,10)带()的参数类型为tuple。
warpAffine
平移:
M = np.float32([[1, 0, -50], [0, 1, -90]]) #下、右 平移
M = np.float32([[1, 0, -50], [0, 1, -90]]) # 上、左平移
shifted = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
平移还可以用imutils,更方便
shifted = imutils.translate(image, -10, 100)#左、下平移
warpPerspective
getRotationMatrix2D
逆时针旋转
M = cv2.getRotationMatrix2D((image.shape[1]//2,image.shape[0]//2), -90, 1.0)#顺时针90,-1为反转
rotated = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))更简单:
rotated = imutils.rotate(image, 180)#简单
flip
flipped = cv2.flip(image, 1)#反转0:垂直,-1:水平
getAffineTransform
getPerspectiveTransform
add subtract
全像素批量处理:
cv2.add(np.uint8([200]), np.uint8([100])) = 255 # 因为是unsigned int8,最大256
cv2.subtract(np.uint8([50]), np.uint8([100])) = 0
np.uint8([200]) + np.uint8([100]) = 44
np.uint8([50]) - np.uint8([100]) = 206.
M = np.ones(image.shape, dtype="uint8") * 100 # 此时M可以说是一张图,也可以说是array
added = cv2.add(image, M)
bitwise_add bitwise_or
按位and or xor not:
bitwiseAnd = cv2.bitwise_and(rectangle, circle)
bitwiseOr = cv2.bitwise_or(rectangle, circle)
bitwiseXor = cv2.bitwise_xor(rectangle, circle)
bitwiseNot = cv2.bitwise_not(circle)
split merge
(B, G, R) = cv2.split(image) # 通道拆分,变成3个2维数组
merged = cv2.merge([B, G, R])chans=cv2.split(image) #chans is list
.
看单通道彩图:
zeros = np.zeros(image.shape[:2], dtype = "uint8")
cv2.imshow("Red", cv2.merge([zeros, zeros, R]))
cv2.imshow("Green", cv2.merge([zeros, G, zeros]))
cv2.imshow("Blue", cv2.merge([B, zeros, zeros]))
threshold
adaptiveThreshold
filter2D
blur 均值滤波
GaussianBlur
medianBlur
bilateralFilter
erode
dilate
morphologyEx 开闭运算、梯度、礼帽、黑帽
Sobel
Laplacian
Canny
pyrDown
pyrUp
calcHist
灰度直方图
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([image], [0], None, [256], [0, 256])# [image] :将ndarray转为list2D
hist = cv2.calcHist([chans[1], chans[0]], [0, 1], None, [32, 32], [0, 256, 0, 256])
hist = cv2.calcHist([chans[1], chans[2]], [0, 1], None, [32, 32], [0, 256, 0, 256])
ist = cv2.calcHist([chans[0], chans[2]], [0, 1], None, [32, 32], [0, 256, 0, 256])
3D
hist = cv2.calcHist([image], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
equalizeHist 直方图均衡
# 直方图均衡化
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
eq = cv2.equalizeHist(image)
createCLAHE 直方图局部均衡
imutils
imutils提供一系列便捷功能进行基本的图像处理功能,如平移,旋转,缩放,骨架,matplotlib图像显示,排序的轮廓,边缘检测,让你更容易使用OpenCV和Python。
imutils.translate(im,25,-75)
imutils.rotate(im,angle=angle)
imutils.resize(im,width=width)
imutils.skeletonize(gray,size=(3,3),structuring=cv2.MORPH_RECT) #灰度骨架提取
imutils.opencv2matplotlib(im) Opencv默认用BGR格式打开图片,motplotib用RGB格式打开图片
imutils.sort_contours(cnts, method="left-to-right"):