JAVA编写RGB转HLS,RGB转HSV,HSV转RGB,RGB HLS HSV三种模式转换

  1. RGB & HSV & HLS英文全称及解释

2. RGB转HSV

3.HSV转RGB

4.RGB HSV HLS三种色彩模式转换(C语言实现)

5.RGB转HLS并获取绿视绿(JAVA语言实现)

1.RGB & HSV & HLS英文全称及解释

RGBRed, Green, Blue,是工业界的一种颜色标准。

HSVHue, Saturation, Value,是根据颜色的直观特性。

  max=max(R,G,B)
  min=min(R,G,B)
  if R = max, H = (G-B)/(max-min)
  if G = max, H = 2 + (B-R)/(max-min)
  if B = max, H = 4 + (R-G)/(max-min)

  H = H * 60
  if H < 0, H = H + 360

 V=max(R,G,B)
 S=(max-min)/max
 if s = 0
 R=G=B=V
 else
 H /= 60;
 i = INTEGER(H)

 f = H - i
 a = V * ( 1 - s )
 b = V * ( 1 - s * f )
 c = V * ( 1 - s * (1 - f ) )

 switch(i)
 case 0: R = V; G = c; B = a;
 case 1: R = b; G = v; B = a;
 case 2: R = a; G = v; B = c;
 case 3: R = a; G = b; B = v;
 case 4: R = c; G = a; B = v;
 case 5: R = v; G = a; B = b;

void RGB2HSV( uint16_t * h, uint16_t * s, uint16_t * v, uint8_t r, uint8_t g, uint8_t b)
{
    double rr, gg, bb;
    double hh, ss, vv;
    double cmax, cmin, cdes;

    rr = r;
    gg = g;
    bb = b;

    cmax = (rr > gg) ? rr : gg;
    if (bb > cmax) {
        cmax = bb;
    }

    cmin = (rr < gg) ? rr : gg;
    if (bb < cmin) {
        cmin = bb;
    }

    cdes = cmax - cmin;
    vv = cmax;
    if (cdes != 0) {
        ss = cdes * SCALE / cmax;
        if (cmax == rr) {
            hh = (gg - bb) * SCALE / cdes;
        }else if (cmax == gg) {
            hh = (bb - rr) * SCALE / cdes + 2 * H_SCALE;
        }else {
            hh = (rr - gg) * SCALE / cdes + 4 * H_SCALE;
        }
    }else if (cmax != 0) {
        ss = cdes * SCALE / cmax;
        hh = 0;
    }else {
        ss = 0;
        hh = 0;
    }
    if (hh < 0) {
        hh += 6 * H_SCALE;
    }

    *h = hh * H_GETA;
    *s = ss * H_GETA;
    *v = vv * H_GETA;
}

void HSV2RGB( uint8_t *r, uint8_t *g, uint8_t *b, uint16_t h, uint16_t s, uint16_t v)
{
    double rr = 0, gg = 0, bb = 0;
    double hh, ss, vv;

    if (h == 6 * H_GETA * H_SCALE) {
        h = 0;
    }
    hh = (double)h / H_GETA;
    ss = (double)s / GETA;
    vv = (double)v / GETA;

    switch((int)(hh / H_SCALE)) {
        case 0:
            rr = SCALE;
            gg = hh;
            bb = 0;
            break;
        case 1:
            rr = 2 * H_SCALE - hh;
            gg = SCALE;
            bb = 0;
            break;
        case 2:
            rr = 0;
            gg = SCALE;
            bb = hh - 2 * H_SCALE;
            break;
        case 3:
            rr = 0;
            gg = 4 * H_SCALE - hh;
            bb = SCALE;
            break;
        case 4:
            rr = hh - 4 * H_SCALE;
            gg = 0;
            bb = SCALE;
            break;
        case 5:
            rr = SCALE;
            gg = 0;
            bb = 6 * H_SCALE - hh;
            break;
    }

    rr = (rr + (SCALE - rr) * (SCALE - ss) / SCALE) * vv / SCALE;
    gg = (gg + (SCALE - gg) * (SCALE - ss) / SCALE) * vv / SCALE;
    bb = (bb + (SCALE - bb) * (SCALE - ss) / SCALE) * vv / SCALE;

    *r = rr;
    *g = gg;
    *b = bb;
    if (*r > 255)*r = 255;
    if (*g > 255)*g = 255;
    if (*b > 255)*b = 255;
}

void RGB2HLS( double *h, double *l, double *s, uint8_t r, uint8_t g, uint8_t b)
{
    double dr = (double)r/255;
    double dg = (double)g/255;
    double db = (double)b/255;
    double cmax = MAX(dr, MAX(dg, db));
    double cmin = MIN(dr, MIN(dg, db));
    double cdes = cmax - cmin;
    double hh, ll, ss;

    ll = (cmax+cmin)/2;
    if(cdes){
        if(ll  360)
        hue -= 360;
    else if(hue < 0)
        hue += 360;
    if(hue < 60)
        return n1+(n2-n1)*hue/60;
    else if(hue < 180)
        return n2;
    else if(hue < 240)
        return n1+(n2-n1)*(240-hue)/60;
    else
        return n1;
}

5.RGB转HLS并获取绿视绿(JAVA语言实现)

RGB2HLS.java

package com.example.GLR;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class RGB2HLS {
    static class HLS {
        float H, L, S;
        /**
         * 定义HLS返回类
         * @param H h
         * @param L l
         * @param S s
         */
        public HLS(float H, float L, float S) {
            this.H = H;
            this.L = L;
            this.S = S;
        }
    }
    /**
     * 获取图片RGB
     * @param imgPath 文件路径
     * @param sf 压缩率
     * @return
     */
    public static double GetRGB(String imgPath, double sf){
        BufferedImage bufferedImage;
        int r,g,b;
        int height,width;
        int gg = 0, rr= 0;
        bufferedImage= ImageUtil.reSize(new File(imgPath),sf,sf);
        assert bufferedImage != null;
        height = bufferedImage.getHeight();
        width = bufferedImage.getWidth();
        for (int y = 0; y0){
            if(ll

ImageUtil.java

package com.example.GLR;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtil {
    /**
     * @param srcImg  原图片
     * @param sx      缩放率
     * @param sy      缩放率
     * @return
     */
    public static BufferedImage reSize(File srcImg, double sx, double sy) {
        String type = getImageType(srcImg);
        if (type == null || sx < 0 || sy < 0) {
            return null;
        }
        BufferedImage srcImage;
        try {
            srcImage = ImageIO.read(srcImg);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        // targetW,targetH分别表示目标长和宽
        BufferedImage target;
        double width =  sx * srcImage.getWidth();
        double height =  sy * srcImage.getHeight();
        ColorModel cm = srcImage.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster((int) width, (int) height);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();

        target = new BufferedImage(cm, raster, alphaPremultiplied, null);
        Graphics2D g = target.createGraphics();
        // smoother than exlax:
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.drawRenderedImage(srcImage, AffineTransform.getScaleInstance(sx, sy));
        g.dispose();
        return target;
    }

    /**
     * 获取文件后缀不带.

     *
     * @param file 文件后缀名
     * @return
     */
    private static String getImageType(File file) {
        if (file != null && file.exists() && file.isFile()) {
            String fileName = file.getName();
            int index = fileName.lastIndexOf(".");
            if (index != -1 && index < fileName.length() - 1) {
                return fileName.substring(index + 1);
            }
        }
        return null;
    }
}

GRL.java

package com.example.GLR;

public class GLR {
    public static void GLRsingle(String imgPath, double sf){
        double proportion = RGB2HLS.GetRGB(imgPath,sf);
        System.out.println("The green visual rate is:"+proportion*100+"%");

    }
    public static void main(String[] args) {
        GLRsingle("C:\\Users\\Obama\\IdeaProjects\\demo\\src\\main\\java\\com\\example\\GLR\\inputImg\\greenTest1.png",0.2);
    }
}

Original: https://blog.csdn.net/qq_41564074/article/details/124633322
Author: Obama110
Title: JAVA编写RGB转HLS,RGB转HSV,HSV转RGB,RGB HLS HSV三种模式转换

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/640965/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球