POJ1009

边缘检测算法疑难解析
本文介绍了一种边缘检测算法的实现细节,并分享了在提交评测平台后遇到的问题。该算法使用C++编写,通过一系列变换点来计算图像边缘。文章讨论了可能引发错误的原因,并邀请读者共同探讨解决方案。

自己测试都没问题,但提交后却是Wrong Answer,不知道问题出在哪儿。

C++源代码:

//	Edge Detection
//	2017.03.19 by wyj
#include  
#include  
#include
#include
#include
using namespace std;


int Inmap[1100][2], Outmap[10000][2];
int width;
int total;

int main()
{
	int GetValue(int pos);
	int GetCode(int pos);
	int comp(const void*a, const void*b);

	int pixelout;
	int In[1100],Out[10000];			//输入变换点的下标和对应周围点的下标

	while (scanf_s("%d", &width) && width)
	{
		int n = 0, i = 0, j = 0, k = 0, p = 0;
		while ((cin >> Inmap[i][0] >> Inmap[i][1]) && (Inmap[i][0] || Inmap[i][1]))
		{
			In[i] = n;					//第i个输入变化点的下标
			n = n + Inmap[i][1];
			i++;
		}
		total = n;

		for (p = 0;p < i;p++)
		{
			int row = In[p] / width;
			int col = In[p] % width;
			for (int x = row - 1;x <= row + 1;x++)
				for (int y = col - 1;y <= col + 1;y++)
				{
					if (x < 0 || y < 0 || y >= width || x*width + y >=total )
						continue;
					Out[k++] = x*width + y;
				}
		}

		qsort(Out, k, sizeof(int), comp);

		Outmap[0][0] = GetCode(0);
		Outmap[0][1] = 1;
		int t = 0;
		for (p = 1;p < k;p++)
		{
			if (Out[p] == Out[p - 1])
				continue;
			pixelout = GetCode(Out[p]);
			
			if (pixelout != GetCode(Out[p] - 1))				
			{
				Outmap[++j][0] = pixelout;
				Outmap[j-1][1] = Out[p] - t;
				t += Outmap[j - 1][1];
			}
		}
		Outmap[j][1] = total - t;

		cout << width << endl;
		for (k = 0;k <= j;k++)
			cout << Outmap[k][0] << ' ' << Outmap[k][1] << endl;
		cout <<"0 0"<< endl;

	}

	cout << 0<< endl;

	return 0;

}

/*快排比较规则*/
int comp(const void*a, const void*b)
{
	return *(int*)a - *(int*)b;
}

/*返回第pos个像素点的像素值*/
int GetValue(int pos)
{
	int i = 0, p = 0;
	while (p<=pos)
		p += Inmap[i++][1];

	return Inmap[i - 1][0];
}

/*返回第pos个像素点的编码*/
int GetCode(int pos)
{
	int code = GetValue(pos);
	int max(int a, int b, int c, int d, int e);
	int max(int a, int b, int c, int d, int e, int f, int g, int h);
	
	int MaxAbs = 0;
	int a[8];
	int k;
	a[0] = pos - width - 1;
	a[1] = pos - width;
	a[2] = pos - width + 1;
	a[3] = pos - 1;
	a[4] = pos + 1;
	a[5] = pos + width - 1;
	a[6] = pos + width;
	a[7] = pos + width + 1;
	for (k = 0;k < 8;k++)
	{
		if (a[k] < 0 || a[k] >= total)
			a[k] = pos;
	}

	if (pos%width == 0)
		MaxAbs = max(abs(GetValue(pos) - GetValue(a[1])), abs(GetValue(pos) - GetValue(a[2])),
			abs(GetValue(pos) - GetValue(a[4])), abs(GetValue(pos) - GetValue(a[6])), abs(GetValue(pos) - GetValue(a[7])));
	else
		if (pos%width == width - 1)
			MaxAbs = max(abs(GetValue(pos) - GetValue(a[0])), abs(GetValue(pos) - GetValue(a[1])),
				abs(GetValue(pos) - GetValue(a[3])), abs(GetValue(pos) - GetValue(a[5])), abs(GetValue(pos) - GetValue(a[6])));
		else
			MaxAbs = max(abs(GetValue(pos) - GetValue(a[0])), abs(GetValue(pos) - GetValue(a[1])),
				abs(GetValue(pos) - GetValue(a[2])), abs(GetValue(pos) - GetValue(a[3])), abs(GetValue(pos) - GetValue(a[4])),
				abs(GetValue(pos) - GetValue(a[5])), abs(GetValue(pos) - GetValue(a[6])), abs(GetValue(pos) - GetValue(a[7])));

	return MaxAbs;
}

int max(int a, int b, int c, int d, int e)
{
	if (b > a)
		a = b;
	if (c > a)
		a = c;
	if (d > a)
		a = d;
	if (e > a)
		a = e;
	return a;
}

int max(int a, int b, int c, int d, int e, int f, int g, int h)
{
	if (b > a)
		a = b;
	if (c > a)
		a = c;
	if (d > a)
		a = d;
	if (e > a)
		a = e;
	if (f > a)
		a = f;
	if (g > a)
		a = g;
	if (h > a)
		a = h;
	return a;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值