java - 在 Java 中实现修改草图生成

标签 java image matlab

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

最佳答案

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

编辑:
根据这个问题的评论,我认为这段代码可以工作。它并不完整,因为我没有复制和重写所有操作,但它应该给你一个想法。

    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"));
    }
}

编辑:
修改代码以按预期工作。虽然速度不快,但很有效。
之前:
Before

之后:
After

关于java - 在 Java 中实现修改草图生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4935955/

相关文章:

java - Android VideoView 创建过多的信息/警告消息

jquery - 向 Ghost 博客添加图像样式的简单方法

java - 在 Matlab 中使用 java BufferedRead 和 TCP/IP 以太网连接 - 访问最新数据

windows - 如何隐藏随我的应用程序一起打开的控制台窗口?

java - 通过创建不同的对象来使用相同的 AsyncTask

java - 使用 throws 将异常传播到被调用的方法

Android YuvImage 类格式不正确?

java - 使用 Java/JaCoB 让 32 位 COM 客户端与进程外 64 位服务器通信

Java方法不返回链表的第一个值

javascript - 图像上的隐形边框