Improving Opencv10 More Morphology Transformations

https://docs.opencv.org/4.3.0/d3/dbe/tutorial_opening_closing_hats.html

目录

https://docs.opencv.org/4.3.0/d3/dbe/tutorial_opening_closing_hats.html

Goal

Theory

Opening

Closing


Goal

In this tutorial you will learn how to:

  • Use the OpenCV function cv::morphologyEx to apply Morphological Transformation such as:
    • Opening
    • Closing
    •  

Theory

Note

The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler.

In the previous tutorial we covered two basic Morphology operations:

  • Erosion
  • Dilation.

Based on these two we can effectuate more sophisticated transformations to our images. Here we discuss briefly 5 operations offered by OpenCV:

Opening

  • It is obtained by the erosion of an image followed by a dilation.

    dst=open(src,element)=dilate(erode(src,element))

  • Useful for removing small objects (it is assumed that the objects are bright on a dark foreground)
  • For instance, check out the example below. The image at the left is the original and the image at the right is the result after applying the opening transformation. We can observe that the small dots have disappeared.

Improving Opencv10 More Morphology Transformations

Closing

  • It is obtained by the dilation of an image followed by an erosion.

    dst=close(src,element)=erode(dilate(src,element))

  • Useful to remove small holes (dark regions).

Improving Opencv10 More Morphology Transformations

# _*_ coding: utf-8 _*_

from __future__ import print_function
import cv2 as cv
# import numpy as np
import argparse

morph_size = 0
max_operator = 4
max_elem = 2
max_kernel_size = 21
title_trackbar_operator_type = 'Operator:\n 0: Opening - 1: Closing  \n 2: Gradient - 3: Top Hat \n 4: Black Hat'
title_trackbar_element_type = 'Element:\n 0: Rect - 1: Cross - 2: Ellipse'
title_trackbar_kernel_size = 'Kernel size:\n 2n + 1'
title_window = 'Morphology Transformations Demo'
morph_op_dic = {0: cv.MORPH_OPEN, 1: cv.MORPH_CLOSE, 2: cv.MORPH_GRADIENT, 3: cv.MORPH_TOPHAT, 4: cv.MORPH_BLACKHAT}

def morphology_operations(val):
    morph_operator = cv.getTrackbarPos(title_trackbar_operator_type, title_window)
    morph_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_window)
    morph_elem = 0
    val_type = cv.getTrackbarPos(title_trackbar_element_type, title_window)
    if val_type == 0:
        morph_elem = cv.MORPH_RECT
    elif val_type == 1:
        morph_elem = cv.MORPH_CROSS
    elif val_type == 2:
        morph_elem = cv.MORPH_ELLIPSE
    element = cv.getStructuringElement(morph_elem, (2*morph_size + 1, 2*morph_size+1), (morph_size, morph_size))
    operation = morph_op_dic[morph_operator]
    dst = cv.morphologyEx(src, operation, element)
    cv.imshow(title_window, dst)

parser = argparse.ArgumentParser(description='Code for More Morphology Transformations tutorial.')
parser.add_argument('--input', help='Path to input image.', default='brid.jpg')
args = parser.parse_args()
src = cv.imread(cv.samples.findFile(args.input))

reWidth = int(src.shape[1]/4)
reHight = int(src.shape[0]/4)
src = cv.resize(src, (reWidth, reHight))


if src is None:
    print('Could not open or find the image: ', args.input)
    exit(0)

cv.namedWindow(title_window)
cv.createTrackbar(title_trackbar_operator_type, title_window , 0, max_operator, morphology_operations)
cv.createTrackbar(title_trackbar_element_type, title_window , 0, max_elem, morphology_operations)
cv.createTrackbar(title_trackbar_kernel_size, title_window , 0, max_kernel_size, morphology_operations)
morphology_operations(0)
cv.waitKey()

Improving Opencv10 More Morphology Transformations

 

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

Mat src, dst;
int morph_elem = 0;
int morph_size = 0;
int morph_operator = 0;
int const max_operator = 4;
int const max_elem = 2;
int const max_kernel_size = 21;
const char* window_name = "Morphology Transformations Demo";
void Morphology_Operations(int, void*);

int main(int argc, char** argv)
{
	//CommandLineParser parser(argc, argv, "{@input | baboon.jpg | input image}");
	//src = imread(samples::findFile(parser.get<String>("@input")), IMREAD_COLOR);
	src = imread("NWPUSZ.jpg", IMREAD_COLOR);
	resize(src, src, Size(src.cols / 4, src.rows / 4));

	if (src.empty())
	{
		std::cout << "Could not open or find the image!\n" << std::endl;
		std::cout << "Usage: " << argv[0] << " <Input image>" << std::endl;
		return EXIT_FAILURE;
	}
	namedWindow(window_name, WINDOW_AUTOSIZE); // Create window
	createTrackbar("Operator:\n 0: Opening - 1: Closing  \n 2: Gradient - 3: Top Hat \n 4: Black Hat", window_name, &morph_operator, max_operator, Morphology_Operations);
	createTrackbar("Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name,
		&morph_elem, max_elem,
		Morphology_Operations);
	createTrackbar("Kernel size:\n 2n +1", window_name,
		&morph_size, max_kernel_size,
		Morphology_Operations);
	Morphology_Operations(0, 0);
	waitKey(0);
	return 0;
}
void Morphology_Operations(int, void*)
{
	// Since MORPH_X : 2,3,4,5 and 6
	int operation = morph_operator + 2;
	Mat element = getStructuringElement(morph_elem, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));
	morphologyEx(src, dst, operation, element);
	imshow(window_name, dst);
}

Improving Opencv10 More Morphology Transformations

 

 

 

上一篇:Python OpenCV 形态学处理


下一篇:图像处理之形态学操作