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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String st[] = in.readLine().split(" ");
int M= Integer.parseInt(st[0]);
int N = Integer.parseInt(st[1]);
int A = Integer.parseInt(st[2]);
int B = Integer.parseInt(st[3]);
String instead = st[4];
//格式化替代的值
if(Integer.parseInt(instead)>=0 && Integer.parseInt(instead)<=9){
instead="00"+instead;
}
if(Integer.parseInt(instead)>=10 && Integer.parseInt(instead)<=99){
instead="0"+instead;
}
String arr[]= new String[M];
for(int i =0;i<M;i++){
arr[i]= in.readLine();
String strings[]= arr[i].split(" ");
//改变strings[j]的值,然后再进行拼接
for (int j=0;j<N;j++){
if(Integer.parseInt(strings[j])>=A && Integer.parseInt(strings[j]) <=B){
strings[j] = instead;
continue;
}
if(Integer.parseInt(strings[j])>=0 && Integer.parseInt(strings[j])<=9){
strings[j]="00"+strings[j];
continue;
}
if(Integer.parseInt(strings[j])>=10 && Integer.parseInt(strings[j])<=99){
strings[j]="0"+strings[j];
continue;
}
}
arr[i] =strings[0];
for(int j =1;j<N;j++){
arr[i]+=" "+strings[j];
}
}
for(int i=0;i<M;i++){
System.out.println(arr[i]);
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String st[] = in.readLine().split(" ");
// int M= Integer.parseInt(st[0]);
// int N = Integer.parseInt(st[1]);
// int A = Integer.parseInt(st[2]);
// int B = Integer.parseInt(st[3]);
//String instead = st[4];
//格式化替代的值
if(Integer.parseInt(st[4])>=0 && Integer.parseInt(st[4])<=9){
st[4]="00"+st[4];
}
if(Integer.parseInt(st[4])>=10 && Integer.parseInt(st[4])<=99){
st[4]="0"+st[4];
}
String arr[]= new String[Integer.parseInt(st[0])];
for(int i =0;i<Integer.parseInt(st[0]);i++){
arr[i]= in.readLine();
String strings[]= arr[i].split(" ");
//改变strings[j]的值,然后再进行拼接
for (int j=0;j<Integer.parseInt(st[1]);j++){
if(Integer.parseInt(strings[j])>=Integer.parseInt(st[2]) && Integer.parseInt(strings[j]) <=Integer.parseInt(st[3])){
strings[j] = st[4];
continue;
}
if(Integer.parseInt(strings[j])>=0 && Integer.parseInt(strings[j])<=9){
strings[j]="00"+strings[j];
continue;
}
if(Integer.parseInt(strings[j])>=10 && Integer.parseInt(strings[j])<=99){
strings[j]="0"+strings[j];
continue;
}
}
arr[i] =strings[0];
for(int j =1;j<Integer.parseInt(st[1]);j++){
arr[i]+=" "+strings[j];
}
}
for(int i=0;i<Integer.parseInt(st[0]);i++){
System.out.println(arr[i]);
}
}
}
package PTA;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String st[] = in.readLine().split(" “);
//格式化替代的值
if(Integer.parseInt(st[4])>=0 && Integer.parseInt(st[4])<=9){
st[4]=“00”+st[4];
}
else if(Integer.parseInt(st[4])>=10 && Integer.parseInt(st[4])<=99){
st[4]=“0”+st[4];
}
for(int i =0;i<Integer.parseInt(st[0]);i++){
String strings[]= in.readLine().split(” “);
//改变strings[j]的值,然后再进行拼接
for (int j=0;j<Integer.parseInt(st[1]);j++){
if(Integer.parseInt(strings[j])>=Integer.parseInt(st[2]) && Integer.parseInt(strings[j]) <=Integer.parseInt(st[3])){
strings[j] = st[4];
continue;
}
if(Integer.parseInt(strings[j])>=0 && Integer.parseInt(strings[j])<=9){
strings[j]=“00”+strings[j];
continue;
}
if(Integer.parseInt(strings[j])>=10 && Integer.parseInt(strings[j])<=99){
strings[j]=“0”+strings[j];
continue;
}
}
StringBuffer sb = new StringBuffer();
sb.append(strings[0]);
for(int j =1;j<Integer.parseInt(st[1]);j++){
sb.append(” "+strings[j]);
}
System.out.println(sb);
}
}
}