用Java实现修改后的草图生成

用Java实现修改后的草图生成,java,image,matlab,Java,Image,Matlab,我正在上最后一年的英语课。我希望在最后一年的项目中实现略作修改的草图生成算法。为此,我希望访问和修改像素强度值(不是以RGB为单位,而是以Matlab中显示的数字为单位)。尽管算法在Matlab中运行良好,但是由于项目的需要,我打算用Java来做。浏览互联网和各种Java论坛对我没有帮助。 Matlab代码允许我使用以下代码片段访问像素 120234处的像素强度值由“A(120234)”给出,其中A是正在考虑的图像的名称。 类似地,我想用Java访问图像的像素强度值,并用算法修改它们。 如果有人

我正在上最后一年的英语课。我希望在最后一年的项目中实现略作修改的草图生成算法。为此,我希望访问和修改像素强度值(不是以RGB为单位,而是以Matlab中显示的数字为单位)。尽管算法在Matlab中运行良好,但是由于项目的需要,我打算用Java来做。浏览互联网和各种Java论坛对我没有帮助。
Matlab代码允许我使用以下代码片段访问像素
120234处的像素强度值由“A(120234)”给出,其中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"));
    }
}
导入java.awt.image.buffereImage;
导入java.io.File;
导入javax.imageio.imageio;
公共类卷积{
公共静态void main(字符串[]args)引发异常{
File inputFile=新文件(“apple.jpg”);
BuffereImage BuffereImage=ImageIO.read(inputFile);
int w=bufferedImage.getWidth();
int h=bufferedImage.getHeight();
System.out.println(“w=“+w+”,h=“+h”);
//获取像素
int[]image=新的int[w*h];
getRGB(0,0,w,h,image,0,w);
//转换为简单灰度
对于(int y=0;y>16;
INTG=p&0x0000FF>>8;
int b=p&0x000000FF;
图像[idx]=(int)((r+g+b)/3.0);
}
}
int卷积=3;
int[][]卷积={{0,-1,0},{-1,4,-1},{0,-1,0};
int[]newImage=newint[w*h];
//将卷积应用于整个图像,注意我们从
//1而不是0 0以避免越界访问
对于(int y=1;y+10){
newImage[idx]=120-newImage[idx];
}否则{
newImage[idx]=255;
}
}
}
//转换为“适当”灰度
对于(int y=0;ynewImage[idx]=0xFF000000|(p在我看来,您想要使用的是a,从中可以得到a,您可以使用它来读取/写入像素值


另请参阅与BuffereImage交互的几种方法。

一些代码:谢谢你的建议。我是按照你提到的那样做的,但似乎没有适当提到Matlabs获取强度值的方法。在java中,我得到的值是6到8位数字,也是负数。我在java中尝试了各种类,如Raster和Color类,但它们不够有用。这是彩色图像还是灰度图像?它的格式是什么,JPEG,GIF,…?能否给出在matlab中加载图像所用的代码。我假设您正在执行
a=imread(文件名)
然后直接操作矩阵
A
。这是正确的吗?我将使用的图像可以是两者。(颜色和灰度)。格式可以是JPEG或TIFF。是的,正如您所提到的,我将执行上述操作。Matlab代码如下..感谢您的关注。在Matlab中,当您使用
imread
读取图像时,它看起来只是返回代表像素的位值。它不进行任何颜色转换。因此
A(i,j)
返回该像素的R、G、B通道。请参阅我修改过的问题,并附上一些我认为可行的代码。我尝试使用BuffereImage和Raster类。但它们在项目范围内不够有用。不过,感谢您的帮助:)