Image Edge and Corner Detection Based on OpenCv

Write down the problems one by one. They are all mentally retarded.

1: Mat result1(5, 5, CV_8UC1, Scalar(255));

This sentence is to create a matrix of 5*5. The data type is CV_8UC1, which means that the unsigned character type C1 represents the number of channels 1 Scalar(255) represents the assignment of 255, result1(5, 5, CV_8UC2, Scalar(255, 0)); two channels, the first channel assignment is 255; the second channel assignment is 0.

2.:MorphoFeatures() :threShold(-1), cross(5, 5, CV_8U, Scalar(0))

Among them, MorphoFeatures is a well-defined class name, which should be used as a constructor, which is generally used to initialize variables. But add one after the constructor: there's something behind it that you don't know very well. Attached below is the answer:

The function of single colon (:):

Public: and private: The colon that follows indicates that all members defined later are public or private until the next public: or private: appears. Private: Processing by default.

The colon after the constructor acts as a separator. It is a method for the class to assign the member variables. It is more suitable for the const class member variables, such as the following:

class fun
	{
	public: int a;
	        Mat image;
		fun() :a(5), image(5, 5, CV_8SC1, Scalar(128))
		{
			cout << a << endl;
			cout << image << endl;
	        }
	};

3: Mat_<uchar>::const_iterator it = binary.begin<uchar>();

There are two ways to declare an iterator for an image. This way is to use the iterator type defined inside Mat_ Another way is:

Cv:: Mat Iterator < Vec3b > it // Declares an image iterator

const_iterator: Indicates that it is a pointer to a constant. Note! Here is a pointer to a constant, such as writing * it =4 in a program; the compiler will tell you that this is illegal, because it points to a constant and is not allowed to be modified. However, it values are variable.

4: The problem of variable definition is defined as: int threShold notices that S in threShold here is capitalized. When two words are involved in a general variable or function definition, the first word is lowercase and the second word is capitalized. Note that the function th of binarization is capitalized. Reshold () The letters in the function name are all lowercase, and the threshold variable will be a problem if it is defined in lowercase!! Just pay attention to the details. C++ is case-sensitive.

5: The concept of bit depth:

Example: 8-bit color map, bit depth is 8, expressed by 8 power of 2, which contains 256 colors (or 256 gray levels). Four-bit color can be called true color, and the bit depth is 24. It can be combined into 24 power colors of 2, that is, 16777216 colors. When we use 24-bit to record colors, it is actually 2 ^ (8 x 3), namely red, green and blue (RGB). Tricolor There are 256 kinds of colours, each of which has 8 power of 2. The combination of three colours forms 16 million kinds of colours.

6: The binarization operation of threshold () function is only for single channel images, which can be 8-bit uchar or 32-bit float.

7: Define the cross-shaped, diamond-shaped and x-shaped structural elements used in corner detection. I don't know why to define these things. What's the use of these things? How do they work in image processing? Why can we use these masks to get the angular features of the image? Why should cross expansion be used in diamond corrosion first???

Attach the code:

#include<opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
class MorphoFeatures
{
private:
	//Setting threshold of binarization
	int threShold;
	//Define the cross-diamond square x-shaped structural elements used in corner detection
	Mat cross;
	Mat diamond;
	Mat square;
	Mat  x;
public:
	MorphoFeatures() :
	threShold(-1), cross(5, 5, CV_8U, Scalar(0)), diamond(5, 5, CV_8U, Scalar(0)), square(5, 5, CV_8U, Scalar(1)),x(5,5,CV_8U,Scalar(0))
	{
		for (int i = 0; i < 5; i++)
		{
			cross.at<uchar>(2, i) = 1;
			cross.at<uchar>(i, 2) = 1;
		}

		diamond.at<uchar>(0, 2) = 1;
		diamond.at<uchar>(1, 1) = 1;
		diamond.at<uchar>(1, 3) = 1;
		diamond.at<uchar>(2, 0) = 1;
		diamond.at<uchar>(2, 4) = 1;
		diamond.at<uchar>(3, 1) = 1;
		diamond.at<uchar>(3, 3) = 1;
		diamond.at<uchar>(4, 2) = 1;
	    
		for (int i = 0; i < 5; i++)
		{
			x.at<uchar>(i, i) = 1;
			x.at<uchar >(4 - i, i) = 1;
		}
	}

	/*Binary threshold setting function*/
	void setThreshold(int t)
	{
		threShold = t;
	} 

	/*Image binarization function*/
	void applyThreshold(Mat & result)
	{
		if (threShold > 0)
		{
			threshold(result, result, threShold, 255, THRESH_BINARY);//Binarization function
		}
	}

	Mat getCorners(const Mat &image)
	{
		Mat result;
		//Cross expansion
		dilate(image, result, cross);
		
		//Rhombic corrosion
		erode(result, result, diamond);

		Mat result2;
		//X-shaped expansion
		dilate(image, result2, x);

		//Square corrosion
		erode(result2, result2, square);

		//Make a difference to get the corner image
		absdiff(result2, result, result);

		//Binary image to make it look clearer
		applyThreshold(result);
		return result;
	}


	/*Boundary acquisition function*/
	Mat getEdges(const Mat &image)
	{
		Mat result;
		morphologyEx(image, result, MORPH_GRADIENT, Mat());	
		applyThreshold(result);
		return result;
	}
	
	/*Draw a circle for the corner */

	void drawOnImage(const Mat &binary, Mat &image)
	{
		Mat_<uchar>:: const_iterator it = binary.begin<uchar>();
		Mat_<uchar>::const_iterator itEnd = binary.end<uchar>();
		for (int i = 0; it != itEnd; i++, it++)
		{
			if (*it)
			{
				circle(image, Point(i%image.step, i / image.step), 5, Scalar(255, 0, 0));
			}
		}

	}
};
int main()
{
	MorphoFeatures morphology;
	morphology.setThreshold(40);
	Mat image = imread("Test.jpg",IMREAD_GRAYSCALE);
	Mat edges,corners;
	edges = morphology.getEdges(image);
	imshow("Edge image", edges);

	corners = morphology.getCorners(image);
	imshow("Corner image", corners);

	//corners is a binary image which is used to locate the position of the image edges so that it can be labeled on the image.
	morphology.drawOnImage(corners, image); 
	imshow("Draw a circle with corners", image);

	waitKey(0);
	return 0;
}

 

 

 

 

Keywords: angular OpenCV

Added by [JayZ] on Sat, 07 Sep 2019 15:28:14 +0300