java - 在java中以矩阵形式操作彩色图像

标签 java eclipse swing raster

我正在开发一个与使用 JAVA 进行彩色图像处理相关的项目。

我了解了使用 Raster 类的 getSample 方法将彩色图像转换为矩阵,

像素[x][y]=raster.getSample(x,y,0);

我得到了以像素[][]为单位的矩阵(仅红色带中的值)。 然后我使用 WritableRaster 将矩阵转换回图像,

raster.setSample(i,j,0,pixels[i][j]);

我使用将其转换为图像,

*BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); image.setData(光栅);*

但问题是,

1)我希望彩色图像按原样显示,而我只得到一个特定的波段(例如只有红色,只有蓝色......),因为我必须根据方法 setSample 的原型(prototype)指定一个波段并且获取在此处输入代码示例。

2)我怎样才能得到一个代表彩色图像的二维矩阵(在3个不同的矩阵中代表所有3个波段)

这是我借助在线代码片段编写的代码...

<p></p>

import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


class TestImage {
    ImageIcon icon;
    SampleModel sampleModel;
    public static void main(String args[]){
        TestImage mamu = new TestImage();
        File file = new File("photo.jpg");
        mamu.compute(file);
    }

    public void compute(File file){
        try{
            BufferedImage img= ImageIO.read(file);
            Raster raster=img.getData();
            sampleModel = raster.getSampleModel();
            int w=raster.getWidth(),h=raster.getHeight();
            int pixels[][]=new int[w][h];
            for (int x=0;x<w;x++){
                for(int y=0;y<h;y++){
                    pixels[x][y]=raster.getSample(x,y,0);
                }
            }
            Image image = getImage(pixels);
            JFrame frame = new JFrame("uff");
            ImageIcon icon = new ImageIcon(image);
            JLabel label = new JLabel(icon);
            frame.setContentPane(label);
            frame.setVisible(true);
            frame.setSize(200,200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public java.awt.Image getImage(int pixels[][]){
         int w=pixels.length;
         int h=pixels[0].length;
         WritableRaster raster= Raster.createWritableRaster(sampleModel, new Point(0,0));
         for(int i=0;i<w;i++){
             for(int j=0;j<h;j++){
                 raster.setSample(i,j,0,pixels[i][j]);
             }
         }
         BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
         image.setData(raster);
         File output=new File("check.jpg");
         try {
             ImageIO.write(image,"jpg",output);
         }catch (Exception e){
             e.printStackTrace();
         }
         return image;
    }
}

最佳答案

您可能正在寻找或 java.awt.image.LookupOp ,它使用 java.awt.image.LookupTable修改波段整体。举了几个例子here 。下图说明了反转:

short[] invert = new short[256];
for (int i = 0; i < 256; i++) {
    invert[i] = (short) (255 - i);
}
BufferedImageOp invertOp = new LookupOp(new ShortLookupTable(0, invert), null));
invertOp.filter(src, dst);

image

关于java - 在java中以矩阵形式操作彩色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21763192/

相关文章:

java没有执行系统命令

java - 为什么我的 head id 没有在 swing 表单中显示数据库中的下一个自动 id?如何编写必填字段的代码?

java - 使用 Java Swing 设置表中一行中突出显示的文本颜色

java空指针异常传递对象

java - 同步不同类(和不同包)中的两个方法

java - 淡入淡出 JSP 页面

eclipse 从 root 显示 README

java - JMenu 出现在 JFrame 后面

java - 如何为 Java 代码的某些部分关闭 Eclipse 代码格式化程序?

java - 具有键值对的树找不到键