# import cv2 # import numpy as np # a = cv2.imread("D:\\img\\lenna.png",cv2.IMREAD_UNCHANGED ) # cv2.imshow("original",a) # b,g,r = cv2.split(a) # cv2.imshow("b",b) # cv2.imshow("g",g ) # cv2.imshow("r",r) # m = cv2.merge([r,g,b]) # cv2.imshow("merge",m) # cv2.waitKey() # cv2.destroyAllWindows() '''图像拆分与合并''' # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\lenna.png",cv2.IMREAD_UNCHANGED ) # cv2.imshow("original",a) # b,g,r = cv2.split(a) # rows,cols,chns = a.shape # b = np.zeros((rows,cols),a.dtype) # g = np.zeros((rows,cols),a.dtype) # r = cv2.split(a)[0] # m = cv2.merge([b,g,r]) # cv2.imshow("merge",m) # cv2.waitKey() # cv2.destroyAllWindows() # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\lena.bmp ",cv2.IMREAD_UNCHANGED ) # b = a # result1 = a+b # result2 = cv2.add(a,b) # cv2.imshow("orignial",a) # cv2.imshow("zhijie",result1) # cv2.imshow("OpenCV",result2) # cv2.waitKey() # cv2.destroyAllWindows() # '''图像融合''' # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\gaO.png ",cv2.IMREAD_UNCHANGED ) # b = cv2.imread("D:\\img\\hi.png ",cv2.IMREAD_UNCHANGED) # result = cv2.addWeighted(a,0.7,b,0.3,0) # cv2.imshow("ronghe",result) # cv2.waitKey() # cv2.destroyAllWindows() '''图像类型转换''' # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\lenna.png ",cv2.IMREAD_UNCHANGED ) # zhuan = cv2.cvtColor(a,cv2.COLOR_BGR2RGB) # b,g,r = cv2.split(zhuan) # cv2.imshow("b",b) # cv2.imshow("a",a) # cv2.imshow("g",g) # cv2.imshow("r",r) # cv2.imshow("zhuan",zhuan) # cv2.waitKey() # cv2.destroyAllWindows() # '''图像翻转''' # import cv2 # a = cv2.imread("D:\\img\\gao.png ",cv2.IMREAD_UNCHANGED ) # b = cv2.flip(a,0) # c = cv2.flip(a,1) # d = cv2.flip(a,-1) # cv2.imshow("a",a) # cv2.imshow("b",b) # cv2.imshow("c",c) # cv2.imshow("d",d) # cv2.waitKey() # cv2.destroyAllWindows() # '''图像阈值化处理''' # import cv2 # a = cv2.imread("D:\\img\\Lena.bmp",cv2.IMREAD_UNCHANGED ) # r,b = cv2.threshold(a,128,255,cv2.THRESH_BINARY) # m,c = cv2.threshold(a,127,255,cv2.THRESH_BINARY_INV) # n,d = cv2.threshold(a,127,255,cv2.THRESH_TRUNC) # p,e = cv2.threshold(a,127,255,cv2.THRESH_TOZERO) # o,f = cv2.threshold(a,127,255,cv2.THRESH_TOZERO_INV) # cv2.imshow("erjinzhi",b) # cv2.imshow("fanerjinzhi",c) # cv2.imshow("original",a) # cv2.imshow("jieduan",d) # cv2.imshow("yuzhi",e) # cv2.imshow("fanyuzhi",f) # cv2.waitKey() # cv2.destroyAllWindows() # '''均值滤波''' # import cv2 # a = cv2.imread("D:\\img\\gao.png ",cv2.IMREAD_UNCHANGED ) # result = cv2.blur(a,(5,5)) # result1 = cv2.blur(a,(100,100)) # cv2.imshow("original",a) # cv2.imshow("lvbo",result) # cv2.imshow("lvbo1",result1) # cv2.waitKey() # cv2.destroyAllWindows() # '''高斯滤波''' # import cv2 # a = cv2.imread("D:\\img\\gao.png ",cv2.IMREAD_UNCHANGED ) # result = cv2.GaussianBlur(a,(31,31),0) # cv2.imshow("original",a) # cv2.imshow("result",result) # cv2.waitKey() # cv2.destroyAllWindows() # '''中值滤波''' # import cv2 # a = cv2.imread("D:\\img\\gao.png ",cv2.IMREAD_UNCHANGED ) # result = cv2.medianBlur(a,13) # cv2.imshow("original",a) # cv2.imshow("result",result) # cv2.waitKey() # cv2.destroyAllWindows() # '''图像膨胀''' # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\dilation.bmp",cv2.IMREAD_UNCHANGED ) # kernel = np.ones((5,5),np.uint8 ) # result = cv2.dilate(a,kernel,iterations= 4) # cv2.imshow("original",a) # cv2.imshow("result",result) # cv2.waitKey() # cv2.destroyAllWindows() # '''图像腐蚀''' # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\erode.bmp",cv2.IMREAD_UNCHANGED ) # kernel = np.ones((5,5),np.uint8 ) # result = cv2.erode(a,kernel) # result2 = cv2.erode(a,kernel,iterations= 5) # cv2.imshow("original",a) # cv2.imshow("result",result ) # cv2.imshow("result2",result2 ) # cv2.imwrite("D:\\img\\erode2.bmp",result2 ) # cv2.waitKey() # cv2.destroyAllWindows() # '''开运算''' # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\lenaNoise.png",cv2.IMREAD_UNCHANGED ) # kernel = np.ones((5,5),np.uint8) # result = cv2.morphologyEx(a,cv2.MORPH_OPEN ,kernel) # cv2.imshow("original",a) # cv2.imshow("result",result) # cv2.waitKey() # cv2.destroyAllWindows() # '''闭运算''' # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\gao.png ",cv2.IMREAD_UNCHANGED ) # kernal = np.ones((5,5),np.uint8 ) # result = cv2.morphologyEx(a,cv2.MORPH_CLOSE ,kernal ) # cv2.imshow("original",a) # cv2.imshow("result",result) # cv2.waitKey() # cv2.destroyAllWindows() # '''梯度运算''' # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\erode2.bmp",cv2.IMREAD_UNCHANGED ) # kernal = np.ones((3,3),np.uint8 ) # tidu = cv2.morphologyEx(a,cv2.MORPH_GRADIENT ,kernal) # cv2.imshow("tidu",tidu) # cv2.imshow("o",a) # cv2.waitKey() # cv2.destroyAllWindows() # '''礼帽运算''' # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\erode.bmp ",cv2.IMREAD_UNCHANGED ) # kernal = np.ones((5,5),np.uint8 ) # result = cv2.morphologyEx(a,cv2.MORPH_TOPHAT ,kernal) # cv2.imshow("original",a) # cv2.imshow("limao",result) # cv2.waitKey() # cv2.destroyAllWindows() # '''黑帽运算''' # import cv2 # import numpy as np # a = cv2.imread("D:\\img\\erode.bmp ",cv2.IMREAD_UNCHANGED ) # kernal = np.ones((5,5),np.uint8 ) # result = cv2.morphologyEx(a,cv2.MORPH_BLACKHAT ,kernal ) # cv2.imshow("original",a) # cv2.imshow("result",result) # cv2.waitKey() # cv2.destroyAllWindows() # '''转bmp''' # import cv2 # a = cv2.imread("D:\\img\\gao.png",cv2.IMREAD_GRAYSCALE) # cv2.imshow("sobel",a) # cv2.imwrite("D:\\img\\gaogao.bmp",a) # cv2.waitKey() # cv2.destroyAllWindows() # '''Sobel算子''' # import cv2 # a = cv2.imread("D:\\img\\lena.bmp",cv2.IMREAD_UNCHANGED ) # # result = cv2.Sobel(a,-1,1,0) # # result1 = cv2.Sobel(a,cv2.CV_64F,1,1) # # dx = cv2.Sobel(a,cv2.CV_64F ,1,0) # dy = cv2.Sobel(a,cv2.CV_64F ,0,1) # result2 = cv2.convertScaleAbs(dx ) # result4 = cv2.convertScaleAbs(dy ) # result3 = cv2.addWeighted(result2 ,0.5,result4 ,0.5,0) # cv2.imshow("original",a) # # cv2.imshow("sobel",result ) # # cv2.imshow("sobel1",result1 ) # # cv2.imshow("sobel2",result2 ) # cv2.imshow("sobel3",result3 ) # cv2.waitKey() # cv2.destroyAllWindows() # '''scharr算子''' # import cv2 # a = cv2.imread("D:\\img\\gaogao.bmp",cv2.IMREAD_UNCHANGED ) # scharrx = cv2.Scharr(a,cv2.CV_64F ,1,0) # scharry = cv2.Scharr(a,cv2.CV_64F ,0,1) # resultx = cv2.convertScaleAbs(scharrx ) # resulty = cv2.convertScaleAbs(scharry ) # result = cv2.addWeighted(resultx,0.5,resulty ,0.5,0) # cv2.imshow("original",a) # cv2.imshow("result",result ) # cv2.waitKey() # cv2.destroyAllWindows() # '''laplacian算子''' # import cv2 # a = cv2.imread("D:\\img\\suanzi.bmp ",cv2.IMREAD_UNCHANGED ) # laplacian = cv2.Laplacian(a,cv2.CV_64F ) # laplacian = cv2.convertScaleAbs(laplacian ) # cv2.imshow("original",a) # cv2.imshow("laplacian",laplacian ) # cv2.waitKey() # cv2.destroyAllWindows()
# '''canny边缘检测''' # import cv2 # a = cv2.imread("D:\\img\\gaogao.bmp",cv2.IMREAD_UNCHANGED ) # canny = cv2.Canny(a,50,60) # cv2.imshow("original",a) # cv2.imshow("canny",canny ) # cv2.waitKey() # cv2.destroyAllWindows() # '''向下取样''' # import cv2 # a = cv2.imread("D:\\img\\gao.png",cv2.IMREAD_UNCHANGED ) # result = cv2.pyrDown(a) # result1 = cv2.pyrDown(result) # cv2.imshow("result",result) # cv2.imshow("result1",result1) # cv2.imshow("original",a) # cv2.waitKey() # cv2.destroyAllWindows() # '''向上取样''' # import cv2 # a = cv2.imread("D:\\img\\lena.bmp ",cv2.IMREAD_UNCHANGED ) # result = cv2.pyrUp(a) # result1 = cv2.pyrUp(result) # cv2.imshow("result",result) # cv2.imshow("result1",result1) # cv2.imshow("original",a) # cv2.waitKey() # cv2.destroyAllWindows() # '''向上取样和向下取样的可逆性研究''' # import cv2 # a = cv2.imread("D:\\img\\lena.bmp ",cv2.IMREAD_UNCHANGED ) # result = cv2.pyrUp(a) # result = cv2.pyrDown(result) # result1 = cv2.pyrDown(a) # result1 = cv2.pyrUp(result1) # cv2.imshow("result",result) # cv2.imshow("result1",result1) # cv2.imshow("original",a) # cv2.waitKey() # cv2.destroyAllWindows() # '''拉普拉斯金字塔''' # import cv2 # o = cv2.imread("D:\\img\\lena.bmp ",cv2.IMREAD_UNCHANGED ) # od = cv2.pyrDown(o) # odu = cv2.pyrUp(od) # laplacian = o - odu # cv2.imshow("first",laplacian ) # cv2.imshow("original",o ) # o1 = od # o2d = cv2.pyrDown(o1) # o2du = cv2.pyrUp(o2d) # laplacian2 = o1 - o2du # cv2.imshow("second",laplacian2 ) # cv2.waitKey() # cv2.destroyAllWindows() # '''图像轮廓''' # import cv2 # a = cv2.imread("D:\\img\\lena.bmp ") # gray = cv2.cvtColor(a,cv2.COLOR_BGR2GRAY ) # ret,binary = cv2.threshold(gray ,127,255,cv2.THRESH_BINARY ) # image , contours,hierarchy =cv2.findContours(binary ,cv2.RETR_EXTERNAL ,cv2.CHAIN_APPROX_NONE ) # co = a.copy() # result = cv2.drawContours(co,contours ,-1,(0,0,255),1) # cv2.imshow("original",a) # cv2.imshow("gray",gray) # cv2.imshow("result") # cv2.waitKey() # cv2.destroyAllWindows() # '''图像轮廓''' # import cv2 # o = cv2.imread("D:\\img\\gao.png ",cv2.IMREAD_UNCHANGED ) # gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY ) # ret,binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY ) # # image, contours,heirarchy = cv2.findContours(binary ,cv2.RETR_EXTERNAL ,cv2.CHAIN_APPROX_NONE ) # '''高版本中image参数不再需要''' # contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # co = o.copy() # result = cv2.drawContours(co,contours ,-1,(0,255,0),1) # cv2.imshow("original",o) # cv2.imshow("result",result) # cv2.waitKey() # cv2.destroyAllWindows() # '''绘制直方图''' # import cv2 # import matplotlib.pyplot as plt # a = cv2.imread("D:\\img\\gao.png ") # cv2.imshow("original",a) # plt.hist(a.ravel() ,256) # plt.show() # cv2.waitKey() # cv2.destroyAllWindows() # '''OpenCV绘制直方图''' # import cv2 # a = cv2.imread("D:\\img\\lena.bmp ") # hist =cv2.calcHist([a],[0],None,[256],[0,255],None) # print(hist ) # cv2.imshow("hist",hist) # cv2.waitKey() # cv2.destroyAllWindows() # '''OpenCV绘制直方图''' # import numpy as np # import matplotlib.pyplot as plt # a = cv2.imread("D:\\img\\gao.png ") # # x = np.arange(0,5,0.1) # # y = np.sin(x) # # plt.plot(x,y) # # y = [0.1,0.2,0.3,0.5,0.9,2,4,7,8,12,45] # # plt.plot(y) # hist1 = cv2.calcHist([a],[0],None ,[256],[0,255]) # hist2 = cv2.calcHist([a],[1],None ,[256],[0,255]) # hist3 = cv2.calcHist([a],[2],None ,[256],[0,255]) # plt.plot(hist1,color='b') # plt.plot(hist2,color='g') # plt.plot(hist3,color='r') # plt.show() # '''使用掩膜绘制直方图''' # import cv2 # import numpy as np # import matplotlib .pyplot as plt # a = cv2.imread("D:\\img\\lena.bmp ",cv2.IMREAD_GRAYSCALE ) # mask = np.zeros(a.shape,np.uint8 ) # mask[50:80,50:80] = 255 # histm = cv2.calcHist([a],[0],mask,[256],[0,255]) # histn = cv2.calcHist([a],[0],None ,[256],[0,255]) # plt.plot (histm) # plt.plot (histn) # cv2.imshow("origina",a) # cv2.imshow("origin",mask ) # plt .show() # cv2.waitKey() # cv2.destroyAllWindows() # '''图像掩膜处理''' # import cv2 # import numpy as np # import matplotlib .pyplot as plt # a = cv2.imread("D:\\img\\lena.bmp ",cv2.IMREAD_GRAYSCALE ) # mask = np.zeros(a.shape ,np.uint8 ) # mask[30:100,30:100] = 255 # mi = cv2.bitwise_and(a,mask) # cv2.imshow("original",a) # cv2.imshow("mask",mask ) # cv2.imshow("mi",mi ) # hisk = cv2.calcHist([a],[0],mask,[256],[0,255]) # plt.plot(hisk) # plt.show() # cv2.waitKey() # cv2.destroyAllWindows() '''直方图均衡化处理''' # import cv2 # # import numpy as np '''cv2和matplotlib库都依赖于numpy,建议导入''' # import matplotlib .pyplot as plt # img = cv2.imread("D:\\img\\gao.png ",cv2.IMREAD_GRAYSCALE ) # equ = cv2.equalizeHist(img) # plt.hist(img.ravel(),256 ) # plt.figure() # plt.hist (equ.ravel(),256) # plt.show() # cv2.imshow("original",img) # cv2.imshow("equ",equ) # cv2.waitKey() # cv2.destroyAllWindows() # '''plt.imshow函数''' # import cv2 # import matplotlib . pyplot as plt # img = cv2.imread("D:\\img\\lenna.png " ) # cvt = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY ) # plt.subplot (2,2,1) , plt.imshow (img),plt.axis('off') # plt.subplot (2,2,2) , plt.imshow (img,cmap = plt.cm.gray),plt.axis('off') # plt.subplot (2,2,3) , plt.imshow (cvt),plt.axis('off') # plt.subplot (2,2,4) , plt.imshow (cvt,cmap = plt.cm.gray),plt.axis('off') # plt.show() # cv2.waitKey() # cv2.destroyAllWindows() # '''imshow函数显示彩色图像''' # import cv2 # import matplotlib .pyplot as plt # import numpy as np # a = cv2.imread("D:\\img\\lenna.png ") # '''方法一直接对图片空间进行转化,BGR2RGB''' # cvt = cv2.cvtColor(a,cv2.COLOR_BGR2RGB ) # plt.subplot(1,2,1),plt.imshow(cvt),plt.axis ('off') # plt.subplot(1,2,2),plt.imshow(a),plt.axis ('off') # '''方法2将图片通道拆分后再进行RGB合并''' # b,g,r = cv2.split(a) # result = cv2.merge([r,g,b]) # plt.subplot (1,2,1),plt.imshow(a),plt.axis('off') # plt.subplot (1,2,2),plt.imshow(result),plt.axis('off') # plt.show() # cv2.waitKey() # cv2.destroyAllWindows() '''imshow函数显示灰度图像''' import cv2 import matplotlib .pyplot as plt import numpy as np # # '''方法一直接以灰度图像显示''' # # a = cv2.imread("D:\\img\\lena.bmp ",cv2.IMREAD_GRAYSCALE ) # # plt.subplot(1,2,2),plt.imshow(a,cmap = plt.cm.gray),plt.axis ('off') # '''方法二将BGR转灰度图像后显示''' # a = cv2.imread("D:\\img\\lena.bmp ") # cvt = cv2.cvtColor(a,cv2.COLOR_BGR2GRAY ) # plt.subplot (1,2,1),plt.imshow(cvt),plt.axis('off') # plt.subplot (1,2,2),plt.imshow(cvt,cmap= plt.cm.gray),plt.axis('off') # plt.show() # cv2.waitKey() # cv2.destroyAllWindows()