Skip to content

[C++ and opencv] image expansion and control

The last time to write the image of the corrosion, download to add the control bar.

In the opencv document has a corrosion and expansion processing, is the use of shell bar to see the program [click here], but this code is to imitate the Internet and any similarity is purely coincidental ^_^ [], is a little different from the method and opencv documents.

Clearly in the opencv document on the corrosion was introduced, I will no longer speak more. In fact, I also do not understand, also a rookie^_^

In the code, or have a look a function to use.

**********************************************************************************************

1.Mat getStructuringElement(int shape, Size ksize, Point anchor=Point(-1,-1))

shape –The shape of the elements availablevalue:
– MORPH_RECT – a rectangular structuring element
– MORPH_ELLIPSE – an elliptic structuring element, that is, a filled ellipse inscribed
into the rectangle Rect(0, 0, esize.width, 0.esize.height)
– MORPH_CROSS – a cross-shaped structuring element:
– CV_SHAPE_CUSTOM – custom structuring element (OpenCV 1.x API)

ksize –- the size of structural elements

2.int createTrackbar(const string& trackbarname, const string& winname, int* value, int count,
TrackbarCallback onChange=0, void* userdata=0)

Code:

#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include <iostream>
#include <windows.h>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	FreeConsole();
    //Read image
	Mat image;
	image= imread("F://Opencv_picture//s4.jpg");
	if(image.empty())
	{
	    cout<<"Cannot find or open image"<<endl;
		return -1;
	}
	//Create window
	namedWindow("Image", 0);
	//Create trackbar 
	int erosion_elem= 0;
	int erosion_size= 0;
	//Create corrosion control.
	createTrackbar("Element", "Image", &erosion_elem, 2);  
    createTrackbar("Kernel", "Image", &erosion_size, 21);
	while(true)
	{    
	     Mat dst;
		 
		 int erosion_type;
		 if(erosion_elem== 0)
		 {
		    erosion_type= MORPH_RECT;
		 }
		 else if(erosion_elem==1)
		 {
		    erosion_type= MORPH_CROSS;
		 }
		 else if(erosion_elem)
		 {
			  erosion_type= MORPH_ELLIPSE;
		 }
		 Mat element= getStructuringElement(erosion_type,
			                                                         Size(2* erosion_size+ 1, 2*erosion_size+ 1),
		   														     Point(erosion_size, erosion_size) );
		 //Corrosion operation
		 erode(image, dst, element);
		 //show image
		 imshow("Image", dst);
		 //wait untill user press any key
		 if(waitKey(33)== 27)
		 {
		     break;
		 }
	}
	return 0;
}

Results figure:

PS: Forget that, control Elment[0, 2] [shape] is selected, in the election of 1: MORPH_CROSS

Leave a Reply

Your email address will not be published. Required fields are marked *