【问题标题】:How to draw a 16x16 rectangular table at imagej?如何在 imagej 上绘制一个 16x16 的矩形表格?
【发布时间】:2014-05-04 16:30:49
【问题描述】:

我想要做的是创建一个 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();          
  }

}

【问题讨论】:

    标签: java image-processing imagej


    【解决方案1】:

    先创建一个 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();          
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      如果我理解了这个任务,一个简单的解决方案是将tbl 函数替换为具有内部循环的函数,该循环为矩形而不是单个像素着色,例如

           private void tbl(int[][] allRGB){
      
              ImageProcessor newip =new ColorProcessor(256,256);
              int count = 0 ;
              // The following could be inputs if desired. 
              int W = 5;
              int H = 5;
              for(int i=0;i<16;i++){
                  for(int j=0;j<16;j++){              
                      for(int w=0; w<W; w++){
                          for(int h=0; h<H; h++){
                              newip.putPixel(i*W+w,j*H+h,allRGB[count]);
                          }
                      }
                      count++;
                  }
              }                           
              ImagePlus cwin = new ImagePlus("TBL", newip);
              cwin.show();          
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多