1066 图像过滤 java

1066 图像过滤(15 分)

图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来。现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换。

输入格式:

输入在第一行给出一幅图像的分辨率,即两个正整数 M 和 N(0<M,N≤500),另外是待过滤的灰度值区间端点 A 和 B(0≤A<B≤255)、以及指定的替换灰度值。随后 M 行,每行给出 N 个像素点的灰度值,其间以空格分隔。所有灰度值都在 [0, 255] 区间内。

输出格式:

输出按要求过滤后的图像。即输出 M 行,每行 N 个像素灰度值,每个灰度值占 3 位(例如黑色要显示为 000),其间以一个空格分隔。行首尾不得有多余空格。

输入样例:

3 5 100 150 0
3 189 254 101 119
150 233 151 99 100
88 123 149 0 255

输出样例:

003 189 254 000 000
000 233 151 099 000
088 000 000 000 255
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner sc = new Scanner(System.in);
	     int a=sc.nextInt();
	     int b=sc.nextInt();
	     int c=sc.nextInt();
	     int d=sc.nextInt();
	     int e=sc.nextInt();sc.nextLine();
	     int ar[][]=new int [a][b];
	     for(int i=0;i<ar.length;i++) {
	    	 for(int j=0;j<ar[i].length;j++) {
	    		 ar[i][j]=sc.nextInt();
	    	 }
	     }
	     for(int i=0;i<ar.length;i++) {
	    	 for(int j=0;j<ar[i].length-1;j++) {
	    		if(ar[i][j]>=c&&ar[i][j]<=d) {
	    			String str =  new DecimalFormat("000").format(e);	    			
	    			System.out.print(str+" ");
	    		}
	    		else {
	    			String str =  new DecimalFormat("000").format(ar[i][j]);	    			
	    			System.out.print(str+" ");
	    		}
	    	 }
	    	 if(ar[i][ar[i].length-1]>=c&&ar[i][ar[i].length-1]<=d) {
	    		 String str =  new DecimalFormat("000").format(e);	    			
	    			System.out.println(str);
	    	 }else {
	    		 String str =  new DecimalFormat("000").format(ar[i][ar[i].length-1]);	    			
	    			System.out.println(str);
	    	 }
	    	 	    	
	     }	 	     
	}
}

 思路总结:主要就是对二维数组的遍历,还有就是对最后一个元素进行换行操作,  同时还需要对不满足三位的数字进行补 0 ,替换 的数字也要考虑进去.

技术总结

int i=3;

 String str   =  new DecimalFormat("000").format(i);                    
                    System.out.println(str);

通过这种操作就可以把3  变为字符串的003;同理里面的000也可以换成别的数已满足不同的条件

猜你喜欢

转载自blog.csdn.net/qq_41859891/article/details/81814134