zoukankan      html  css  js  c++  java
  • opencv在python和c#中的对照应用-替换颜色

    先来python的,

    # -*- coding:utf-8 -*-
    import cv2 as cv
    import numpy as np
    
    def rgb2hsv(r, g, b):
        r, g, b = r/255.0, g/255.0, b/255.0
        mx = max(r, g, b)
        mn = min(r, g, b)
        m = mx-mn
        if mx == mn:
            h = 0
        elif mx == r:
            if g >= b:
                h = ((g-b)/m)*60
            else:
                h = ((g-b)/m)*60 + 360
        elif mx == g:
            h = ((b-r)/m)*60 + 120
        elif mx == b:
            h = ((r-g)/m)*60 + 240
        if mx == 0:
            s = 0
        else:
            s = m/mx
        v = mx
        H = h / 2
        S = s * 255.0
        V = v * 255.0
        return H, S, V
    
    def fixHSVRange(h, s, v):
        # Normal H,S,V: (0-360,0-100%,0-100%)
        # OpenCV H,S,V: (0-180,0-255 ,0-255)
        return (180 * h / 360, 255 * s / 100, 255 * v / 100)
    
    
    # Load the aerial image and convert to HSV colourspace
    image = cv.imread("d:Screenshot1.png")
    hsv=cv.cvtColor(image,cv.COLOR_BGR2HSV)
    
    
    rgbs=[(254,214,105),(255,238,187),(151,196,252),(208,239,196),(232,157,224)
        ,(219,95,209),(13,95,152)]
    for rgb in rgbs:
        pixel =rgb2hsv(rgb[0], rgb[1], rgb[2])
        brown_hi =  np.array([pixel[0] + 20, pixel[1] + 20, pixel[2] + 40])
        brown_lo =  np.array([pixel[0] - 20, pixel[1] - 20, pixel[2] - 40])
        mask=cv.inRange(hsv,brown_lo,brown_hi)
        image[mask>0]=(255,255,255)
    
    rgbs=[(175,174,173),(170,173,167),(175,175,173),(237,236,229)]
    for rgb in rgbs:
        pixel =rgb2hsv(rgb[0], rgb[1], rgb[2])
        brown_hi =  np.array([pixel[0] + 10, pixel[1] + 10, pixel[2] + 20])
        brown_lo =  np.array([pixel[0] - 10, pixel[1] - 10, pixel[2] - 20])
        mask=cv.inRange(hsv,brown_lo,brown_hi)
        image[mask>0]=(255,255,255)
    
    
    
    cv.imshow('res',image)
    cv.imwrite("d:Screenshot3.png", image)
    # cv.imwrite("result.png",image)
    cv.waitKey(0)
    cv.destroyAllWindows()
    View Code

    c#的

            private double max(double x, double y, double z)
            {
                return (new double[] { x, y, z }).Max();
            }
            private double min(double x, double y, double z)
            {
                return (new double[] { x, y, z }).Min();
            }
            private double[] rgb2hsv(double r, double g, double b)
            {
                r = r / 255.0;
                g = g / 255.0;
                b = b / 255.0;
                double mx = max(r, g, b);
                double mn = min(r, g, b);
                double m = mx - mn;
                double h = 0;
                double s = 0;
                if (mx == mn)
                    h = 0;
                else if (mx == r)
                    if (g >= b)
                        h = ((g - b) / m) * 60;
                    else
                        h = ((g - b) / m) * 60 + 360;
                else if (mx == g)
                    h = ((b - r) / m) * 60 + 120;
                else if (mx == b)
                    h = ((r - g) / m) * 60 + 240;
                if (mx == 0)
                    s = 0;
                else
                    s = m / mx;
                double v = mx;
                double H = h / 2;
                double S = s * 255.0;
                double V = v * 255.0;
                return new double[] { H, S, V };
            }
    
    
            private void btnReplaceColor_Click(object sender, EventArgs e)
            {
                var image = Cv2.ImRead(@"d:Screenshot1.png");
                new Window("DST", WindowMode.Normal, image);
                Cv2.WaitKey();
    
                byte[][] rgbs1 = { new byte[] { 254, 214, 105 },
                    new byte[] { 255, 238, 187 }, 
                    new byte[] { 151, 196, 252 },
                    new byte[] { 208, 239, 196 }, 
                    new byte[] { 232, 157, 224 } };
    
                byte[][] rgbs2 = {
                    new byte[] { 175, 174, 173 },
                    new byte[] { 170, 173, 167 },
                    new byte[] { 175, 175, 173 },
                    new byte[] { 237, 236, 229 } };
    
                using (Mat hsv = new Mat())
                {
                    Cv2.CvtColor(image, hsv, ColorConversionCodes.BGR2HSV);
    
                    foreach (var rgb in rgbs1)
                    {
                        var pixel = rgb2hsv(rgb[0], rgb[1], rgb[2]);
                        Scalar brown_hi = new Scalar(pixel[0] + 20, pixel[1] + 20, pixel[2] + 40);
                        Scalar brown_lo = new Scalar(pixel[0] - 20, pixel[1] - 20, pixel[2] - 40);
                        Mat mask = new Mat();
                        Cv2.InRange(hsv, brown_lo, brown_hi, mask);
                        //image[mask > 0] = (255, 255, 255);
                        image.SetTo(new Scalar(255, 255, 255), mask);
                    }
    
                    foreach (var rgb in rgbs2)
                    {
                        var pixel = rgb2hsv(rgb[0], rgb[1], rgb[2]);
                        Scalar brown_hi = new Scalar(pixel[0] + 10, pixel[1] + 10, pixel[2] + 20);
                        Scalar brown_lo = new Scalar(pixel[0] - 10, pixel[1] - 10, pixel[2] - 20);
                        Mat mask = new Mat();
                        Cv2.InRange(hsv, brown_lo, brown_hi, mask);
                        //image[mask > 0] = (255, 255, 255);
                        image.SetTo(new Scalar(255, 255, 255), mask);
                    }
    
                }
    
                new Window("DST", WindowMode.Normal, image);
                Cv2.WaitKey();
            }
    View Code

     

  • 相关阅读:
    navicat连接mysql报错1251解决方案
    ubuntu 安装nodejs/npm
    sync-settings(vscode)
    ubuntu远程桌面连接windows系统
    three.js中点生成矩阵方法
    threeJs中旋转位移等操作坐标系
    ubuntu查看进程端口号及运行的程序
    Ubuntu终端远程连接linux服务器
    THREE.OrbitControls参数控制
    canvas设置长宽
  • 原文地址:https://www.cnblogs.com/yansc/p/15041916.html
Copyright © 2011-2022 走看看