POJ 1009

Edge Detection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18826 Accepted: 4359

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 (10 9) 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-10 9). 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

import java.util.ArrayList;
import java.util.Scanner;

public class Poj1009 {
	
	public static void main(String[] args) {
	
		Scanner sc = new Scanner(System.in);
		
		while(true){
			//读取图片列值,如果为0,则退出程序
			int column = sc.nextInt();
			
			if(column == 0){
				break;
			}
			
			ArrayList<Integer> arr1 = new ArrayList<Integer>();
			
			while(sc.hasNext()){
				//获取像素值
				int pixel = sc.nextInt();
				//获取重复次数
				int len = sc.nextInt();
				
				if(pixel == 0 && len == 0){
					break;
				}
				
				for(int i=0; i<len; i++){
					arr1.add(pixel);
				}
			}
			
			//图像像素值总长度
			int size = arr1.size();
			//二维表行数
			int row = size/column;
			
			//新建输入图片数组
			int[][] inputImage = new int[row][column];
			//新建输出图片数组
			int[][] outputImage = new int[row][column];
			
			//初始化输入图片数组
			for(int i=0; i<row; i++){
				for(int j=0; j<column; j++){
					inputImage[i][j] = arr1.get(i*column + j);
				}
			}
			
			//存储一个某像素与周边8个像素之间的绝对值
			int[] nums = new int[8];
			
			for(int i=0; i<row; i++){
				for(int j=0; j<column; j++){
					
					int max = 0;
					
					//寻找周边像素值,如果没有则为-1
					int num1 = (i-1)<0||(j-1)<0 ? -1 : inputImage[i-1][j-1];
					int num2 = (i-1)<0 ? -1 : inputImage[i-1][j];
					int num3 = (i-1)<0||(j+1)>=column ? -1 : inputImage[i-1][j+1];
					int num4 = (j+1)>=column ? -1 : inputImage[i][j+1];
					int num5 = (i+1)>=row || (j+1)>=column ? -1 : inputImage[i+1][j+1];
					int num6 = (i+1)>=row ? -1 : inputImage[i+1][j];
					int num7 = (i+1)>=row || (j-1)<0 ? -1 : inputImage[i+1][j-1];
					int num8 = (j-1)<0 ? -1 : inputImage[i][j-1];
					
					//计算与周边像素值的绝对值,如果没有则为-1
					nums[0] = num1==-1?num1:Math.abs(inputImage[i][j] - num1);
					nums[1] = num2==-1?num2:Math.abs(inputImage[i][j] - num2);;
					nums[2] = num3==-1?num3:Math.abs(inputImage[i][j] - num3);;
					nums[3] = num4==-1?num4:Math.abs(inputImage[i][j] - num4);;
					nums[4] = num5==-1?num5:Math.abs(inputImage[i][j] - num5);;
					nums[5] = num6==-1?num6:Math.abs(inputImage[i][j] - num6);;
					nums[6] = num7==-1?num7:Math.abs(inputImage[i][j] - num7);;
					nums[7] = num8==-1?num8:Math.abs(inputImage[i][j] - num8);;
					
					for(int k=0; k<nums.length; k++){
						if(nums[k] != -1){
							//该值不为-1,说明周边该像素存在
							if(max < nums[k]){
								max = nums[k];
							}
						}
					}
					//已经找出绝对值中的最大值
					
					outputImage[i][j] = max;
				}
			}
			
			System.out.println(column);
			
			int count = 0;
			int oldValue = -1;
			
			//通过输出二位数组输出一对一对的值
			for(int i=0; i<row; i++){
				for(int j=0; j<column; j++){
					if(oldValue == -1){
						//说明这是第一个像素,直接赋值,并计数为1
						oldValue = outputImage[i][j];
						count = 1;
					}else if(oldValue == outputImage[i][j]){
						//说明和上一个数重复,计数加1
						count++;
					}else{
						//说明不和上一个数重复,计数置为1,并打印上一个数的值和次数
						System.out.println(oldValue+" " + count);
						oldValue = outputImage[i][j];
						count = 1;
					}
				}
			}
			
			System.out.println(oldValue+" " + count);
			System.out.println("0 0");
		}
		
		System.out.println("0");
	}
	

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值