如何使用Eclipse查找图像中的像素总数和图像中心? [英] How Do I Find The Total Number Of Pixels In An Image And The Center Of An Image Using Eclipse?
问题描述
我已经这样做了几天,我似乎无法理解如何找到像素总数。此外,我需要找到图像的中心,这只是坐标的平均值,但每次我放入其中的东西都无法解决。这里是代码到目前为止:
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io. *;
import java.net。*;
public class Quiz {
static int width,高度;
public static void main(String [] args){
try {
BufferedImage image = null;
URL url = new URL(http://imc.kean.edu/cps2231/program6.png);
image = ImageIO.read(url);
height = image.getHeight();
width = image.getWidth();
System.out.println(width:+ width +height: +高度);
int [] [] img = new int [height] [width];
for(int y = 0; y< height; y ++)>
for(int x = 0; x< width; x ++)>
img [ y] [x] = image.getData()。getSample(x,y,0);
printImg(img);
int color = 2;
int cX = 0;
int cY = 0;
int pixels = 0;
for(int Y = 0; Y<高度; Y ++)> for(int x = 0; x< width; x ++){>
if(img [y] [x] == 0){
label(img, x,y,color);
System.out.println(颜色:+颜色+对象大小:+像素+像素,+居中于+(+ cX +,+ cY +));
color ++;
}
}
}
System.out.println(黑色物体总数:+(颜色-2));
printImg(img);
} catch(IOException e){e.printStackTrace(); }
}
static void label(int [] [ ] A,int x,int y,int color){
if((x> = 0)&&(y> = 0)&&(x< width)> System.out.printf( (%d,%d)颜色:%d \ n,x,y,颜色);
A [y] [x] =颜色;
标签(A,x,y-1,颜色);
标签(A,x-1,y,颜色);
标签(A,x,y + 1,颜色);
标签(A,x + 1,y,颜色);
}
}
static void printImg(int [] [] A){
for(int y = 0; y< height; y ++)> for(int x = 0; x< width; x ++)>
System.out.printf(%d,A [y] [x]);
System.out。 println();
< br $> b $ b
}
}
}
I have been doing this for several days and I cant seem to understand how to find the total number of pixels. Also I need to find the center of the image as well which is just the average of the coordinates but everytime I put something in it doesn't work out. here is code so far:
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
public class Quiz {
static int width,height;
public static void main( String[] args ){
try {
BufferedImage image = null;
URL url=new URL("http://imc.kean.edu/cps2231/program6.png");
image = ImageIO.read(url);
height=image.getHeight();
width=image.getWidth();
System.out.println("width:"+width+ " height:"+height);
int [][] img=new int[height][width];
for (int y=0;y<height;y++)>
for (int x=0;x<width;x++)>
img[y][x]= image.getData().getSample(x, y, 0);
printImg(img);
int color=2;
int cX=0;
int cY=0;
int pixels=0;
for (int y=0;y<height;y++)> for (int x=0;x<width;x++){>
if (img[y][x] ==0){
label(img, x, y, color);
System.out.println("Color: " + color + " Object size: " + pixels + " pixels," + " Center at "+"("+cX+","+cY+")");
color++;
}
}
}
System.out.println("Total number of black objects:"+ (color-2));
printImg(img);
} catch (IOException e) { e.printStackTrace(); }
}
static void label(int[][] A, int x, int y, int color) {
if ((x>=0) && (y>=0) && (x<width)> System.out.printf("(%d,%d) color:%d\n",x,y,color);
A[y][x]=color;
label(A, x, y-1, color);
label(A, x-1,y, color);
label(A, x, y+1, color);
label(A, x+1, y, color);
}
}
static void printImg(int[][] A) {
for (int y=0;y<height;y++)> for (int x=0;x<width;x++)>
System.out.printf("%d",A[y][x]);
System.out.println();
}
}
}
推荐答案
您应该查看图像的属性以找出宽度和高度。
You should view the properties of the image to find out the width and height.
这篇关于如何使用Eclipse查找图像中的像素总数和图像中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!