⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scalablecolor.java

📁 图像检索的代码b
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * This file is part of Caliph & Emir.
 *
 * Caliph & Emir is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Caliph & Emir is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Caliph & Emir; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * --------------------
 * (c) 2002-2004 by Mathias Lux (mathias@juggle.at)
 * http://www.juggle.at
 */

/*
 * Created by Mathias Lux, mathias@juggle.at
 * User: Mathias Lux, mathias@juggle.at
 * Date: 24.09.2002
 * Time: 20:51:41
 */

package at.lux.imageanalysis;

import org.jdom.Element;
import org.jdom.Namespace;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.StringTokenizer;

/**
 * ScalableColor
 *
 * @author Mathias Lux, mathias@juggle.at
 */
public class ScalableColor {
    private BufferedImage img;
    private int NumberOfCoefficients = 64;
    private int NumberOfBitplanesDiscarded = 0;
    private int _ySize, _xSize;
    private int _h_value, _s_value, _v_value;
    private double _quant_h, _quant_s, _quant_v;
    private int _xNumOfBlocks, _yNumOfBlocks;
    private int[][][] _wholeHist = null;
//    private int[][][][][] _desHistograms;
    private int[] haarTransformedHistogram;
    private static int[][] scalableColorQuantValues =
            {
                {217, 9, 255}, {-71, 9, 255}, {-27, 8, 127}, {-54, 9, 255}, {-8, 7, 63}, {-14, 7, 63}, {-22, 7, 63}, {-29, 8, 127},
                {-6, 6, 31}, {-13, 7, 63}, {-11, 6, 31}, {-22, 7, 63}, {-9, 7, 63}, {-14, 7, 63}, {-19, 7, 63}, {-22, 7, 63},
                {0, 4, 7}, {-1, 5, 15}, {0, 3, 3}, {-2, 6, 31}, {1, 5, 15}, {-5, 6, 31}, {0, 5, 15}, {0, 7, 63},
                {2, 5, 15}, {-2, 6, 31}, {-2, 5, 15}, {0, 7, 63}, {3, 5, 15}, {-5, 6, 31}, {-1, 6, 31}, {4, 7, 63},
                {0, 3, 3}, {0, 3, 3}, {0, 3, 3}, {-1, 5, 15}, {0, 3, 3}, {0, 3, 3}, {-1, 5, 15}, {-2, 5, 15},
                {-1, 5, 15}, {-1, 4, 7}, {-1, 5, 15}, {-3, 5, 15}, {-1, 5, 15}, {-2, 5, 15}, {-4, 5, 15}, {-5, 5, 15},
                {-1, 5, 15}, {0, 3, 3}, {-2, 5, 15}, {-2, 5, 15}, {-2, 5, 15}, {-3, 5, 15}, {-3, 5, 15}, {0, 5, 15},
                {0, 5, 15}, {0, 5, 15}, {0, 5, 15}, {2, 5, 15}, {-1, 5, 15}, {0, 5, 15}, {3, 6, 31}, {3, 5, 15},
                {0, 2, 1}, {0, 2, 1}, {0, 3, 3}, {0, 4, 7}, {0, 2, 1}, {0, 2, 1}, {0, 3, 3}, {-1, 4, 7},
                {-1, 4, 7}, {-1, 4, 7}, {-2, 5, 15}, {-1, 5, 15}, {-2, 5, 15}, {-2, 5, 15}, {-2, 5, 15}, {-1, 5, 15},
                {0, 3, 3}, {0, 2, 1}, {0, 3, 3}, {-1, 4, 7}, {0, 2, 1}, {0, 3, 3}, {-1, 4, 7}, {-1, 5, 15},
                {-2, 5, 15}, {-1, 4, 7}, {-2, 5, 15}, {-1, 5, 15}, {-3, 5, 15}, {-3, 5, 15}, {-2, 5, 15}, {0, 5, 15},
                {0, 3, 3}, {0, 3, 3}, {0, 3, 3}, {-1, 4, 7}, {0, 3, 3}, {0, 3, 3}, {-2, 5, 15}, {-2, 5, 15},
                {-2, 5, 15}, {-2, 4, 7}, {-2, 5, 15}, {-1, 5, 15}, {-3, 5, 15}, {-3, 5, 15}, {-1, 5, 15}, {0, 5, 15},
                {1, 4, 7}, {0, 3, 3}, {0, 4, 7}, {-1, 4, 7}, {0, 3, 3}, {0, 4, 7}, {-1, 4, 7}, {0, 4, 7},
                {-1, 4, 7}, {-1, 3, 3}, {-1, 4, 7}, {0, 4, 7}, {-1, 5, 15}, {0, 5, 15}, {1, 5, 15}, {-1, 5, 15},
                {0, 2, 1}, {0, 2, 1}, {0, 3, 3}, {0, 3, 3}, {0, 2, 1}, {0, 2, 1}, {0, 3, 3}, {0, 3, 3},
                {0, 2, 1}, {0, 2, 1}, {0, 3, 3}, {0, 4, 7}, {0, 2, 1}, {0, 2, 1}, {0, 3, 3}, {0, 3, 3},
                {0, 3, 3}, {0, 2, 1}, {0, 3, 3}, {1, 4, 7}, {0, 2, 1}, {0, 3, 3}, {-1, 4, 7}, {1, 4, 7},
                {0, 3, 3}, {0, 3, 3}, {0, 3, 3}, {0, 4, 7}, {0, 3, 3}, {0, 3, 3}, {-1, 4, 7}, {0, 4, 7},
                {0, 3, 3}, {0, 2, 1}, {0, 3, 3}, {0, 3, 3}, {0, 2, 1}, {0, 2, 1}, {0, 3, 3}, {0, 3, 3},
                {0, 3, 3}, {0, 2, 1}, {0, 3, 3}, {1, 4, 7}, {0, 2, 1}, {0, 3, 3}, {0, 4, 7}, {1, 4, 7},
                {0, 3, 3}, {0, 2, 1}, {0, 3, 3}, {1, 5, 15}, {0, 3, 3}, {0, 3, 3}, {-1, 5, 15}, {2, 5, 15},
                {0, 3, 3}, {0, 3, 3}, {0, 3, 3}, {0, 4, 7}, {0, 3, 3}, {0, 3, 3}, {-1, 4, 7}, {1, 5, 15},
                {0, 3, 3}, {0, 2, 1}, {0, 3, 3}, {0, 3, 3}, {0, 2, 1}, {0, 3, 3}, {0, 4, 7}, {0, 4, 7},
                {0, 3, 3}, {0, 2, 1}, {0, 3, 3}, {1, 4, 7}, {0, 3, 3}, {0, 3, 3}, {-1, 5, 15}, {1, 5, 15},
                {0, 3, 3}, {0, 2, 1}, {-1, 3, 3}, {1, 5, 15}, {0, 3, 3}, {-1, 4, 7}, {-1, 5, 15}, {2, 5, 15},
                {0, 3, 3}, {0, 3, 3}, {0, 3, 3}, {0, 4, 7}, {0, 3, 3}, {-1, 3, 3}, {0, 4, 7}, {1, 4, 7},
                {1, 3, 3}, {0, 2, 1}, {-1, 3, 3}, {0, 3, 3}, {0, 3, 3}, {0, 3, 3}, {0, 3, 3}, {1, 4, 7},
                {0, 3, 3}, {0, 2, 1}, {-1, 3, 3}, {0, 4, 7}, {0, 3, 3}, {0, 3, 3}, {0, 4, 7}, {1, 4, 7},
                {0, 3, 3}, {0, 2, 1}, {0, 3, 3}, {0, 4, 7}, {0, 3, 3}, {-1, 3, 3}, {0, 4, 7}, {1, 4, 7},
                {0, 3, 3}, {0, 3, 3}, {0, 3, 3}, {0, 3, 3}, {0, 3, 3}, {-1, 3, 3}, {0, 3, 3}, {-1, 4, 7}
            };


    public ScalableColor(BufferedImage image, int NumberOfCoefficients, int NumberOfBitplanesDiscarded) {
        this.img = image;
        int numC = NumberOfCoefficients;
        if (numC < 31)
            numC = 16;
        else if (numC < 64)
            numC = 32;
        else if (numC < 128)
            numC = 64;
        else if (numC < 256)
            numC = 128;
        else if (numC >= 256) numC = 256;
        int numB = NumberOfBitplanesDiscarded;
        if (numB < 1) numB = 0;
        if (numB > 8) numB = 8;
        this.NumberOfBitplanesDiscarded = numB;
        this.NumberOfCoefficients = numC;
        _xSize = img.getWidth();
        _ySize = img.getHeight();
        _xNumOfBlocks = 1;
        _yNumOfBlocks = 1;
        _h_value = 16;
        _s_value = 4;
        _v_value = 4;
        _quant_h = 360 / (_h_value * 1.0);
        _quant_s = 256 / (_s_value * 1.0);
        _quant_v = 256 / (_v_value * 1.0);
        extract();
    }

    public ScalableColor(BufferedImage image) {
        this.img = image;
        this.NumberOfBitplanesDiscarded = 0;
        this.NumberOfCoefficients = 64;
        _xSize = img.getWidth();
        _ySize = img.getHeight();
        _xNumOfBlocks = 1;
        _yNumOfBlocks = 1;
        _h_value = 16;
        _s_value = 4;
        _v_value = 4;
        _quant_h = 360 / (_h_value * 1.0);
        _quant_s = 256 / (_s_value * 1.0);
        _quant_v = 256 / (_v_value * 1.0);
        extract();
    }

    public ScalableColor(Element Descriptor) {
        this.img = null;
        this.NumberOfBitplanesDiscarded = 0;
        this.NumberOfCoefficients = 256;

        String numCoeff = Descriptor.getAttributeValue("numOfCoeff");
        String numBitPlanes = Descriptor.getAttributeValue("numOfBitplanesDiscarded");

        if (numCoeff != null && numBitPlanes != null) {
            this.NumberOfBitplanesDiscarded = Integer.parseInt(numBitPlanes);
            this.NumberOfCoefficients = Integer.parseInt(numCoeff);
            String coeffs = Descriptor.getChildText("Coeff", Descriptor.getNamespace());
            haarTransformedHistogram = new int[NumberOfCoefficients];
            StringTokenizer st = new StringTokenizer(coeffs, " ", false);
            int count = 0;
            while (st.hasMoreElements()) {
                String s = st.nextToken();
                haarTransformedHistogram[count] = Integer.parseInt(s.trim());
                count++;
            }
        } else {
            System.out.println("Error: Couldn't read both attributes numOfBitplanesDiscarded and numOfCoeff");
            haarTransformedHistogram = null;
        }

        _xSize = 0;
        _ySize = 0;
        _xNumOfBlocks = 1;
        _yNumOfBlocks = 1;
        _h_value = 16;
        _s_value = 4;
        _v_value = 4;
        _quant_h = 360 / (_h_value * 1.0);
        _quant_s = 256 / (_s_value * 1.0);
        _quant_v = 256 / (_v_value * 1.0);
//        extract();
    }


//    public ScalableColor(BufferedImage image, int h_value, int s_value, int v_value, int x_blocks, int y_blocks) {
//        //image parameters
//        img = image;
//        this._xSize = img.getWidth();
//        this._ySize = img.getHeight();
//
//
//        //number of blocks
//        _xNumOfBlocks = x_blocks;
//        _yNumOfBlocks = y_blocks;
//
//        //values for quantisation
//        this._h_value = h_value;
//        this._s_value = s_value;
//        this._v_value = v_value;
//        this._quant_h = 360 / (_h_value * 1.0);
//        this._quant_s = 256 / (_s_value * 1.0);
//        this._quant_v = 256 / (_v_value * 1.0);
//        extract();
//    }


    void extract() {
//        int i,j,m;
        int imageColSize = _xSize * _ySize * 3;
//        System.out.println("IMG: (" + _xSize + ", " + _ySize + ")");
        //contains HSV- values of the Image
        int[] hsvImageBuffer;
        hsvImageBuffer = new int[imageColSize];

        //_RGB2HSV
        int[] hsv = new int[3];
        WritableRaster raster = img.getRaster();
        int[] pixel = new int[3];

        for (int i = 0; i < imageColSize; i += 3) {
//            System.out.println(i+ ": (" + (i/3) % _xSize + ", " + (i/3) / _xSize + ")");
            raster.getPixel((i / 3) % _xSize, (i / 3) / _xSize, pixel);
            _RGB2HSV(pixel[0], pixel[1], pixel[2], hsv);
            hsvImageBuffer[i] = hsv[0];
            hsvImageBuffer[i + 1] = hsv[1];
            hsvImageBuffer[i + 2] = hsv[2];
        }

        if (_xNumOfBlocks > _xSize)
            _xNumOfBlocks = _xSize;
        if (_yNumOfBlocks > _ySize)
            _yNumOfBlocks = _ySize;

        //width and height of the blocks
        int width = _xSize / _xNumOfBlocks;
        int height = _ySize / _yNumOfBlocks;

        // nur bei verwendung von bloecken ...
//        _desHistograms = new int[_xNumOfBlocks][_yNumOfBlocks][_h_value][_s_value][_v_value];
//        for (int i = 0; i < _xNumOfBlocks; i++) {
//            for (int j = 0; j < _yNumOfBlocks; j++) {
//                for (int k = 0; k < _h_value; k++) {
//                    for (int l = 0; l < _s_value; l++) {
//                        for (int m = 0; m < _v_value; m++)
//                            _desHistograms[i][j][k][l][m] = 0;
//                    }
//                }
//            }
//        }

        _wholeHist = new int[_h_value][_s_value][_v_value];
        for (int k = 0; k < _h_value; k++) {
            for (int l = 0; l < _s_value; l++) {
                for (int m = 0; m < _v_value; m++)
                    _wholeHist[k][l][m] = 0;
            }
        }

        //Quantisation and histogram-calculation
        int x, y;
        x = 0;
        y = 0;

        for (int m = 0; m < _xNumOfBlocks; m++) {
            for (int n = 0; n < _yNumOfBlocks; n++) {
                int xy;
                for (int j = y; j < (y + height); j++) {
                    xy = (x * 3) + (j * _xSize * 3);
                    for (int i = xy; i < (xy + (width * 3)); i += 3) {
                        _Quant(hsvImageBuffer[i], hsvImageBuffer[i + 1], hsvImageBuffer[i + 2], m, n);
                    }
                }
                y += height;
            }
            x += width;
            y = 0;
        }
        int[] tmpHist = new int[_h_value * _v_value * _s_value];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -