跟着b站的教程学习链接:https://www.bilibili.com/vIDeo/BV1RQ4y1R7M6?p=6
内容基本按照教程走,进行了一些简单的注释和总结。
无原理解释,默认学过
1.读取和处理
图片读取
src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_GRAYSCALE) # opencv读取文件的路径不能有中文名# src =img[0:50, 0:200] # 截取图像中的一部分b, g, r = cv.split(src)# cv.IMREAD_GRAYSCALE 转灰度图像cv.nameDWindow("cat and girl", cv.WINDOW_autoSIZE)cv.imshow("cat and girl", src)cv.waitKey(0) # 等待事件触发,参数0表示指令发出后立即执行。本题是按任意键执行destroyAllwindowscv.destroyAllwindows() # 释放窗口cv.imwrite()
视频读取
vIDeo_test01 = cv.VIDeoCapture("D:/ALL_LEARN/vIDeo/test01.mp4")# 检查是否能打开if vIDeo_test01.isOpened(): open, frame = vIDeo_test01.read()else: open = Falsewhile open: ret, frame = vIDeo_test01.read() if frame is None: break # if ret == True: if ret: gray = cv.cvtcolor(frame, cv.color_BGR2GRAY) # 转灰度图 cv.imshow("result", gray) if cv.waitKey(10) & 0xFF == 27: # 27表示键盘中退出键Esc, 10表示功能运行速度 breakvIDeo_test01.release()cv.destroyAllwindows()
2.ROI区域(兴趣区域)
src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_color) # b, g, r = cv.split(src)# src = cv.merge(b, g, r) # 转回rgb图cur_src = src.copy()cur_src[:, :, 0] = 0 # 前两个":"表示取所有像素点cur_src[:, :, 1] = 0 # BGR 分表用012代表,各通道置0cv.imshow("R", cur_src)cv.waitKey(0)cv.destroyAllwindows()cv.imwrite()
3.边界填充
src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_color)replicate = cv.copyMakeborder(src, 50, 50, 50, 50, borderType=cv.border_CONSTANT)cv.imshow("R", replicate)cv.waitKey(0)cv.destroyAllwindows()cv.imwrite()# border_REPliCATE:复制边缘像素# border_CONSTANT常量法,常数值填充,value=0,# border_REFLECT:反射法,fedcba|abcdef|fedcba# border_REFLECT_101:反射法:fedcba|bcdef|edcba
4.数值计算
# img_dog = cv.imread("dog.jpg")# img_cat = cv.imread("cat.jpg")# img_cat1 = img_cat + 10 # 所有值+10# img_cat[:5, :, 0] # 打印前五行# cv.add(img_cat, img_dog) # 像素点相加,大于255置255# cv.resize(img_cat, (0, 0), fx=2, fy=1) # 重新设置大小# cv.adDWeighted(img_cat, 0.2, img_dog, 0.6) # 图像融合ctrl+/取消注释
5.图像阈值
src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_GRAYSCALE)ret, dst = cv.threshold(src, 127, 255, type=cv.THRESH_TOZERO)cv.imshow("R", dst)cv.waitKey(0)cv.destroyAllwindows()# src:输入图,单通道,灰度图# dst:输出图、thresh:阈值,maxval:像素值超过阈值后所赋予的值# type:二值化类型,加上INV就是其反转# type=cv.THRESH_BINARY超过阈值部分取maxval,否则取0# cv.THRESH_BINARY_INV超过阈值部分取0,否则取maxval# THRESH_TOZERO大于阈值不变,否则取0# THRESH_Trunc:大于设为阈值,否则不变# THRESH_MASK:不支持,未找到相关用法,尝试后全黑# TOZERO:大于阈值不变,小于为0
6.图像平滑处理
# src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_GRAYSCALE)src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_color)mean = cv.blur(src, (3, 3))cv.imshow("mean", mean)cv.waitKey(0)cv.destroyAllwindows()"""1.均值滤波 取滤波器均值cv.blur(src,(3,3))2.方框滤波加入归一化cv.BoxFilter(src,-1,(3,3),normalize=False)-1表示通道数一致False表示不归一化,大于255取2553.高斯滤波构造权重矩阵cv.GaussianBlur(src,(3,3),1)4.中值滤波找到均值后作为当前值medianBlur展示所有res =np.hstack((,,,))print(res)imshow(res)"""
7.形态学
src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_GRAYSCALE)# src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_color)kernel = np.ones((5, 5), np.uint8)erosion = cv.erode(src, kernel, iterations=1)cv.imshow("erosion", erosion)cv.waitKey(0)cv.destroyAllwindows()"""1.腐蚀kernel =no.ones((5,5),np.uint8) # 核大小决定每次腐蚀面积的大小erosion=cv.erode(src,kernel,iterations=2)#iterations迭代次数2.膨胀dilate=cv.dilate(src,kernel,iterations=2)#iterations迭代次数3.开闭运算开运算:先腐蚀后膨胀闭运算:先膨胀后腐蚀open = cv.morphologyEx(src,cv.MORPH_OPEN,kernel)close = cv.morphologyEx(src,cv.MORPH_CLOSE,kernel)"""
8.梯度计算
src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_GRAYSCALE)# src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_color)kernel = np.ones((5, 5), np.uint8)sobel_x = cv.sobel(src, cv.CV_64F, 1, 0, ksize=3)sobel_x = cv.convertScaleAbs(sobel_x) # 数值转换cv.imshow("sobel_x", sobel_x)cv.waitKey(0)cv.destroyAllwindows()"""1.sobel算子:[(-1 0 1)(-2 0 2)(-1 0 1)]cv.sobel(src,ddepth,dx,dy,ksize)sobel_x=cv.sobel(src,cv.CV_64F,1,0,ksize=3)#计算dx,ddepth一般为-1,CV_64F可以表示负数的形式,然后取绝对值sobel_x=cv.convertScaleAbs(sobel_x)#数值转换#白到黑为正,黑到白为负,负数会被截断为0,需要取正2.梯度计算方法先计算dx,dy,再进行叠加(分开计算结果更好,为什么?)sobelxy=cv.adDWeighted(sobelx,0.5,sobely,0.5)3.Scharr算子[(-3 0 3)(-10 0 10)(-3 0 3)]对于sobel,对结果更敏感,头发线条的梯度信息也会捕捉到4.laplacian[(0 1 0)(1 -4 1)(0 1 0)]但同时对噪声点也敏感"""
9.边缘检测
src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_GRAYSCALE)# src = cv.imread("D:/ALL_LEARN/picture/cat.jpg", cv.IMREAD_color)src = src[200:500, 200:500]v1 = cv.Canny(src, 80, 150) # 80,150双阈值检测的阈值minVal,越小获得的边界越多v2 = cv.Canny(src, 50, 100)res = np.hstack((v1, v2)) # vstack# takes 1 positional argument but 2 were given (())cv.imshow("res", res)cv.waitKey(0)cv.destroyAllwindows()"""Canny边缘检测1.使用高斯滤波,去噪2.计算每个像素点梯度和方向,sobel算子。3.应用非极大值(Non-Maximum Suppression)抑制,消除杂散响应极大值抑制:同一目标获得多个结果,取概率最大的结果 1.线性插值法:亚像素点梯度的值为临近两点的均值(有权重) 2.把一个像素的梯度离散为八个方向进行计算4.双阈值检测边缘:进一步过滤,获得真实的边界5.抑制鼓励弱边缘"""
总结 以上是内存溢出为你收集整理的python+opencv学习day01全部内容,希望文章能够帮你解决python+opencv学习day01所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)