Object extraction and measurement in opencv-calculate cell area and circumference

Note: This tutorial is a record of teacher Jia Zhigang’s opencv course study. I would like to express my gratitude to teacher Jia.
Requirements: Calculate the perimeter and area of ​​the cells in the picture


Idea: Through binary segmentation + image morphology + contour extraction
Code:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
    
    
	Mat src = imread("/home/fuhong/code/cpp/opencv_learning/src/small_case/imgs/case6.png");
	if (src.empty()) {
    
    
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

	Mat blurImage;
	GaussianBlur(src, blurImage, Size(15, 15), 0, 0);
	imshow("blur", blurImage);

	Mat gray_src, binary;
	cvtColor(blurImage, gray_src, COLOR_BGR2GRAY);
	threshold(gray_src, binary, 0, 255, THRESH_BINARY | THRESH_TRIANGLE);
	imshow("binary", binary);

	Mat morphImage;
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	morphologyEx(binary, morphImage, MORPH_CLOSE, kernel, Point(-1, -1), 2);
	imshow("morphology", morphImage);

	vector<vector<Point>> contours;
	vector<Vec4i> hireachy;
	findContours(morphImage, contours, hireachy, CV_RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point());
	Mat connImage = Mat::zeros(src.size(), CV_8UC3);
	for (size_t t = 0; t < contours.size(); t++) {
    
    
		Rect rect = boundingRect(contours[t]);
		if (rect.width < src.cols / 2) continue;
		if (rect.width > (src.cols - 20)) continue;
		double area = contourArea(contours[t]);
		double len = arcLength(contours[t], true);
		drawContours(connImage, contours, static_cast<int>(t), Scalar(0, 0, 255), 1, 8, hireachy);
		printf("area  of star could : %f\n", area);
		printf("length  of star could : %f\n", len);
	}
	imshow("result", connImage);

	waitKey(0);
	return 0;
}

The results are as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/hongge_smile/article/details/108742885