在Java中实现修改的Sketch生成 [英] Implementing Modified Sketch generation in Java

查看:251
本文介绍了在Java中实现修改的Sketch生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最后一年Engg.i希望通过对我的最后一年项目进行一些修改来实现草图生成算法。为此我希望访问和修改像素强度值(不是以RGB为单位,而是在matlab中提供的数值。虽然算法在matlab中运行良好,但由于项目的要求,我打算用Java做。通过互联网和各种Java论坛没有帮助我。

Matlab代码允许我使用以下片段访问像素

像素强度值120,234由A(120,234)给出)其中A是正在考虑的图像的名称。

同样,我想访问Java中图像的像素强度值,并使用算法修改它们。

i will如果有人帮助我,我会很高兴。

提前致谢

i am in my final year Engg.i wish to implement sketch generation algorithm with a little modification for my final year project.For this i wish to access and modify the pixel intensity values(not in terms of RGB but in terms in numerical number as presented in Matlab).Though the algorithm works fine in matlab,but due to the requirement of the project,i intend to do it in Java. Going through the internet and various Java forums hasnt helped me.
The Matlab code allows me to access pixel using following snippets
The pixel intensity value at 120,234 is given by "A(120,234)" where A is the name of the image under consideration.
Similarly i would like to access the pixel intensity values of the image in Java and modify them with the algorithm.
i would be very glad if someone helps me out.
Thanks in advance

推荐答案

因为您可以访问matlab我建议深入研究他们的代码,假设他们的图像是用Matlab编写的,我认为是这样,看看他们如何将RGB转换成强度。他们使用HSL(色调 - 饱和度 - 亮度)吗?或者其他一些颜色转换。知道了,您可以找到将代码转换为HSL的Java代码。

Since you have access to matlab, I'd suggest digging into their code, assuming their image stuff is written in Matlab, which I think it is, and see how they convert RGB into intensity. Are they using HSL (Hue-Saturation-Luminance)? Or some other color conversion. Knowing that, you can find Java code to convert, for instance, RGB into HSL.

编辑:

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


As per the comments on this question, I think this code will work. Its not complete as I didn't copy and re-write all the manipulation, but it should give you the idea.

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

编辑

修改代码以按预期工作。它不快,但它的工作原理。

之前:


Modified the code to work as expected. Its not fast, but it works.
Before:

之后:

After:

这篇关于在Java中实现修改的Sketch生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆