0

我正在尝试计算灰度图像的区域,我知道如果它是缓冲图像,我可以使用 getRGB(),但我使用的是工具包,因此它是一个 int 图像。我只想问我如何获得像素值?我在下面包含了我的代码

import iptoolkit.*;

public class FindArea {

public static void main(String[] args) {

    String imageDir = "C:/Users/John/Dropbox/finalYear/Project/Leaves/";
    MainWindow mw = new MainWindow();
    int area = 0;

    IntImage src = new IntImage(imageDir + "bg7.jpg", 256, 256);
    src.displayImage(); //displays the image in a window

    for (int row = 0; row <= src.getRows(); row++)
    {
        for (int col=0; col <= src.getCols(); col++)
        {
            //if(src.pixels[row][col] >= 0)
                area++;
        }
    }
    System.out.print("The area of the leaf is:" +area);     
}
4

2 回答 2

0

我相信要记住 RGB 值中的位是这样排序的:8 位 R | 8 位 G | B 的 8 位但这也取决于您使用的图像类型。使用一些位运算符,如 shift << 和 >>,并使用和运算符 & 屏蔽值。

于 2013-10-29T17:33:14.470 回答
0
int  pixel = src.pixels[row][col];
int  red = (pixel & 0x00ff0000) >> 16;
int  green = (pixel & 0x0000ff00) >> 8;
int  blue = pixel & 0x000000ff;
// and the Java Color is ...
Color color = new Color(red,green,blue);

BufferedImage基于,但原理相同。

于 2013-10-29T17:38:01.623 回答