📜  Java DIP-图像像素

📅  最后修改于: 2020-12-14 05:36:02             🧑  作者: Mango


图像包含像素的二维阵列。实际上,就是构成图像的那些像素的值。通常,图像可以是彩色或灰度。

在Java中,BufferedImage类用于处理图像。您需要调用BufferedImage类的getRGB()方法来获取像素值。

获得像素价值

可以使用以下语法接收像素值-

Color c = new Color(image.getRGB(j, i));

获取RGB值

方法getRGB()以行和列的索引为参数,并返回适当的像素。如果是彩色图像,它将返回三个值(红色,绿色,蓝色)。他们可以得到如下-

c.getRed();
c.getGreen();
c.getBlue();

获取图像的宽度和高度

可以通过调用BufferedImage类的getWidth()getHeight()方法来获取图像的高度和宽度。其语法如下-

int width = image.getWidth();
int height = image.getHeight();

除了这些方法外,BufferedImage类还支持其他方法。他们简要描述-

Sr.No. Method & Description
1

copyData(WritableRaster outRaster)

It computes an arbitrary rectangular region of the BufferedImage and copies it into a specified WritableRaster.

2

getColorModel()

It returns ColorModel of an image.

3

getData()

It returns the image as one large tile.

4

getData(Rectangle rect)

It computes and returns an arbitrary region of the BufferedImage.

5

getGraphics()

This method returns a Graphics2D, but is here for backwards compatibility.

6

getHeight()

It returns the height of the BufferedImage.

7

getMinX()

It returns the minimum x coordinate of this BufferedImage.

8

getMinY()

It returns the minimum y coordinate of this BufferedImage.

9

getRGB(int x, int y)

It returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.

10

getType()

It returns the image type.

以下示例演示了Java BufferedImage类的使用,该类显示大小为(100 x 100)的图像像素

import java.awt.*;
import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

class Pixel {
   BufferedImage image;
   int width;
   int height;
   
   public Pixel() {
      try {
         File input = new File("blackandwhite.jpg");
         image = ImageIO.read(input);
         width = image.getWidth();
         height = image.getHeight();
         
         int count = 0;
         
         for(int i=0; i

输出

当您执行以上示例时,它将打印以下图像的像素-

原始图片

了解图像像素教程

像素输出

了解图像像素教程

如果向下滚动输出,将看到以下模式:

了解图像像素教程