【OpenCV入门--python--腐蚀与膨胀】

理论知识:

1.函数cv2.morphologyEx()用于开运算、闭运算。
先腐蚀再膨胀(开运算):除噪声
先膨胀再腐蚀(闭运算):填充前景物体的小洞
2.腐蚀函数:cv2.erode()
3.膨胀函数:cv2.dilate()

代码调试:

报错1:

usage: ipykernel_launcher.py [-h] [--input INPUT]
ipykernel_launcher.py: error: unrecognized arguments: -f C:\Users\NYX\AppData\Roaming\jupyter\runtime\kernel-df8f3828-4901-4ff7-98a1-83a4f667b94e.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

【OpenCV入门--python--腐蚀与膨胀】_第1张图片
解决1:
当在jupyter下使用parser.parse_args()出错
则改换为parser.parse_known_args()[0]

报错2:
error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\utils\samples.cpp:64: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: LinuxLogo.jpg in function 'cv::samples::findFile'

解决2:图像所在位置有中文路,或者没找到图像。将源码的py文件和图像放到同一个文件夹中,在文件夹中打开命令控制符,
【OpenCV入门--python--腐蚀与膨胀】_第2张图片

原图:
【OpenCV入门--python--腐蚀与膨胀】_第3张图片

【OpenCV入门--python--腐蚀与膨胀】_第4张图片

运行结果:

膨胀图像:

【OpenCV入门--python--腐蚀与膨胀】_第5张图片

【OpenCV入门--python--腐蚀与膨胀】_第6张图片
腐蚀图像:
【OpenCV入门--python--腐蚀与膨胀】_第7张图片

【OpenCV入门--python--腐蚀与膨胀】_第8张图片

源代码:

#1.引入包
from __future__ import print_function
import cv2 as cv
import numpy as np
import argparse
#2.初始化
src = None
erosion_size = 0
max_elem = 2
max_kernel_size = 21
title_trackbar_element_shape = 'Element:\n 0: Rect \n 1: Cross \n 2: Ellipse'
title_trackbar_kernel_size = 'Kernel size:\n 2n +1'
title_erosion_window = 'Erosion Demo'
title_dilation_window = 'Dilation Demo'
#3.主函数
def main(image):
    global src
    src = cv.imread(cv.samples.findFile(image))#读取图像
    if src is None:#若找不到图像路径,则给出提示
        print('Could not open or find the image: ', image)
        exit(0)
    cv.namedWindow(title_erosion_window)#调用一系列函数
    cv.createTrackbar(title_trackbar_element_shape, title_erosion_window, 0, max_elem, erosion)
    cv.createTrackbar(title_trackbar_kernel_size, title_erosion_window, 0, max_kernel_size, erosion)
    cv.namedWindow(title_dilation_window)
    cv.createTrackbar(title_trackbar_element_shape, title_dilation_window, 0, max_elem, dilatation)
    cv.createTrackbar(title_trackbar_kernel_size, title_dilation_window, 0, max_kernel_size, dilatation)
    erosion(0)#调用腐蚀函数,传入参数0即规定内核形状为矩形
    dilatation(0)#调用膨胀函数
    cv.waitKey()#cv2.waitkey(delay);若设置delay为0,则表示必须点击窗口界面的×才能关闭程序。
# optional mapping of values with morphological shapes
def morph_shape(val):
    if val == 0:
        return cv.MORPH_RECT
    elif val == 1:
        return cv.MORPH_CROSS
    elif val == 2:
        return cv.MORPH_ELLIPSE
def erosion(val):#腐蚀
    erosion_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_erosion_window)
    erosion_shape = morph_shape(cv.getTrackbarPos(title_trackbar_element_shape, title_erosion_window))
    
    element = cv.getStructuringElement(erosion_shape, (2 * erosion_size + 1, 2 * erosion_size + 1),
                                       (erosion_size, erosion_size))
    
    erosion_dst = cv.erode(src, element)#调用腐蚀操作
    cv.imshow(title_erosion_window, erosion_dst)#显示图像
def dilatation(val):#扩张
    dilatation_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_dilation_window)
    dilation_shape = morph_shape(cv.getTrackbarPos(title_trackbar_element_shape, title_dilation_window))
    element = cv.getStructuringElement(dilation_shape, (2 * dilatation_size + 1, 2 * dilatation_size + 1),
                                       (dilatation_size, dilatation_size))
    dilatation_dst = cv.dilate(src, element)
    cv.imshow(title_dilation_window, dilatation_dst)
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Code for Eroding and Dilating tutorial.')
    parser.add_argument('--input', help='Path to input image.', default='LinuxLogo.jpg')
    args = parser.parse_known_args()[0]
    #args = parser.parse_args()
    
    main(args.input)

你可能感兴趣的:(OpenCV,opencv,python,计算机视觉)