FCM算法C++代码实现,Fuzzy-C-Means算法C++实现图像聚类,用opencv读取图片

#include 
#include "cv.h"
#include   
#include   
#include 
#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include
using namespace std;
using namespace cv;

double *imgFuzzyCmeans(Mat img)
{
	int length = img.rows;
	int width = img.cols;
	vector > A;
	vector temp;
	double v1 = img.at(length / 2, width / 2);
	double v2 = img.at(length / 3, width / 3);
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < width; j++)
		{
			temp.push_back(img.at(i, j));
		}
		A.push_back(temp);
		temp.clear();
	}
	int i = 0;
	int j = 0;
	vector > p[2];
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < width; j++)
		{
			temp.push_back(0.000000);
		}
		p[0].push_back(temp);
		temp.clear();
	}//初始化
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < width; j++)
		{
			temp.push_back(0.000000);
		}
		p[1].push_back(temp);
		temp.clear();
	}//初始化
	while (i < length)
	{
		while (j < width)
		{
			/*p(i, j, 1) = abs(A[i][j]) - v1);
			p(i, j, 2) = abs(double(A(i, j)) - double(v2));
			p(i, j, 3) = abs(double(A(i, j)) - double(v3));
			j = j + 1;*/
			p[0][i][j] = abs(A[i][j] - v1);
			p[1][i][j] = abs(A[i][j] - v2);
			j++;
		}
		j = 0;
		i = i + 1;
	}
	double V1 = -100;
	double V2 = -100;
	int x = 0;
	vector > u[2];//u代表归属度
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < width; j++)
		{
			temp.push_back(0.000000);
		}
		u[0].push_back(temp);
		temp.clear();
	}//初始化
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < width; j++)
		{
			temp.push_back(0.000000);
		}
		u[1].push_back(temp);
		temp.clear();
	}//初始化
	while (abs(v1 - V1)>1 || abs(v2 - V2)>1)
	{
		V1 = v1;
		V2 = v2;
		for (int i = 0; i < length; i++)
		{
			for (int j = 0; j < width; j++)
			{
				if (p[0][i][j] < 1)
				{
					u[0][i][j] = 1;
				}
				else if (p[1][i][j] < 1)
				{
					u[1][i][j] = 1;
				}
				else
				{
					//u(i, j, 1) = 1 / [(p(i, j, 1) / p(i, j, 1)) ^ 2 + (p(i, j, 1) / p(i, j, 2)) ^ 2 + (p(i, j, 1) / p(i, j, 3)) ^ 2];
					//u(i, j, 2) = 1 / [(p(i, j, 2) / p(i, j, 1)) ^ 2 + (p(i, j, 2) / p(i, j, 2)) ^ 2 + (p(i, j, 2) / p(i, j, 3)) ^ 2];
					u[0][i][j] = 1 / ((p[0][i][j] / p[0][i][j])*(p[0][i][j] / p[0][i][j]) + (p[0][i][j] / p[1][i][j])*(p[0][i][j] / p[1][i][j]));
					u[1][i][j] = 1 / ((p[1][i][j] / p[0][i][j])*(p[1][i][j] / p[0][i][j]) + (p[1][i][j] / p[1][i][j])*(p[1][i][j] / p[1][i][j]));
				}
			}
		}
		double a1 = 0;
		double a2 = 0;
		double b1 = 0;
		double b2 = 0;
		for (int i = 0; i < length; i++)
		{
			for (int j = 0; j < width; j++)
			{
				a1 = a1 + u[0][i][j] * u[0][i][j] * A[i][j];
				a2 = a2 + u[0][i][j] * u[0][i][j];
				b1 = b1 + u[1][i][j] * u[1][i][j] * A[i][j];
				b2 = b2 + u[1][i][j] * u[1][i][j];;
			}
		}
		v1 = a1 / a2;
		v2 = b1 / b2;
		for (int i = 0; i < length; i++)
		{
			for (int j = 0; j < width; j++)
			{
				//p(i, j, 1) = abs(double(A(i, j)) - double(v1));
				//p(i, j, 2) = abs(double(A(i, j)) - double(v2));
				p[0][i][j] = abs(A[i][j] - v1);
				p[1][i][j] = abs(A[i][j] - v2);
			}
		}
	}
	double q[2] = {0.0000};
	q[0] = v1;
	q[1] = v2;
	return q;

}
int main()
{
	Mat img = imread("G:\\图.png");
	double *a;
	a = imgFuzzyCmeans(img);
	double x1 = a[0];
	double x2 = a[1];
	cout << x1 << endl;
	cout << x2 << endl;
	system("pause");
	return 0;
}

整个计算过程全部手写,我将一张灰度图聚合为两个类,读者可按照需求自己修改。

使用opencv主要是读取图像。方便。

代码实现部分是将读取的图像(灰度图)赋值给一个A矩阵,A矩阵用二维vector形式。计算时候也是用A。p[0][i][j]表示图像第i,j个像素点到第0个聚类中心点的距离。

同理p[1][i][j]

u[0][i][j]表示第[i][j]个像素点到第0个聚类中心点的归属度。

程序运行时候若保错,把属性——》C++预处理器,加上  _CRT_SECURE_NO_WARNINGS即可

你可能感兴趣的:(FCM算法C++代码实现,Fuzzy-C-Means算法C++实现图像聚类,用opencv读取图片)