首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弗洛伊德-斯坦伯格抖动程序错误。java

弗洛伊德-斯坦伯格抖动程序错误。java
EN

Stack Overflow用户
提问于 2022-06-20 05:54:19
回答 1查看 43关注 0票数 -1

嘿,我试图创建一个程序,它使用的抖动算法生成一个抖动版本的图像。

我的程序代码如下。

但我相信,在"calculateErr“乘法的舍入和除法中,我遇到了一个错误。我用来测试的图像是这个猫--一个:Cat图像

出于某种原因,它最终看起来像这个抖动猫像

对于我所做的错事,任何解决办法都将不胜感激。

代码语言:javascript
复制
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Dithering extends Canvas {
    public BufferedImage ditheredIMG = new BufferedImage(481,480,
            BufferedImage.TYPE_BYTE_GRAY);

public void paint(Graphics g) {
    BufferedImage i = null;
    try {
        i = displayImage(g);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    getPixels(g, i);
    g.drawImage(ditheredIMG,480,0,this);



}
public static void main(String[] args) {
    Dithering d = new Dithering();
    JFrame f =new JFrame();
    f.add(d);
    f.setSize( 960,481);
    f.setVisible(true);
}


public BufferedImage displayImage(Graphics g) throws IOException {
    final File file = new File("Cat.jpg");
    final BufferedImage i = ImageIO.read(file);
    g.drawImage(i, 0,0,this);
    return i;
}

public void getPixels(Graphics g, BufferedImage i) {
    for (int y = 1; y < i.getHeight()-1; y++){
        for (int x = 1; x < i.getWidth()-1; x++) {
            int pixelValue = i.getRGB(x, y);
            int  red   = (pixelValue & 0x00ff0000) >> 16;
            int  green = (pixelValue & 0x0000ff00) >> 8;
            int  blue  =  pixelValue & 0x000000ff;
            int newRed = quantisePixel(red);
            int newGreen = quantisePixel(green);
            int newBlue = quantisePixel(blue);
            int newPixel = (newRed << 16) | (newGreen << 8) | newBlue;

            ditheredIMG.setRGB(x+1,y, (int) (calculateErr(pixelValue, newPixel) * (7/16.0)));
            ditheredIMG.setRGB(x-1,y+1, (int) (calculateErr(pixelValue, newPixel) * (3/16.0)));
            ditheredIMG.setRGB(x,y+1, (int) (calculateErr(pixelValue, newPixel) * (5/16.0)));
            ditheredIMG.setRGB(x+1,y+1, (int) (calculateErr(pixelValue, newPixel)* (1/16.0)));
       
        }
    }
}

public int calculateErr(int oldVal, int newVal){
    return oldVal-newVal;
}

public static int quantisePixel(int val){
    if(val > 127){
        return 255;
    } else{
        return 0;
    }

}

}

EN

回答 1

Stack Overflow用户

发布于 2022-06-20 08:03:06

嘿,这是更新的版本,但我不确定它是否正确工作,如果有人能告诉我,它是或不是,这将是非常感谢。我已经改变了它,所以现在它应该是正确地更新周围的像素,但是在我看来,它看起来和没有更新邻居完全一样。

代码语言:javascript
复制
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import static java.lang.Math.abs;

public class Dithering extends Canvas {
public BufferedImage ditheredIMG = new BufferedImage(481,480,
        BufferedImage.TYPE_BYTE_GRAY);

public void paint(Graphics g) {
    BufferedImage i = null;
    try {
        i = displayImage(g);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    getPixels(g, i);
    g.drawImage(ditheredIMG,480,0,this);



}
public static void main(String[] args) {
    Dithering d = new Dithering();
    JFrame f =new JFrame();
    f.add(d);
    f.setSize( 960,481);
    f.setVisible(true);
}


public BufferedImage displayImage(Graphics g) throws IOException {
    final File file = new File("Cat.jpg");
    final BufferedImage i = ImageIO.read(file);
    g.drawImage(i, 0,0,this);
    return i;
}

public void getPixels(Graphics g, BufferedImage i) {
    for (int y = 1; y < i.getHeight()-1; y++){
        for (int x = 1; x < i.getWidth()-1; x++) {
            int pixelValue = i.getRGB(x, y);
            int  red   = (pixelValue & 0x00ff0000) >> 16;
            int  green = (pixelValue & 0x0000ff00) >> 8;
            int  blue  =  pixelValue & 0x000000ff;
            int newRed = quantisePixel(red);
            int newGreen = quantisePixel(green);
            int newBlue = quantisePixel(blue);
            int newPixel = (newRed << 16) | (newGreen << 8) | newBlue;
            ditheredIMG.setRGB(x,y,newPixel);

            int newPixelValue = i.getRGB(x+1,y);
            ditheredIMG.setRGB(x+1,y,      (int) (newPixelValue + calculateErr(pixelValue, newPixel) * (7/16.0)));
            newPixelValue = i.getRGB(x-1,y+1);
            ditheredIMG.setRGB(x-1,y+1, (int) (newPixelValue + calculateErr(pixelValue, newPixel) * (3/16.0)));
            newPixelValue = i.getRGB(x,y+1);
            ditheredIMG.setRGB(x,y+1,      (int) (newPixelValue + calculateErr(pixelValue, newPixel) * (5/16.0)));
            newPixelValue = i.getRGB(x+1,y+1);
            ditheredIMG.setRGB(x+1,y+1, (int) (newPixelValue + calculateErr(pixelValue, newPixel)* (1/16.0)));


        }
    }
}

public int calculateErr(int oldVal, int newVal){
    return oldVal-newVal;
}

public static int quantisePixel(int val){
    if(val > 127){
        return 255;
    } else{
        return 0;
    }

}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72682753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档