zoukankan      html  css  js  c++  java
  • 我的SWT与数字图像处理总结(3)—SWT如何得到图像某个位置的像素值和相应的RGB的值

    SWT中如何得到图像某个位置的像素值和相应的RGB的值
    方法一:
    通过PaletteData中的三个mask来得到RGB,这里是逐行得到的像素值
    这种方式是最严谨的方式,对于非索引图像来说
    ImageData ideaImageData = img.getImageData(); 
    // The pixel data of the image.
    byte[] pixelData = ideaImageData.data;
    int redMask = ideaImageData.palette.redMask;
    int blueMask = ideaImageData.palette.blueMask;
    int greenMask = ideaImageData.palette.greenMask;
    int h = 0;
    int pixelValue =0;
    int[] lineData = new int[ideaImageData.width];
    for (int y = 0; y < ideaImageData.height; y++) {
        // 首先是得到每行的像素值,保存到int数组lineData中
        ideaImageData.getPixels(0, y, ideaImageData.width, lineData, 0);
        for (int x = 0; x < lineData.length; x++) {
            //得到像素值
            pixelValue = lineData[x];
            //根据像素值得到RGB的值
            int r = pixelValue & redMask;
            int g = (pixelValue & greenMask) >> 8;
            int b = (pixelValue & blueMask) >> 16;    
           //根据RGB的值得到亮度(或者intensity)
            double temp = (double) (0.3 * r + 0.59 * g + 0.11 * b);
            h = (int) (temp) + ((temp - (int) (temp)) > 0.5 ? 1 : 0);
            h = h < 0 ? 0 : h;
            h = h > 255 ? 255 : h;
            histArray[h]++;
        }
    }
     
    方法二:
    这里是直接得到的,通过偏移量offset从ImageData的data中得到像素值,bingdedaoRGB分量以及Brightness
    ImageData ida = img.getImageData(); 
    byte[] data2 = new byte[ida.data.length];
    int offset = 0;
    int k = 0;
    int r, g, b, gray;
    for (int i = 0; i < ida.height; i++) {
        for (int j = 0; j < ida.width; j++) {
            k = offset + j * 3;
            b = ida.data[k] & 0xff;
            g = ida.data[k + 1] & 0xff;
            r = ida.data[k + 2] & 0xff;
            gray = (int) (0.11 * b + 0.59 * g + 0.3 * r);
            data2[k] = data2[k + 1] = data2[k + 2] = (byte) gray;
        }
        offset += ida.bytesPerLine;
    }
    ImageData ida2 = new ImageData(ida.width, ida.height, ida.depth, ida.palette, ida.scanlinePad, data2);
    imgdst = new Image(img.getDevice(), ida2);
     
    方式三:
    根据颜色值得到亮度值,也包含了通过像素值得到RGB分量,并得到Brightness
    /** 
     * 根据颜色值(实际上是像素值)得到亮度 
     */

    public static int getBrightness(int color) {
        int r = (color & 0x00ff0000) >> 16;
        int g = (color & 0x0000ff00) >> 8;
        int b = (color & 0x000000ff);
        int y = Math.round(0.3f * r + 0.59f * g + 0.11f * b);
        y = y < 0 ? 0 : y;
        y = y > 255 ? 255 : y;
        return y;
    }





  • 相关阅读:
    Ubuntu10.04搭建linux-0.11编译环境(1.bochs安装和使用)
    Linux 0.11内核编译和bochs上的实验环境的搭建
    64位Linux的内核和用户地址空间
    2012年计算机考研大纲——操作系统
    【27.34%】【codeforces 611D】New Year and Ancient Prophecy
    【14.94%】【codeforces 611E】New Year and Three Musketeers
    【53.57%】【codeforces 610C】Harmony Analysis
    【42.49%】【hdu 1542】Atlantis(线段树扫描线简析)
    【49.23%】【hdu 1828】Picture
    【20.51%】【codeforces 610D】Vika and Segments
  • 原文地址:https://www.cnblogs.com/yinger/p/2237798.html
Copyright © 2011-2022 走看看