如何使用Eclipse查找图像中的像素总数和图像中心?

编程入门 行业动态 更新时间:2024-10-27 10:18:54
本文介绍了如何使用Eclipse查找图像中的像素总数和图像中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经这样做了几天,我似乎无法理解如何找到像素总数。此外,我需要找到图像的中心,这只是坐标的平均值,但每次我放入其中的东西都无法解决。这里是代码到目前为止: import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io. *; import java。*; public class Quiz { static int width,高度; public static void main(String [] args){ try { BufferedImage image = null; URL url = new URL(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(); 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.*; public class Quiz { static int width,height; public static void main( String[] args ){ try { BufferedImage image = null; URL url=new URL("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查找图像中的像素总数和图像中心?

本文发布于:2023-06-08 21:23:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/589300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   如何使用   像素   总数   中心

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!