Experiment of edge detection of Susan operator and image processing of several rice grains using Visual Studio+OpenCV

1. Experimental content

        Counting grains of rice with computer vision.

The purpose and significance of the experiment

        The application of computer vision is very extensive. This experiment is to give students a simple understanding of the application of computer vision technology.

        1. Understand the experimental environment of computer vision. This experiment uses VS+OpenCV.

        2. Master the environment configuration method of OpenCV in VS.

        3. Learn how to use OpenCV to read and display images in VS.

        4. Grayscale processing can be performed on the read color image, and the value of each pixel in the grayscale image can be obtained.

        5. Understand the purpose and significance of image edge detection, and deepen the perceptual understanding of edge detection.

        6. Master simple image filtering methods.

        7. Master the SUNSAN edge detection method.

        8. Understand the purpose and meaning of image segmentation, and deepen the perceptual understanding of images.

        9. Master the simple image segmentation method.

        10. Understand the purpose and meaning of target shape detection, and deepen the perceptual understanding of shape detection.

        11. Master the shape detection method based on geometric features.

3. Experimental environment

         Visual Studio + OpenCV。

4. Experimental principle

         1. Install VS and OpenCV software;

        2. Configure OpenCV:

            (1) Configure environment variables.

            (2) Create a VS control project project and configure the project directory.

        3. The main image processing function: use imread to read an image, and use imshow to display the image.

        4. Convert a color image to a grayscale image.

        5. Perform median filtering on the rice grain image and use the medianBlur function.

        6. Use the SUNSAN operator to perform edge detection on the filtered rice grain image.

        7. Draw the grayscale histogram of the rice grain image;

        8. According to the histogram, determine the threshold value of the rice grain image segmentation, and convert the rice grain image into a binary image with only rice grains and background.

        9. Calculate the regional center of gravity, area, perimeter and other attributes of each grain of rice.

        10. Calculate the area of ​​various geometric shapes by the maximum and minimum distances.

        11. Output the number of rice grains in the rice grain image; the largest and the smallest number of rice grains.

5. Experimental results


6. Experimental code

(Only the main function code is shown, if you want to get all the code and resource files, please go to https://download.csdn.net/download/asdf1712/10401389 to download)

int main() {
	uint i;
	CvMemStorage * large = cvCreateMemStorage (0);
	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;
	int thresh1;
	double thresh2;
	Mat Img,binImg,subImg1, subImg2,ground, grayGround,otsuImg,maxrice;
	Mat grayImg,medImg,susanImg;
	// open the image
	Img = imread("..\\rice.jpg",1);

	ground = imread("..\\migrain image binary image.jpg", 1);
	imshow("groundtruth",ground);
	//Convert to grayscale image
	cvtColor(Img, grayImg, CV_BGR2GRAY);
	cvtColor(ground, grayGround, CV_BGR2GRAY);
	
	//median filter
	medianBlur(grayImg, medImg, 7);
	imshow("median filter", medImg);
	Mat histogram = getHistogramImage(medImg);
	imshow("Histogram", histogram);

	thresh1 = SelectThreshold(medImg);

	threshold(medImg,binImg,thresh1, 255, CV_THRESH_BINARY);
	namedWindow("binarized image");
	imshow("Binarized Image", binImg);

	susanImg = SusanFun(medImg);
	imshow("Susan Filter",susanImg);
	imwrite("Susan outline map.jpg", susanImg);

	findContours(binImg, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
	cout << "The number of rice grains is: " << contours.size() << endl;

	//find the largest grain of rice
	// area
	vector<float> area(contours.size());
	for (i = 0; i< contours.size(); i++) {
		area[i] = contourArea(contours[i]);
	}
	float area_max = 0;
	for (i = 0; i< area.size(); i++) {
		if (area[i] >area_max)
			area_max = area[i];
	}
	for (i = 0; i< area.size(); i++) {
		if (area[i] ==area_max)
			break;
	}
	maxrice = susanImg;
	drawContours(maxrice, contours,i, Scalar(255), CV_FILLED);
	imshow("Find the largest grain of rice", maxrice);

	//Subtract with groundtruth
	absdiff(binImg, grayGround, subImg1);//I=|I1-I2|;
	imshow("Subtraction between empirical threshold segmentation method and groundtruth", subImg1);

	thresh2= threshold(medImg, otsuImg, 0, 255, CV_THRESH_OTSU);
	imshow("Otsu method binarization", otsuImg);
	absdiff(otsuImg, grayGround, subImg2);//I=|I1-I2|;
	imshow("Otsu method and groundtruth subtraction", subImg2);
	
	waitKey(0);
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326582253&siteId=291194637