Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据像素的邻域为其指定颜色_Java_Image Processing_Arraylist_Pixels - Fatal编程技术网

Java 根据像素的邻域为其指定颜色

Java 根据像素的邻域为其指定颜色,java,image-processing,arraylist,pixels,Java,Image Processing,Arraylist,Pixels,我将图像中的所有黑色像素放在一个数组中,我希望它们得到左邻域的颜色。我运行代码时没有出现错误,但结果并不是我所期望的。 那些黑色条纹是从哪里来的?我原以为它全是红色的 这是我的代码和结果 import java.awt.Color; import java.awt.image.BufferedImage; import java.util.*; public class ImageTest { public static BufferedImage Threshold(Buffere

我将图像中的所有黑色像素放在一个数组中,我希望它们得到左邻域的颜色。我运行代码时没有出现错误,但结果并不是我所期望的。 那些黑色条纹是从哪里来的?我原以为它全是红色的

这是我的代码和结果

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


public class ImageTest {
    public static BufferedImage Threshold(BufferedImage img) {

        int height = img.getHeight();
        int width = img.getWidth();
        BufferedImage finalImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        int r = 0;
        int g = 0;
        int b = 0;
        List<Integer> blackpixels = new ArrayList<Integer>();



        for (int x = 0; x < width; x++) {
            try {

                for (int y = 0; y < height; y++) {

                    //Get RGB values of pixels
                    int rgb = img.getRGB(x, y); 
                    r = ImageTest.getRed(rgb);
                    g = ImageTest.getGreen(rgb);
                    b = ImageTest.getBlue(rgb);

                    int leftLoc = (x-1) + y*width;

                    if ((r < 5) && (g < 5) && (b < 5)) {
                        blackpixels.add(rgb);
                        Integer[] simpleArray = new Integer[ blackpixels.size() ];
                        System.out.print(simpleArray.length);

                        int pix = 0;

                        while(pix < simpleArray.length) {
                            r = leftLoc;
                            pix = pix +1;   
                        }


                    }

                    finalImage.setRGB(x,y,ImageTest.mixColor(r, g,b));  
                }

                }
            catch (Exception e) {
                    e.getMessage();
            }
    }
    return finalImage;

    }
    private static int mixColor(int red, int g, int b) {
        return red<<16|g<<8|b;
    }

    public static int getRed(int rgb) {
        return (rgb & 0x00ff0000)  >> 16;
    }

    public static int getGreen(int rgb) {
        return  (rgb & 0x0000ff00)  >> 8;
    }

    public static int getBlue(int rgb) {
        return (rgb & 0x000000ff)  >> 0;

    }
}
导入java.awt.Color;
导入java.awt.image.buffereImage;
导入java.util.*;
公共类图像测试{
公共静态BuffereImage阈值(BuffereImage img){
int height=img.getHeight();
int width=img.getWidth();
BuffereImage finalImage=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_RGB);
int r=0;
int g=0;
int b=0;
List blackpixels=new ArrayList();
对于(int x=0;x>0;
}
}
以下方法可能有效。 主要的变化是,它首先收集所有暗像素的位置,然后遍历它们,从它们的左邻域分配颜色

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

public class BlackRedImage
{
    public static BufferedImage Threshold( BufferedImage img )
    {
        int height = img.getHeight();
        int width = img.getWidth();
        BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        List<Integer> blackpixels = new ArrayList<Integer>();

        for ( int x = 0; x < width; x++ )
        {
            for ( int y = 0; y < height; y++ )
            {
                int rgb = img.getRGB(x, y); // Get the pixel in question
                int r = BlackRedImage.getRed(rgb);
                int g = BlackRedImage.getGreen(rgb);
                int b = BlackRedImage.getBlue(rgb);

                if ( (r < 5) && (g < 5) && (b < 5) )
                { // record location of any "black" pixels found
                    blackpixels.add(x + (y * width));
                }

                finalImage.setRGB(x, y, rgb);
            }
        }

        // Now loop through all "black" pixels, setting them to the colour found to their left
        for ( int blackPixelLocation: blackpixels )
        {
            if ( blackPixelLocation % width == 0 )
            { // these pixels are on the left most edge, therefore they do not have a left neighbour!
                continue;
            }

            int y = blackPixelLocation / width;
            int x = blackPixelLocation - (width * y);

            int rgb = img.getRGB(x - 1, y); // Get the pixel to the left of the "black" pixel
            System.out.println("x = " + x + ", y = " + y + ", rgb = " + rgb);
            finalImage.setRGB(x, y, rgb);
        }
        return finalImage;
    }

    private static int mixColor( int red, int g, int b )
    {
        return red << 16 | g << 8 | b;
    }

    public static int getRed( int rgb )
    {
        return (rgb & 0x00ff0000) >> 16;
    }

    public static int getGreen( int rgb )
    {
        return (rgb & 0x0000ff00) >> 8;
    }

    public static int getBlue( int rgb )
    {
        return (rgb & 0x000000ff) >> 0;
    }
}
导入java.awt.image.buffereImage;
导入java.util.*;
公共类BlackRedImage
{
公共静态BuffereImage阈值(BuffereImage img)
{
int height=img.getHeight();
int width=img.getWidth();
BuffereImage finalImage=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_ARGB);
List blackpixels=new ArrayList();
对于(int x=0;x>8;
}
公共静态int getBlue(int rgb)
{
返回(rgb&0x000000ff)>>0;
}
}
编辑:这是一个更简单的版本(不收集黑色像素)

导入java.awt.Color;
导入java.awt.image.buffereImage;
导入java.util.*;
公开课
{
公共静态BuffereImage阈值(BuffereImage img)
{
int width=img.getWidth();
int height=img.getHeight();
BuffereImage finalImage=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_ARGB);
对于(int x=1;x
您有一个while循环正在更改
r
值,这可能是条带的原因吗?(从黑色变为红色)是的,当然可以,但我不知道如何让它按我想要的方式工作。你到底想做什么?预期的结果是什么?您能提供所需的输出图像吗?我正在尝试重建图像,说黑色像素缺少部分,所以用相邻颜色替换它们。我想要的输出图像应该是完全红色的。我在公共关系方面做到了这一点
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.*;

public class ColourMove
{
    public static BufferedImage Threshold( BufferedImage img )
    {
        int width = img.getWidth();
        int height = img.getHeight();
        BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        for ( int x = 1; x < width; x++ ) // Start at 1 as the left most edge doesn't have a left neighbour
        {
            for ( int y = 0; y < height; y++ )
            {
                Color colour = new Color(img.getRGB(x, y));
                int red = colour.getRed();
                int green = colour.getGreen();
                int blue = colour.getBlue();

                if ( (red < 5) && (green < 5) && (blue < 5) )
                { // Encountered a "black" pixel, now replace it with it's left neighbour
                    finalImage.setRGB(x, y, img.getRGB(x - 1, y));
                }
                else
                { // Non-black pixel
                    finalImage.setRGB(x, y, colour.getRGB());
                }
            }
        }
        return finalImage;
    }
}