POJ 1009 Edge Detection

Edge Detection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20798 Accepted: 4897
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
Hint

A brute force solution that attempts to compute an output value for every individual pixel will likely fail due to space or time constraints.

#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<algorithm>

#define SIZE 1005//根据题目设置的pair的大小

using namespace std;

typedef struct PAIRNUM{
    int value;//保存的是编码的值
    int num;//保存的是连续编码的个数
}pairNum;

typedef struct PIX{
    int positin;//该像素的位置,从1开始计数
    int value;//该像素的编码
}pix;

vector<pairNum> inputNum(1005);
vector<pix> outPutPix(1005 * 8);//保存的是输出像素点的结果
int width;//图像的宽度
int totalPixNum = 0;//记录一共有多少个像素点

int GetPix(int position)//根据位置得到像素点的值
{
    int i = 0;
    int total = 0;//前边位置点的总数
    while (1)
    {
        if (total >= position)
        {
            break;
        }
        total += inputNum[i++].num;
    }
    return inputNum[i-1].value;//返回该点的像素值
}

int GetValue(int position)
{
    int currentPix = GetPix(position);//得到当前点的像素值
    int row = (position-1) / width;
    int col = (position-1) % width;
    int maxValue = 0;

    for (int m = row - 1; m <= row + 1; m++)
    {
        for (int n = col - 1; n <= col + 1; n++)
        {
            int arroundPOsition = m*width + n;
            if ( m<0 || n<0 || n>= width || arroundPOsition>= totalPixNum||arroundPOsition==position-1)
            {
                continue;
            }
            int arroundPix = GetPix(arroundPOsition+1);
            if (maxValue < abs(arroundPix - currentPix))
            {
                maxValue = abs(arroundPix - currentPix);
            }
        }
    }
    return maxValue;
}
bool Comp(const PIX a, const PIX b)
{
    return a.positin < b.positin;
}
int main()
{
    while (cin >> width)
    {
        totalPixNum = 0;
        if (width == 0)
        {
            break;
        }
        int pix, num;
        int pairCount = 0;//记录输入的数据组数
        int outPutNum = 0;//记录输出的pix的个数
        while (cin >> pix >> num)
        {
            if (pix == 0 && num == 0)
            {
                break;
            }
            pairNum p;
            p.value = pix;
            p.num = num;
            inputNum[pairCount++] = p;
            totalPixNum += num;//更新总数的值
        }
        cout << width << endl;//按照题目要求输出结果
        //输入完成
        int currentPosition = 1;
        for (int i = 0; i <= pairCount; i++)
        {
            int row = (currentPosition - 1) / width;//当前点的纵坐标
            int col = (currentPosition - 1) % width;//当前点的横坐标
            for (int m = row - 1; m <= row + 1; m++)//遍历该点周围的8个点,因为边缘处输出点值可能会变化
            {
                for (int n = col - 1; n <= col + 1; n++)
                {
                    int arroungPosition = m*width + n;//得到当前点值的position
                    if (m<0 || n>=width || n<0 || arroungPosition >= totalPixNum)//越界的跳过
                    {
                        continue;
                    }
                    PIX tempPix;
                    tempPix.positin = arroungPosition+1;
                    tempPix.value = GetValue(arroungPosition+1);
                    outPutPix[outPutNum++] = tempPix;
                }
            }

            currentPosition += inputNum[i].num;
        }
        sort(outPutPix.begin(), outPutPix.begin() + outPutNum, Comp);

        PIX tempPix = outPutPix[0];
        for (int i = 0; i < outPutNum; i++)
        {
            if (outPutPix[i].value == tempPix.value)
            {
                continue;
            }
            cout << tempPix.value << " " << outPutPix[i].positin - tempPix.positin << endl;
            tempPix = outPutPix[i];
        }
        cout << tempPix.value << " " << totalPixNum - tempPix.positin + 1<< endl;
        cout << "0 0" << endl;
    }
    cout << "0" << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值