Opencv3 C++ VS2017 study notes 08 morphology operation MORPHOLOGY

  • Summary of learning methods:
    • Preliminary understanding of theoretical knowledge points + related API usage through video learning
    • Sort out the knowledge points and look up relevant mathematical theoretical support from the textbook

MORPGHOLOGY


  • Callback function : the main function calls the callback function, after the main function is executed, the callback function is executed
  • createTrackbar
    • 原型:  createTrackbar(const String& trackbarname, const String& winname,int* value, int count,TrackbarCallback onChange = 0, void* userdata = 0);
    • 实例:  createTrackbar("Element Size", "dst_image", &element_size, max_size,CallBack_Dem);
    • Parameters: the name of the sliding control, the name of the window displayed, the initial threshold (the parameter pointer, so the address is passed), the maximum value of the control scale, the callback function
      • The callback function can be understood as a function variable
      • The callback function prototype is void (*TrackbarCallback)(int pos, void* userdata);
        • (*TrackbarCallback) is the alias of function void (int pos, void* userdata)
        • The callback function is specifically generated for sliding controls,
        • The first parameter pos, it represents the position of the current slider, and its value is passed to him by createTrackbar(), which is the initial threshold
  • getStructuringElement
    • Get a structural element, how to understand???
      • It can be understood that the previous definition of filter\kernel is to declare an object with Mat first, and then assign the elements one by one, which is more troublesome. Here opencv encapsulates a function, and you can get the same thing by directly specifying the parameters. It just encapsulates the steps. No optimization
      • Get a kernel\filter by directly modifying the parameters
      • getStructruingElement(shape, ksize, anchor=None);
        • Kernel shape : MORPH_RECT rectangle, MORPH_CORSS cross, MORPH_ELLIPSE oval, etc.
        • The dimension size of the kernel , (n,n), as shown in Size(7,7);
        • Anchor point , (-1, -1) is the default value, as shown in the circle
        • What shape mentioned above is actually an arrangement of effective values ​​in a matrix , as shown in the figure
          • It can also have any other shape
  • Dilation : the operation to find the local maximum
    • Principle: The expansion or erosion operation is to convolve the image (or a part of the image, we call it A) with the kernel (we call it B)
      • The kernel can be any shape, it has a separately defined reference point, anchor anchor point, kernel can be understood as filter mask, etc.
      • Dilation is the operation of finding a local maximum. Kernel B is convolved with the graph, that is, calculating the maximum value of the pixels in the area covered by kernel B, and assigning this maximum value to the anchor. As the convolution kernel scans the image, it will make the image The highlight area in the medium gradually grows
      • Dilation can be simply understood as the process of merging all the background points in contact between B and A into A.
    • API: dilate
      • dilate(src0, dst, structureElement, Point(-1, -1));
      • Parameters: source image, result image, kernel, anchor point (-1, -1 is the default)
  • Corrosion : Corrosion is the operation of finding a local minimum.
    • Principle: Corrosion can be understood as the center of B (anchor point) walking along the inner boundary of A in a circle. Corrosion is also for the highlight part, the part outside A area <A highlight pixel, the inside is replaced by the outside. The pixels in A that can completely contain B are left.
      • Corrosion can be simply understood as the process of eliminating all boundary points of the object A.
      • When the binary image is black and white, it is easy to eliminate the small noise
    • API:erode(src0, dst, structureElement, Point(-1, -1);
  • open operation
    • Principle: Corrosion first and then expansion is called opening operation, the specific application still depends on the foreground and background colors
      • Can remove isolated small dots, small dots not in the image area or connecting bridges
    • API
      • morphologyEx(src0, dst, MORPH_OPEN, kernel);
  • close operation
    • Principle: expand first and then corrode
      • Fill the broken points and basin points inside the image
    • API
      • morphologyEx(src0, dst, MORPH_CLOSE, kernel);
  • Top hat
    • top hat is the original image-open image
    • API
      • morphologyEx(src0, dst, MORPH_TOPHAT, kernel);
  • Blackhat
    • Is the closed image-the source image
    • API
      • morphologyEx(src0, dst, MORPH_BLACKHAT, kernel);
  • Morphological Gradient
    • Principle: Expansion minus corrosion, also known as basic gradient
    • API
      • morphologyEx(src0, dst, MORPH_GRADIENT, kernel);
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>

using namespace std;
using namespace cv; 

Mat src0, src1, dst;
int element_size = 3;
int max_size = 21;
void CallBack_Dem(int, void*);

int main(int argc, char ** argv)
{
	src0 = imread("C:\\Users\\xujin\\Desktop\\test0.JPG");
	if (!src0.data)
	{
		cout << "no image";
		return -1;
	}
	namedWindow("src0_image", WINDOW_AUTOSIZE);
	imshow("src0_image", src0);
	namedWindow("dst_image", WINDOW_AUTOSIZE);
	createTrackbar("Element Size", "dst_image", &element_size, max_size,CallBack_Dem);
	//CallBack_Dem(0, 0);

	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	//morphologyEx(src0, dst, MORPH_OPEN, kernel);
	//morphologyEx(src0, dst, MORPH_CLOSE, kernel);
	//morphologyEx(src0, dst,MORPH_BLACKHAT, kernel);
	morphologyEx(src0, dst, MORPH_TOPHAT, kernel);
    //morphologyEx(src0, dst, MORPH_GRADIENT, kernel);

	imshow("dst_image", dst);

	waitKey(0);
	return 0;
}

void CallBack_Dem(int, void*)
{
	int s = element_size * 2 + 1;
	Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1));
	dilate(src0, dst, structureElement, Point(-1, -1));
	//erode(src0, dst, structureElement, Point(-1, -1);
	imshow("dst_image", dst);
	return;
}

 

Guess you like

Origin blog.csdn.net/Mrsherlock_/article/details/104510077