图像分割评价指标---mIoU(平均交并比)计算代码(OpenCV/C++)

图像分割评价指标---mIoU(平均交并比)计算代码(OpenCV/C++)

运行环境:Visual Studio+OpenCV+C++
输入:准备好的Groundtruth数据+分割得到的二值图像
输出:每张分割结果的IOU值+最后输出MIOU值(平均交并比)

mIoU计算的基本原理可见其他博主的叙述,这里不作介绍。

IOU(交并比)计算子程序如下:

double MIOU(cv::Mat image_ground, cv::Mat image_daice)
{
	//初始化各个计数器
	double jiaoji = 0.0;
	double duli_ground = 0.0;
	double duli_daice = 0.0;
	double bingji = 0.0;
	double iou = 0.0;

	for (int i = 0; i < image_ground.cols; i++)
	{
		for (int j = 0; j < image_ground.rows; j++)
		{
		    //计算交集
			if ((image_ground.at<uchar>(j, i) == 255) && (image_daice.at<uchar>(j, i) == 255))
				jiaoji = jiaoji + 1.0;
			//计算groundtruth内物体所占的像素个数
			if (image_ground.at<uchar>(j, i) == 255)
				duli_ground = duli_ground + 1.0;
			//计算分割图像得到的物体的像素个数
			if (image_daice.at<uchar>(j, i) == 255)
				duli_daice = duli_daice + 1.0;
		}
	}
	//计算并集,等于Groundtruth像素与分割结果像素之和,再减去交集
	bingji = duli_ground + duli_daice - jiaoji;
	iou = (double)(jiaoji / bingji); //计算比值

	cout << iou << endl; //打印
	return iou; //输出
}

MIOU计算主程序如下:

int main()
{
	double sum = 0.0;
	double miou = 0.0;

	for (int i = 0; i <= 99; i++)  //数据集中有几张图,需要改一下数字
	{
		//批量读取groudtruth
		cv::Mat image_ground = cv::imread("C:/Users/Desktop/数据集/" + to_string(i) + ".png", CV_LOAD_IMAGE_GRAYSCALE);

		//批量读取待测分割图像
		cv::Mat image_daice = cv::imread("C:/Users/Desktop/开发工程/" + to_string(i) + "_threshold.png", CV_LOAD_IMAGE_GRAYSCALE);

		//调取计算iou子函数
		double iou_meige = MIOU(image_ground, image_daice);

		sum = sum + iou_meige; //计算总的IOU值

	}
	miou = sum / 100.0;////这里要改照片数量
	cout << "MIOU:" << miou << endl;

	cin.get();
	return 0;
}

整体代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define cvQueryHistValue_1D(hist,idx0)\
((float)cvGetReal1D((hist)->bins,(idx0)))

using namespace std;

double MIOU(cv::Mat image_ground, cv::Mat image_daice)
{
	//初始化各个计数器
	double jiaoji = 0.0;
	double duli_ground = 0.0;
	double duli_daice = 0.0;
	double bingji = 0.0;
	double iou = 0.0;

	for (int i = 0; i < image_ground.cols; i++)
	{
		for (int j = 0; j < image_ground.rows; j++)
		{
		    //计算交集
			if ((image_ground.at<uchar>(j, i) == 255) && (image_daice.at<uchar>(j, i) == 255))
				jiaoji = jiaoji + 1.0;
			//计算groundtruth内物体所占的像素个数
			if (image_ground.at<uchar>(j, i) == 255)
				duli_ground = duli_ground + 1.0;
			//计算分割图像得到的物体的像素个数
			if (image_daice.at<uchar>(j, i) == 255)
				duli_daice = duli_daice + 1.0;
		}
	}
	//计算并集,等于Groundtruth像素与分割结果像素之和,再减去交集
	bingji = duli_ground + duli_daice - jiaoji;
	iou = (double)(jiaoji / bingji); //计算比值

	cout << iou << endl; //打印
	return iou; //输出
}

int main()
{
	double sum = 0.0;
	double miou = 0.0;

	for (int i = 0; i <= 99; i++)  //数据集中有几张图,需要改一下数字
	{
		//批量读取groudtruth
		cv::Mat image_ground = cv::imread("C:/Users/Desktop/数据集/" + to_string(i) + ".png", CV_LOAD_IMAGE_GRAYSCALE);

		//批量读取待测分割图像
		cv::Mat image_daice = cv::imread("C:/Users/Desktop/开发工程/" + to_string(i) + "_threshold.png", CV_LOAD_IMAGE_GRAYSCALE);

		//调取计算iou子函数
		double iou_meige = MIOU(image_ground, image_daice);

		sum = sum + iou_meige; //计算总的IOU值

	}
	miou = sum / 100.0;////这里要改照片数量
	cout << "MIOU:" << miou << endl;

	cin.get();
	return 0;
}

运行效率高,速度快。如有问题,可留言咨询。
完整程序包可见本人分享的资源,进行下载
或从Github免费下载,地址:Github/Code

你可能感兴趣的:(OpenCV,C++,mIOU,平均交并比,代码)