1.图像模糊处理
import?cv2?as?cv; def?blur_demo(): ????src=cv.imread("F:/images/zwj.jpg") ????cv.imshow("test",src) ????dst=cv.blur(src,?(15,15)) ????cv.imshow("blur",dst); ???? if?__name__=="__main__": ????blur_demo() ????cv.waitKey(0); ????cv.destroyWindow()
2.均值和高斯模糊
#?-*-?coding:?utf-8?-*- """ Created?on?Sat?Oct?24?19:12:29?2020 @author:?zwj @email:2377389590@qq测试数据 """ import?cv2?as?cv import?numpy?as?np def?blur_demo(): ????src=cv.imread("F:/images/zwj.jpg") ????cv.imshow("input",src) ????dst=cv.blur(src,(15,15)) ????cv.imshow("blur",dst); def?gaussian_blur_demo(): ????src=cv.imread("F:/images/zwj.jpg") ????cv.imshow("input",?src) ????dst=cv.GaussianBlur(src,?(5,5),?0) ????cv.imshow("gaussian_blur",?dst) if?__name__=="__main__": ????blur_demo() ????gaussian_blur_demo() ????cv.waitKey(0) ????cv.destroyAllWindows()
3、统计滤波(最大、最小、中值滤波)
#?-*-?coding:?utf-8?-*- """ Created?on?Sun?Oct?25?00:09:04?2020 @author:?猪猪侠 @email:2377389590@qq测试数据 """ import?cv2?as?cv import?tensorflow?as?tf import?numpy?as?np def?statistics_filters(): ????src=cv.imread("F:/images/jy.png") ????cv.imshow("input",src) ???? ????kernel=np.ones((3,3),np.uint8) ????dst=cv.erode(src,kernel) ????cv.imshow("minmum",dst) ???? ????dst2=cv.dilate(src,?kernel) ????cv.imshow("maxmum",?dst2) ???? ????dst1=cv.medianBlur(src,?5) ????cv.imshow("medi",dst1) if?__name__=="__main__": ????statistics_filters() ????cv.waitKey(0) ????cv.destroyAllWindows()
4、椒盐噪声and?高斯噪声
#?-*-?coding:?utf-8?-*- """ Created?on?Sun?Oct?25?00:09:04?2020 @author:?猪猪侠 @email:2377389590@qq测试数据 """ import?cv2?as?cv import?numpy?as?np def?add_noise(): ????src=cv.imread("F:/images/zwj.jpg") ????cv.imshow("input",src) ????copy=np.copy(src) ???? ????nums=50000 ????h,w=src.shape[:2] ????rows=np.random.randint(0,h,(nums),dtype=np.int) ????cols=np.random.randint(0,w,(nums),dtype=np.int) ????for?i?in?range(nums): ????????if?i%2==1: ????????????src[rows[i],cols[i]]=(255,255,255) ????????else: ????????????src[rows[i],cols[i]]=(0,0,0) ????cv.imshow("salt?ans?papper?image",?src) ????gnoise=np.zeros(src.shape,src.dtype) ????m=(15,15,15) ????s=(30,30,30) ????cv.randn(gnoise,m,?s) ????cv.imshow("gnoise",?gnoise) ????dst=cv.add(copy,gnoise) ????cv.imshow("gaussian?noise",?dst) if?__name__=="__main__": ????#blur_demo() ????#gaussian_blur_demo() ????#?statistics_filters() ????add_noise() ????cv.waitKey(0) ????cv.destroyAllWindows()
5、素描图像
#?-*-?coding:?utf-8?-*- """ Created?on?Mon?Oct?26?00:33:30?2020 @author:?猪猪侠 @email:2377389590@qq测试数据 """ import?cv2?as?cv import?numpy?as?np src=cv.imread("F:/images/zwj.jpg") cv.imshow("input",src) gray?=?cv.cvtColor(src,?cv.COLOR_BGR2GRAY) cv.imshow("gray",?gray) ksize=15 sigma=10 inv?=?255?-?gray blur?=?cv.GaussianBlur(inv,?ksize=(ksize,?ksize),?sigmaX=sigma,?sigmaY=sigma) res?=?cv.divide(gray,?255?-?blur,?scale=255) cv.imshow("sumiao",?res) mask=np.ones_like(src,np.uint8) #?mask[:,mask.shape[1]//2:,:]=0 mask[:,:mask.shape[1]//2:,:]=0 #mask[mask.shape[1]//2:,:,:]=0 #mask[:mask.shape[1]//2,:,:]=0 res?=?np.expand_dims(res,axis?=?2) img=res*mask+src*(1-mask) cv.imshow("mask",img) cv.waitKey(0) cv.destroyAllWindows()
6、绘制素描视频
import?cv2?as?cv import?numpy?as?np cap?=?cv.VideoCapture('D:/Img/蔡徐坤.mp4') while?(cap.isOpened()): ????ret,?frame?=?cap.read() ????gray?=?cv.cvtColor(frame,?cv.COLOR_BGR2GRAY) ????cv.imshow("gray",?gray) ????ksize?=?15 ????sigma?=?10 ????inv?=?255?-?gray ????blur?=?cv.GaussianBlur(inv,?ksize=(ksize,?ksize),?sigmaX=sigma,?sigmaY=sigma) ????res?=?cv.divide(gray,?255?-?blur,?scale=255) ????cv.imshow("sumiao",?res) ????mask?=?np.ones_like(frame,?np.uint8) ????h,?w?=?frame.shape[:2] ????mask[0:h//2,w//2:,:]=0 ????mask[h//2:,0:w//2,:]=0 ????#?mask[:,w//2:,:]=0 ????#?mask[:,?:w?//?2:,?:]?=?0 ????#mask[h//2:,:,:]=0 ????#?mask[:h//2,:,:]=0 ????res?=?np.expand_dims(res,?axis=2) ????img?=?res?*?mask?+?frame?*?(1?-?mask) ????cv.imshow("mask",?img) ????if?cv.waitKey(25)?&?0xFF?==?ord('q'): ????????break cap.release() cv.destroyAllWindows()
7、模版匹配
import?cv2?as?cv import??numpy?as?np def?match_template(): ????src=cv.imread("F:/images/zwj.jpg") ????tpl=cv.imread("F:/images/zwj2.jpg") ????cv.imshow("input",src) ????cv.imshow("tpl",tpl) ????th,tw=tpl.shape[:2] ????result=cv.matchTemplate(src,tpl,cv.TM_CCORR_NORMED) ????cv.imshow("result",result) ????minv,maxv,min_loc,max_loc=cv.minMaxLoc(result) ????cv.rectangle(src,max_loc,(max_loc[0]+tw,max_loc[1]+th),(0,0,255),2,8,0) ????cv.imshow("dst",src) if?__name__=="__main__": ????match_template() ????cv.waitKey(0) ????cv.destroyWindow();
8、Python调用摄像头
import?cv2?as?cv cap=cv.VideoCapture(0) while(True): ????ret,frame=cap.read() ????gray=cv.cvtColor(frame,cv.COLOR_RGB2BGR) ????cv.imshow("gray",gray) ????if?cv.waitKey(1)&0xFF==ord('1'): ????????break cap.release() cv.destroyWindow()
9、鼠标监听:
import?cv2?as?cv import?numpy?as?np def?my_mouse_callback(event,x,y,flag,params): ????if?event==cv.EVENT_LBUTTONDOWN: ????????b=np.random.randint(0,256) ????????g=np.random.randint(0,256) ????????r=np.random.randint(0,256) ????????cv.circle(params,(x,y),50,(b,g,r),2,cv.LINE_8,0) ????if?event==cv.EVENT_LBUTTONDOWN: ????????x1?=?np.random.rand()?*?600 ????????y1?=?np.random.rand()?*?600 ????????x2?=?np.random.rand()?*?600 ????????y2?=?np.random.rand()?*?600 ????????b?=?np.random.randint(0,?256) ????????g?=?np.random.randint(0,?256) ????????r?=?np.random.randint(0,?256) ????????cv.line(params,?(np.int(x1),?np.int(y1)),?(np.int(x2),?np.int(y2)),?(b,?g,?r),?4,?cv.LINE_8,?0) def?mouse_demo(): ????src=np.zeros((512,512,3),dtype=np.uint8) ????cv.namedWindow("mouse_demo",cv.WINDOW_AUTOSIZE) ????cv.setMouseCallback("mouse_demo",my_mouse_callback,src) ????while(True): ????????cv.imshow("mouse_demo",src) ????????c=cv.waitKey(20) ????????if?c==27: ????????????break ????????#?cv.imshow("src",src) if?__name__=="__main__": ????mouse_demo() ????cv.waitKey(0) ????cv.destroyAllWindows()
?
10、滑动块:
import?cv2?as?cv import?numpy?as?np def?do_nothing(pl): ????pass def?track_bar_demo(): ????src=np.zeros((512,512,3),dtype=np.uint8) ????cv.namedWindow("tb_demo",cv.WINDOW_AUTOSIZE) ????cv.createTrackbar("B","tb_demo",0,255,do_nothing) ????cv.createTrackbar("G","tb_demo",0,255,do_nothing) ????cv.createTrackbar("R","tb_demo",0,255,do_nothing) ????while(True): ????????b=cv.getTrackbarPos("B","tb_demo") ????????g?=?cv.getTrackbarPos("G",?"tb_demo") ????????r?=?cv.getTrackbarPos("R",?"tb_demo") ????????src[:]=[b,g,r] ????????cv.imshow("tb_demo",src) ????????c=cv.waitKey(15) ????????if?c==27: ????????????break if?__name__=="__main__": ????track_bar_demo() ????cv.waitKey(0) ????cv.destroyAllWindows()
?
10、绘制直方图
import?cv2?as?cv from?matplotlib?import??pyplot?as?plt img=cv.imread("D:/images/myj.jpg") cv.imshow('input',img) color=('b','g','r') for?i,col?in?enumerate(color): ????histr=cv.calcHist([img],[i],None,[256],[0,256]) ????plt.plot(histr,color=col) plt.show() cv.waitKey(0) cv.destroyAllWindows()
查看更多关于openCV学习笔记(Python版本)的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did125840