POJ 1009

来源:互联网 发布:seo搜索推广 编辑:程序博客网 时间:2024/06/06 12:25
Edge Detection
Time Limit: 1000MS Memory Limit: 10000KTotal 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 (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

715 4100 1525 2175 225 5175 225 50 01035 500000000200 5000000000 03255 110 1255 210 1255 210 1255 10 00

Sample Output

785 50 285 575 10150 275 30 2150 20 40 0100 499999990165 200 4999999900 03245 90 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;//寻找周边像素值,如果没有则为-1int 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];//计算与周边像素值的绝对值,如果没有则为-1nums[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){//说明这是第一个像素,直接赋值,并计数为1oldValue = outputImage[i][j];count = 1;}else if(oldValue == outputImage[i][j]){//说明和上一个数重复,计数加1count++;}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 0
原创粉丝点击