Java/Swing无法从图像中获取RGB

Java/Swing无法从图像中获取RGB,java,swing,Java,Swing,我在其他任何地方都没有看到这个问题,所以我来这里寻求帮助。我正在制作一个简单的程序来改变黑白图像的对比度,几乎所有的事情都完成了。但是,我有一个使用getRGB方法的问题。对于图像的前半部分(大致上),它返回150、150、150左右的正常值或图像中的任何值。然后由于一个我无法解释的原因,它会返回0,0,0一段时间,然后返回255,255,255,混合了两个0。有人知道为什么会这样吗 private void getRGB(BufferedImage image, int x, int y){

我在其他任何地方都没有看到这个问题,所以我来这里寻求帮助。我正在制作一个简单的程序来改变黑白图像的对比度,几乎所有的事情都完成了。但是,我有一个使用
getRGB
方法的问题。对于图像的前半部分(大致上),它返回150、150、150左右的正常值或图像中的任何值。然后由于一个我无法解释的原因,它会返回0,0,0一段时间,然后返回255,255,255,混合了两个0。有人知道为什么会这样吗

private void getRGB(BufferedImage image, int x, int y){
        //get pixel rgb from image
        int temp=image.getRGB(x,y);
        //set the value to color and then get each component
        Color color=new Color(temp);
        rgb[0]=color.getRed();
        rgb[1]=color.getGreen();
        rgb[2]=color.getBlue();
        //debug file
        data.println(color.getRed()+"\t"+color.getGreen()+"\t"+color.getBlue());       
    }
下面是data.txt文件的一个片段。这里有比我在这里显示的更多的零

189 189 189
184 184 184
178 178 178
181 181 181
171 172 171
196 196 195
182 182 182
175 175 175
186 186 186
181 181 181
192 192 191
183 183 183
184 184 184
186 186 186
188 188 188
183 183 183
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
255 255 255
255 255 255
255 255 255
255 255 255
255 255 255
255 255 255
255 255 255
这里是我计算每个像素的新灰度的函数。据我所知,这是正常运作

    private void equalize(int x, int y, int max, int min){

            //This calls getRGB then calculates the old and new grey levels for a pixel
            //It then will assign the new grey level to the pixel
            getRGB(image1, x, y);
            int oldGrey = ((rgb[0]+rgb[1]+rgb[2])/3);
            int newGrey = (int)Math.round(
                    (((cdf[oldGrey]*1.0)-minCdfValue)/
                        ((x*y)-minCdfValue))*
                            (newMax-newMin)+newMin);

            if(oldGrey != 0){
                for(int i=0; i<3; i++){

                    rgb[i] *= ((newGrey*1.0)/(oldGrey*1.0));
                }
            }else{
                rgb[0]=rgb[1]=rgb[2]=newGrey;
            }
            setRGB(image1, x, y);

        }
private void均衡器(整数x,整数y,整数max,整数min){
//这将调用getRGB,然后计算像素的新旧灰度
//然后,它将为像素指定新的灰度
getRGB(图像1,x,y);
int oldGrey=((rgb[0]+rgb[1]+rgb[2])/3);
int newGrey=(int)Math.round(
((cdf[oldGrey]*1.0-最小cdf值)/
((x*y)-最小CDFvalue))*
(newMax-newMin)+newMin);
如果(oldGrey!=0){

对于(int i=0;i什么是cdf?和minCDFvalue是否有可能超出图像边界?有些事情困扰着我:您谈论的是黑白图像和灰度级别,而您使用的是RGB。您应该使用更合适的颜色space@G你为什么这么说?还有,为什么255和0是非法值?这只是意味着你有一些黑色像素和一些白色像素。在黑白图像中没有什么令人震惊的