gpt4 book ai didi

java - 如何在imagej上绘制一个16x16的矩形表格?

转载 作者:行者123 更新时间:2023-12-02 06:01:20 28 4
gpt4 key购买 nike

我想做的是创建一个 16 × 16 矩形颜色的新图像,并填充类似查找表的颜色。

我是 Java 和 ImageJ 的新手,这是我现在的进步。每个像素仅显示一种颜色。我想修改它以显示每种颜色 5x5 像素。我怎样才能实现这一目标。

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.util.*;
import java.awt.*;
import ij.plugin.filter.*;
import ij.process.*;
import java.lang.Math.*;
import java.awt.image.IndexColorModel;


public class colortable_plugin implements PlugInFilter {

public int setup(String arg, ImagePlus im) {
return DOES_8C; // this plugin works on indexed color images
}

public void run(ImageProcessor ip) {

IndexColorModel icm = (IndexColorModel) ip.getColorModel();
int pixBits = icm.getPixelSize();
int mapSize = icm.getMapSize();

//retrieve the current lookup tables (maps) for R,G,B
byte[] Rmap = new byte[mapSize]; icm.getReds(Rmap);
byte[] Gmap = new byte[mapSize]; icm.getGreens(Gmap);
byte[] Bmap = new byte[mapSize]; icm.getBlues(Bmap);

int[] RGB = new int[3];
int[][] allRGB = new int[256][3];
//put color in rectangle
for (int idx = 0; idx < mapSize; idx++){
int r = 0xff & Rmap[idx]; //mask to treat as unsigned byte
int g = 0xff & Gmap[idx];
int b = 0xff & Bmap[idx];
RGB[0] = r;
RGB[1] = g;
RGB[2] = b;

//save all RGB as array
for(int k=0;k<3;k++){
allRGB[idx][k]= RGB[k];
}
}
tbl(allRGB);


}

private void tbl(int[][] allRGB){

ImageProcessor newip =new ColorProcessor(256,256);
int count = 0 ;
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
newip.putPixel(i,j,allRGB[count]);count++;
}
}
ImagePlus cwin = new ImagePlus("TBL", newip);
cwin.show();
}

}

最佳答案

首先创建一个 16x16 像素的 ImageProcessor 怎么样

ImageProcessor newip =new ColorProcessor(16,16);

然后在最后缩放 ImagePlus:

IJ.run(cwin, "Size...", "width=256 height=256 constrain interpolation=None");

以下是经过这些更改的完整代码:

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.util.*;
import java.awt.*;
import ij.plugin.filter.*;
import ij.process.*;
import java.lang.Math.*;
import java.awt.image.IndexColorModel;


public class colortable_plugin implements PlugInFilter {

public int setup(String arg, ImagePlus im) {
return DOES_8C; // this plugin works on indexed color images
}

public void run(ImageProcessor ip) {

IndexColorModel icm = (IndexColorModel) ip.getColorModel();
int pixBits = icm.getPixelSize();
int mapSize = icm.getMapSize();

//retrieve the current lookup tables (maps) for R,G,B
byte[] Rmap = new byte[mapSize]; icm.getReds(Rmap);
byte[] Gmap = new byte[mapSize]; icm.getGreens(Gmap);
byte[] Bmap = new byte[mapSize]; icm.getBlues(Bmap);

int[] RGB = new int[3];
int[][] allRGB = new int[256][3];
//put color in rectangle
for (int idx = 0; idx < mapSize; idx++){
int r = 0xff & Rmap[idx]; //mask to treat as unsigned byte
int g = 0xff & Gmap[idx];
int b = 0xff & Bmap[idx];
RGB[0] = r;
RGB[1] = g;
RGB[2] = b;

//save all RGB as array
for(int k=0;k<3;k++){
allRGB[idx][k]= RGB[k];
}
}
tbl(allRGB);


}

private void tbl(int[][] allRGB){

ImageProcessor newip =new ColorProcessor(16,16);
int count = 0 ;
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
newip.putPixel(i,j,allRGB[count]);count++;
}
}
ImagePlus cwin = new ImagePlus("TBL", newip);
IJ.run(cwin, "Size...", "width=256 height=256 constrain interpolation=None");
cwin.show();
}

}

关于java - 如何在imagej上绘制一个16x16的矩形表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22658165/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com