【问题标题】:Implementing Modified Sketch generation in Java在 Java 中实现修改后的草图生成
【发布时间】:2011-02-08 17:01:15
【问题描述】:

我在最后一年 Engg。我希望为我的最后一年项目进行一些修改来实现草图生成算法。为此我希望访问和修改像素强度值(不是根据 RGB,而是根据Matlab 中显示的数字)。虽然该算法在 matlab 中运行良好,但由于项目的要求,我打算用 Java 来做。浏览互联网和各种 Java 论坛对我没有帮助。
Matlab 代码允许我使用以下 sn-ps
访问像素 120,234 处的像素强度值由“A(120,234)”给出,其中 A 是所考虑图像的名称。
同样,我想在 Java 中访问图像的像素强度值并使用算法对其进行修改。
如果有人帮助我,我会很高兴。
提前致谢

【问题讨论】:

    标签: java image matlab


    【解决方案1】:

    由于您可以访问 matlab,我建议您深入研究他们的代码,假设他们的图像内容是用 Matlab 编写的,我认为是这样,然后看看他们如何将 RGB 转换为强度。他们是否使用 HSL(色相-饱和度-亮度)?或其他一些颜色转换。知道了这一点,您就可以找到 Java 代码来将例如 RGB 转换为 HSL。

    编辑:
    根据这个问题的 cmets,我认为这段代码会起作用。它不完整,因为我没有复制和重写所有操作,但它应该给你的想法。

        import java.awt.image.BufferedImage;
    import java.io.File;
    
    import javax.imageio.ImageIO;
    
    public class Convolution {
        public static void main( String[] args ) throws Exception {
            File inputFile = new File("apple.jpg");
            BufferedImage bufferedImage = ImageIO.read(inputFile);
            int w = bufferedImage.getWidth();
            int h = bufferedImage.getHeight();
    
            System.out.println("w=" + w + ", h=" + h);
    
            // Get Pixels
            int[] image = new int[w * h];
            bufferedImage.getRGB(0, 0, w, h, image, 0, w);
    
            // Convert to simple grayscale
            for ( int y = 0; y < h; y++ ) {
                for ( int x = 0; x < w; x++ ) {
                    int idx = ( y * w ) + x;
                    int p = image[idx];
                    int r = p & 0x00FF0000 >> 16;
                    int g = p & 0x0000FF >> 8;
                    int b = p & 0x000000FF;
                    image[idx] = (int) ( ( r + g + b ) / 3.0 );
                }
            }
    
            int convolutionSize = 3;
            int[][] convolution = { { 0, -1, 0 }, { -1, 4, -1 }, { 0, -1, 0 } };
    
            int[] newImage = new int[w * h];
            // Apply the convolution to the whole image, note that we start at
            // 1 instead 0 zero to avoid out-of-bounds access
            for ( int y = 1; y + 1 < h; y++ ) {
                for ( int x = 1; x + 1 < w; x++ ) {
                    int idx = ( y * w ) + x;
    
                    // Apply the convolution
                    for ( int cy = 0; cy < convolutionSize; cy++ ) {
                        for ( int cx = 0; cx < convolutionSize; cx++ ) {
                            int cIdx = ( ( ( y - 1 ) + cy ) * w )
                                    + ( ( x - 1 ) + cx );
                            newImage[idx] += convolution[cy][cx] * image[cIdx];
                        }
                    }
    
                    // pixel value rounding
                    if ( newImage[idx] < 0 ) {
                        newImage[idx] = -newImage[idx];
                    } else {
                        newImage[idx] = 0;
                    }
                    if ( newImage[idx] > 0 ) {
                        newImage[idx] = 120 - newImage[idx];
                    } else {
                        newImage[idx] = 255;
                    }
    
                }
            }
    
            // Convert to "proper" grayscale
            for ( int y = 0; y < h; y++ ) {
                for ( int x = 0; x < w; x++ ) {
                    int idx = ( y * w ) + x;
                    int p = newImage[idx];
                    newImage[idx] = 0xFF000000 | ( p << 16 ) | ( p << 8 ) | p;
                }
            }
    
            // Set the image to have the new values;
            bufferedImage.setRGB(0, 0, w, h, newImage, 0, w);
    
            // Write the new image as a PNG to avoid lossey compression,
            // and its eaiser than trying to display an image in Java.
            ImageIO.write(bufferedImage, "png", new File("new_apple.png"));
        }
    }
    

    编辑:
    修改代码以按预期工作。它不快,但它有效。
    之前:

    之后:

    【讨论】:

    • 感谢您的建议。我按照您提到的那样做了,但似乎没有适当提及 Matlabs 获取强度值的方法。在 java 中,我得到的值是 6 到 8 位数字,也是负数。我尝试了 java 中的各种类,如 Raster 和 Color 类,但它们不够有用。
    • 这是彩色还是灰度图像?它是什么格式,JPEG,GIF,...?您能否提供用于在 matlab 中加载图像的代码。我假设你正在做A = imread(filename),然后直接操作矩阵A。这是正确的吗?
    • 我将使用的图像可以是两者。(颜色和灰度)。格式可以是JPEG或TIFF。是的,正如你所说,我会做上述操作。Matlab代码如下。 pastebin.com/zLdXaUPu。感谢您的关注。
    • 看起来当您在 MatLab 中使用imread 读取图像时,它只是返回代表像素的位的值。它不进行任何颜色转换。所以A(i,j) 返回该像素的 R、G、B 通道。用一些我认为可行的代码查看我修改后的问题。
    【解决方案2】:

    在我看来,您想要使用的是bufferedImage,您可以从中获取writableRaster,您可以使用它来读取/写入像素值。

    另请参阅this tutorial,了解与缓冲图像交互的一些方法。

    【讨论】:

    • 我尝试使用 bufferedImages 和 Raster 类。但它们在项目范围内不够有用。感谢您的帮助 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2018-12-14
    • 2015-03-28
    • 2017-11-26
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    相关资源
    最近更新 更多