合并RGB二值图像的问题

发布于 2024-10-13 04:03:00 字数 5271 浏览 3 评论 0原文

我正在执行边缘检测,它将检测每个 RGB 通道的边缘,然后将它们组合起来以显示为最终输出。我现在在组合这三个图像时遇到问题,因为它没有向我显示二进制图像,而是上面有一些颜色。我已经检查了 RGB 的每个二进制图像,它工作正常,给出了黑白图像。更清楚地说,以下是代码:

private void processActionPerformed(java.awt.event.ActionEvent evt) {                                        
        width = inputimage.getWidth(null);
        height = inputimage.getHeight(null);

        inputbuff = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        Graphics r = inputbuff.getGraphics();
        r.drawImage(inputimage, 0, 0, null);
        r.dispose();

        process_red = new int[width * height];
        process_green = new int[width * height];
        process_blue = new int[width * height];
        process_grey = new int[width * height];
        process_rgb = new int[width * height];
        process_combine = new int[width * height];

        for (int i = 0; i < 256; i++) {
            freq_red[i] = freq_green[i] = freq_blue[i] = freq_grey[i] = 0;
        }

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int clr = inputbuff.getRGB(y, x);

                int red = (clr & 0x00ff0000) >> 16;
                int green = (clr & 0x0000ff00) >> 8;
                int blue = clr & 0x000000ff;
                int grey = (11 * red + 16 * green + 5 * blue) / 32;

                freq_red[red] += 1;
                freq_green[green] += 1;
                freq_blue[blue] += 1;
                freq_grey[grey] += 1;
            }
        }

        int threshold = 150;
        for (int i = 0; i < 256; i++) {
            freq_red[i] = applyThreshold(threshold, freq_red[i]);
            freq_green[i] = applyThreshold(threshold, freq_green[i]);
            freq_blue[i] = applyThreshold(threshold, freq_blue[i]);
            freq_grey[i] = applyThreshold(threshold, freq_grey[i]);
        }

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int clr = inputbuff.getRGB(y, x);

                int red = (clr & 0x00ff0000) >> 16;
                int green = (clr & 0x0000ff00) >> 8;
                int blue = clr & 0x000000ff;
                int grey = (11 * red + 16 * green + 5 * blue) / 32;

                red = freq_red[red];
                green = freq_green[green];
                blue = freq_blue[blue];
                grey = freq_grey[grey];


                int alpha = 0xff000000;
            int combine = alpha | (red <<16) |(green <<8)|blue;

                process_red[x * height + y] = (0xFF<<24)|(red<<16)|(red<<8)|red;
                process_green[x * height + y] = (0xFF<<24)|(green<<16)|(green<<8)|green;
                process_blue[x * height + y] = (0xFF<<24)|(blue<<16)|(blue<<8)|blue;
                process_grey[x * height + y] = (0xFF<<24)|(grey<<16)|(grey<<8)|grey;
                process_rgb[x * height + y] = clr;
                process_combine[x * height + y] = combine;

            }
        }

        image_red = new JFrame().createImage(new MemoryImageSource(width, height, process_red, 0, width));
        image_green = new JFrame().createImage(new MemoryImageSource(width, height, process_green, 0, width));
        image_blue = new JFrame().createImage(new MemoryImageSource(width, height, process_blue, 0, width));
        image_grey = new JFrame().createImage(new MemoryImageSource(width, height, process_grey, 0, width));
        image_rgb = new JFrame().createImage(new MemoryImageSource(width, height, process_rgb, 0, width));
        image_combine = new JFrame().createImage(new MemoryImageSource(width, height, process_combine, 0, width));

        buff_red = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        buff_green = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        buff_blue = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        buff_grey = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        buff_rgb = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        buff_combine = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);

        graph_red = buff_red.getGraphics();
        graph_green = buff_green.getGraphics();
        graph_blue = buff_blue.getGraphics();
        graph_grey = buff_grey.getGraphics();
        graph_rgb = buff_rgb.getGraphics();
        graph_combine = buff_combine.getGraphics();

        graph_red.drawImage(image_red, 0, 0, null);
        graph_green.drawImage(image_green, 0, 0, null);
        graph_blue.drawImage(image_blue, 0, 0, null);
        graph_grey.drawImage(image_grey, 0, 0, null);
        graph_rgb.drawImage(image_rgb, 0, 0, null);
        graph_combine.drawImage(image_combine, 0, 0, null);

        graph_red.dispose();
        graph_green.dispose();
        graph_blue.dispose();
        graph_grey.dispose();
        graph_rgb.dispose();
        graph_combine.dispose();


        repaint();
}   

我怀疑问题出在 alpha 值上:

int alpha = 0xff000000;
int combine = alpha | (red <<16) | (green <<8)|blue;

但是,当我删除 alpha 值时,它不显示任何内容。有人可以帮我吗?提前致谢!

im doing an edge detection which will detect edges of each RGB channel and then combine them to show it as a final output. im now having a problem with combining the three as it doesnt show me a binary image, instead it has some colors on it. i have checked each binary image of the RGB and it works fine which gives the black and white image. to be clearer, following is the code:

private void processActionPerformed(java.awt.event.ActionEvent evt) {                                        
        width = inputimage.getWidth(null);
        height = inputimage.getHeight(null);

        inputbuff = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        Graphics r = inputbuff.getGraphics();
        r.drawImage(inputimage, 0, 0, null);
        r.dispose();

        process_red = new int[width * height];
        process_green = new int[width * height];
        process_blue = new int[width * height];
        process_grey = new int[width * height];
        process_rgb = new int[width * height];
        process_combine = new int[width * height];

        for (int i = 0; i < 256; i++) {
            freq_red[i] = freq_green[i] = freq_blue[i] = freq_grey[i] = 0;
        }

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int clr = inputbuff.getRGB(y, x);

                int red = (clr & 0x00ff0000) >> 16;
                int green = (clr & 0x0000ff00) >> 8;
                int blue = clr & 0x000000ff;
                int grey = (11 * red + 16 * green + 5 * blue) / 32;

                freq_red[red] += 1;
                freq_green[green] += 1;
                freq_blue[blue] += 1;
                freq_grey[grey] += 1;
            }
        }

        int threshold = 150;
        for (int i = 0; i < 256; i++) {
            freq_red[i] = applyThreshold(threshold, freq_red[i]);
            freq_green[i] = applyThreshold(threshold, freq_green[i]);
            freq_blue[i] = applyThreshold(threshold, freq_blue[i]);
            freq_grey[i] = applyThreshold(threshold, freq_grey[i]);
        }

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int clr = inputbuff.getRGB(y, x);

                int red = (clr & 0x00ff0000) >> 16;
                int green = (clr & 0x0000ff00) >> 8;
                int blue = clr & 0x000000ff;
                int grey = (11 * red + 16 * green + 5 * blue) / 32;

                red = freq_red[red];
                green = freq_green[green];
                blue = freq_blue[blue];
                grey = freq_grey[grey];


                int alpha = 0xff000000;
            int combine = alpha | (red <<16) |(green <<8)|blue;

                process_red[x * height + y] = (0xFF<<24)|(red<<16)|(red<<8)|red;
                process_green[x * height + y] = (0xFF<<24)|(green<<16)|(green<<8)|green;
                process_blue[x * height + y] = (0xFF<<24)|(blue<<16)|(blue<<8)|blue;
                process_grey[x * height + y] = (0xFF<<24)|(grey<<16)|(grey<<8)|grey;
                process_rgb[x * height + y] = clr;
                process_combine[x * height + y] = combine;

            }
        }

        image_red = new JFrame().createImage(new MemoryImageSource(width, height, process_red, 0, width));
        image_green = new JFrame().createImage(new MemoryImageSource(width, height, process_green, 0, width));
        image_blue = new JFrame().createImage(new MemoryImageSource(width, height, process_blue, 0, width));
        image_grey = new JFrame().createImage(new MemoryImageSource(width, height, process_grey, 0, width));
        image_rgb = new JFrame().createImage(new MemoryImageSource(width, height, process_rgb, 0, width));
        image_combine = new JFrame().createImage(new MemoryImageSource(width, height, process_combine, 0, width));

        buff_red = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        buff_green = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        buff_blue = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        buff_grey = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        buff_rgb = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        buff_combine = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);

        graph_red = buff_red.getGraphics();
        graph_green = buff_green.getGraphics();
        graph_blue = buff_blue.getGraphics();
        graph_grey = buff_grey.getGraphics();
        graph_rgb = buff_rgb.getGraphics();
        graph_combine = buff_combine.getGraphics();

        graph_red.drawImage(image_red, 0, 0, null);
        graph_green.drawImage(image_green, 0, 0, null);
        graph_blue.drawImage(image_blue, 0, 0, null);
        graph_grey.drawImage(image_grey, 0, 0, null);
        graph_rgb.drawImage(image_rgb, 0, 0, null);
        graph_combine.drawImage(image_combine, 0, 0, null);

        graph_red.dispose();
        graph_green.dispose();
        graph_blue.dispose();
        graph_grey.dispose();
        graph_rgb.dispose();
        graph_combine.dispose();


        repaint();
}   

i suspected that the problem is with the alpha value:

int alpha = 0xff000000;
int combine = alpha | (red <<16) | (green <<8)|blue;

however, when i removed the alpha value it doesnt display anything. can anyone please help me? thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2024-10-20 04:03:00

我猜测 freq_red 等是字节数组。如果是这样,那么您将被字节符号扩展所困扰< /a>.

尝试

red = freq_red[red];
green = freq_green[green];
blue = freq_blue[blue];
grey = freq_grey[grey];

用以下内容替换它:

red = freq_red[red] & 0xFF;
green = freq_green[green] & 0xFF;
blue = freq_blue[blue] & 0xFF;
grey = freq_grey[grey] & 0xFF;

更新:由于所有临时图像(graph_red 等),您的方法比需要的要长。您可以通过定义方法来避免它们像这样:

private BufferedImage wrapPixelArray(int width,
                                     int height,
                                     int[] process) {
    DataBuffer db = new DataBufferInt(process, width * height);
    SampleModel sm =
        new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, width, height, MASK);
    WritableRaster wr =
        Raster.createWritableRaster(sm, db, null);
    return new BufferedImage(RGB, wr, false, null);
}

private static final int[] MASK = {0xFF0000, 0xFF00, 0xFF}; 
private static final ColorModel RGB =
    new DirectColorModel(32, MASK[0], MASK[1], MASK[2]);

I am guessing that freq_red etc. are byte arrays. If so then you are being bitten by byte sign extension.

Try replacing this

red = freq_red[red];
green = freq_green[green];
blue = freq_blue[blue];
grey = freq_grey[grey];

with this:

red = freq_red[red] & 0xFF;
green = freq_green[green] & 0xFF;
blue = freq_blue[blue] & 0xFF;
grey = freq_grey[grey] & 0xFF;

Update: your method is longer than it needs to be because of all the temporary images (graph_red etc.) You can avoid them by defining a method like this:

private BufferedImage wrapPixelArray(int width,
                                     int height,
                                     int[] process) {
    DataBuffer db = new DataBufferInt(process, width * height);
    SampleModel sm =
        new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, width, height, MASK);
    WritableRaster wr =
        Raster.createWritableRaster(sm, db, null);
    return new BufferedImage(RGB, wr, false, null);
}

private static final int[] MASK = {0xFF0000, 0xFF00, 0xFF}; 
private static final ColorModel RGB =
    new DirectColorModel(32, MASK[0], MASK[1], MASK[2]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文