Edge Detection

Edge Detection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 21730 Accepted: 5099
Description

IONU Satellite Imaging, Inc. records and stores very large images using run length encoding. You are to write a program that reads a compressed image, finds the edges in the image, as described below, and outputs another compressed image of the detected edges.
A simple edge detection algorithm sets an output pixel’s value to be the maximum absolute value of the differences between it and all its surrounding pixels in the input image. Consider the input image below:

The upper left pixel in the output image is the maximum of the values |15-15|,|15-100|, and |15-100|, which is 85. The pixel in the 4th row, 2nd column is computed as the maximum of |175-100|, |175-100|, |175-100|, |175-175|, |175-25|, |175-175|,|175-175|, and |175-25|, which is 150.
Images contain 2 to 1,000,000,000 (109) pixels. All images are encoded using run length encoding (RLE). This is a sequence of pairs, containing pixel value (0-255) and run length (1-109). Input images have at most 1,000 of these pairs. Successive pairs have different pixel values. All lines in an image contain the same number of pixels.
Input

Input consists of information for one or more images. Each image starts with the width, in pixels, of each image line. This is followed by the RLE pairs, one pair per line. A line with 0 0 indicates the end of the data for that image. An image width of 0 indicates there are no more images to process. The first image in the example input encodes the 5x7 input image above.
Output

Output is a series of edge-detected images, in the same format as the input images, except that there may be more than 1,000 RLE pairs.
Sample Input

7
15 4
100 15
25 2
175 2
25 5
175 2
25 5
0 0
10
35 500000000
200 500000000
0 0
3
255 1
10 1
255 2
10 1
255 2
10 1
255 1
0 0
0
Sample Output

7
85 5
0 2
85 5
75 10
150 2
75 3
0 2
150 2
0 4
0 0
10
0 499999990
165 20
0 499999990
0 0
3
245 9
0 0
0

暴力式解法,不过不能运行,因为不能创建像int[10000000]这么大的数组

#include <iostream>
#include<math.h>
using namespace std;

int max(int num[],int n) {
	int max = num[0];
	for (int i=0; i < 8; i++)
		if (num[i] > max) max = num[i];
	return max;
}
struct pl {
	int a;
	int b;
}p[1000];
int main()
{
	int n;
	int a=1, b=1;
	int *p = new int[10000000];
	int *r = new int[10000000];
	for (int i = 0; i < 10000000; i++)
		r[i] = -1;
	int j = 0;

	while (cin >> n) {
		while (a != 0 && b != 0) {
			cin >> a >> b;
			for (int i = 0; i < b; i++)
			{
				p[j] = a;
				j++;
			}
		}
			for (int i = 0; i < j; i++)
			{
				int num[8];
				for (int i = 0; i < 8; i++)
					num[i] = 0;

				if (i % n == 0) {
					if (i - n >= 0 && i - n < j)
						num[4] = abs(p[i] - p[i - n]);
					if (i - n + 1 >= 0 && i - n + 1 < j)
						num[3] = abs(p[i] - p[i - n + 1]);
					if (i + 1 >= 0 && i + 1 < j)
						num[1] = abs(p[i] - p[i + 1]);
					if (i + n >= 0 && i + n < j)
						num[6] = abs(p[i] - p[i + n]);
					if (i + n + 1 >= 0 && i + n + 1 < j)
						num[2] = abs(p[i] - p[i + n + 1]);
				}
				else if ((i - n + 1) % n == 0) {

					if (i - n - 1 >= 0 && i - n - 1 < j)
						num[0] = abs(p[i] - p[i - n - 1]);
					if (i - n >= 0 && i - n < j)
						num[4] = abs(p[i] - p[i - n]);
					if (i - 1 >= 0 && i - 1 < j)
						num[7] = abs(p[i] - p[i - 1]);
					if (i + n - 1 >= 0 && i + n - 1 < j)
						num[5] = abs(p[i] - p[i + n - 1]);
					if (i + n >= 0 && i + n < j)
						num[6] = abs(p[i] - p[i + n]);
				}
				else {
					if (i - n - 1 >= 0 && i - n - 1 < j)
						num[0] = abs(p[i] - p[i - n - 1]);
					if (i - n >= 0 && i - n < j)
						num[4] = abs(p[i] - p[i - n]);
					if (i - n + 1 >= 0 && i - n + 1 < j)
						num[3] = abs(p[i] - p[i - n + 1]);
					if (i - 1 >= 0 && i - 1 < j)
						num[7] = abs(p[i] - p[i - 1]);
					if (i + 1 >= 0 && i + 1 < j)
						num[1] = abs(p[i] - p[i + 1]);
					if (i + n - 1 >= 0 && i + n - 1 < j)
						num[5] = abs(p[i] - p[i + n - 1]);
					if (i + n >= 0 && i + n < j)
						num[6] = abs(p[i] - p[i + n]);
					if (i + n + 1 >= 0 && i + n + 1 < j)
						num[2] = abs(p[i] - p[i + n + 1]);


				}
				r[i] = max(num, 8);
			}
			int numbers = 1;
			cout << n << endl;
			for (int i = 1; i <= j; i++)
			{
				if (r[i] == r[i - 1]) { numbers++; }
				else {
					cout << r[i - 1] << " " << numbers << endl;
					numbers = 1;
				}

			}
			cout << "0 0" << endl;
			a = 1, b = 1; j = 0;
	}
	cout << "0" << endl;
}

这道题的解法采用的是所谓的跳跃式编码,即只有像素发生变化的点(每次从变化之后的第一个数编码)及其周围八个像素点才有变化的可能,其余的点如果没有受到变化的点的影响,则保持跟左边的点相同的像素

也就是说,我们只需要对变化的点及其周围8个点重新进行编码,这样编码的次数不会超过8000次(题目说输入的点对不会超过1000),我们只要将这8000个像素对编码后的(像素,位置)记录下来,并根据所在位置的大小排序(因为这8000对像素的位置可能是重复的,即一个像素点周围可能有多个变化点而重新编码多次),依次输出(这一个像素对应的值 下一个像素值的第一个像素对应的位置-这一个像素的第一个像素所在的位置)

#include <iostream>
#include<math.h>
#include<algorithm>
using namespace std;

struct inn {
	int value_of_pixel;
	int number_of_pixel;

}Input[1000];
struct out {
	int value_of_pixel;
	int position_of_pixel;
}Output[100000];

int getvalue(int position,inn input[1000]) {
	int sum = 0;
	int i = 0;
	while (position  >sum) {
		sum += input[i].number_of_pixel;
		i++;
	}
	return input[i - 1].value_of_pixel;
}
int encode(int row, int column,int width,int end, inn input[1000],int totalRow) {
	int positon= (row -1)* width + column;
	int value = getvalue(positon,input);
	int maxValue = 0;
	for (int i = row - 1; i <= row + 1; i++)
		for (int j = column - 1; j <= column + 1; j++)
		{
			int pos = (i-1) * width + j;
			if (i < 1 ||i>totalRow|| j < 1 || j > width || pos >end)
				continue;
			int arroundValue = getvalue(pos,input);
			int g = 0;
			g=abs(value - arroundValue);
			maxValue = g > maxValue ? g : maxValue;
		}
	return maxValue;
}
bool cmp(out a, out b) {
	return a.position_of_pixel < b.position_of_pixel;
}
int main()
{
	int width_of_picture=1;
	int numbers_of_input=0;
	int totalnumber_of_pixel=0;
	
	while (cin>>width_of_picture) {
		
		if (width_of_picture == 0) {
			cout << "0" << endl; 
			break;
		}
			while (cin >> Input[numbers_of_input].value_of_pixel >> Input[numbers_of_input].number_of_pixel&&Input[numbers_of_input].number_of_pixel != 0) {
				totalnumber_of_pixel += Input[numbers_of_input].number_of_pixel;
				numbers_of_input++;
			}
			int totalRow = totalnumber_of_pixel / width_of_picture;

			int g = -1;
			int Current_positon = 1;
			for (int i = 0; i < numbers_of_input; i++) {
				int row = Current_positon / width_of_picture + 1;
				int column = Current_positon % width_of_picture;
				for (int j = row - 1; j <= row + 1; j++)
					for (int k = column - 1; k <= column + 1; k++)
					{
						int posInLine = (j - 1)* width_of_picture + k;
						if (j < 1 || j>totalRow || k <1 || k >width_of_picture || posInLine > totalnumber_of_pixel)
							continue;
						else {
							g++;
							Output[g].value_of_pixel = encode(j, k, width_of_picture, totalnumber_of_pixel, Input, totalRow);
							Output[g].position_of_pixel = posInLine;

						}
					}
				Current_positon += Input[i].number_of_pixel;

			}
			g++;
			Output[g].value_of_pixel = encode(totalRow, width_of_picture, width_of_picture, totalnumber_of_pixel, Input, totalRow);
			Output[g].position_of_pixel = totalnumber_of_pixel;

			sort(Output, Output + g + 1, cmp);


			out out = Output[0];
			cout << width_of_picture << endl;
			for (int i = 0; i < g; i++) {
				if (out.value_of_pixel == Output[i].value_of_pixel) continue;
				cout << out.value_of_pixel << " " << (Output[i].position_of_pixel - out.position_of_pixel) << endl;
				out = Output[i];

			}
			cout << out.value_of_pixel << " " << (Output[g].position_of_pixel - out.position_of_pixel + 1) << endl;
			cout << "0 0" << endl;
			width_of_picture = 1;
			numbers_of_input = 0;
			totalnumber_of_pixel = 0;
   }
	return 0;
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值