最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

python数字图像处理、色彩空间类型转换

IT圈 admin 8浏览 0评论

python数字图像处理、色彩空间类型转换

转载的,留下学习!

using System;

using System.Drawing;

namespace EngTyping.Class

{

///

/// ColorHandle 处理Rgb|Hsv的相互转换

///

internal class ColorHandle

{

public int H,S,V;

public int R,G,B;

public int C,M,Y,K;

///

/// 处理Rgb到Hsv的转换

///

///

///

///

///

public void RgbToHsv(int R,int G,int B)

{

Color MyColor=Color.FromArgb(R,G,B);

H=Convert.ToInt32(MyColor.GetHue());

//奇怪——用微软自己的方法获得的S值和V值居然不对

//S=Convert.ToInt32(MyColor.GetSaturation()/255*100);

//V=Convert.ToInt32(MyColor.GetBrightness()/255*100);

decimal min;

decimal max;

decimal delta;

decimal R1=Convert.ToDecimal(R)/255;

decimal G1=Convert.ToDecimal(G)/255;

decimal B1=Convert.ToDecimal(B)/255;

min = Math.Min(Math.Min(R1, G1), B1);

max = Math.Max(Math.Max(R1, G1), B1);

V = Convert.ToInt32(max*100);

delta = (max - min)*100;

if (max == 0 || delta == 0)

S = 0;

else

S = Convert.ToInt32(delta / max);

}

public void RgbToHsv(Color MyColor)

{

//Color MyColor=Color.FromArgb(R,G,B);

int R = MyColor.R;

int G = MyColor.G;

int B = MyColor.B;

H=Convert.ToInt32(MyColor.GetHue());

//奇怪——用微软自己的方法获得的S值和V值居然不对

//S=Convert.ToInt32(MyColor.GetSaturation()/255*100);

//V=Convert.ToInt32(MyColor.GetBrightness()/255*100);

decimal min;

decimal max;

decimal delta;

decimal R1=Convert.ToDecimal(R)/255;

decimal G1=Convert.ToDecimal(G)/255;

decimal B1=Convert.ToDecimal(B)/255;

min = Math.Min(Math.Min(R1, G1), B1);

max = Math.Max(Math.Max(R1, G1), B1);

V = Convert.ToInt32(max*100);

delta = (max - min)*100;

if (max == 0 || delta == 0)

S = 0;

else

S = Convert.ToInt32(delta / max);

}

///

/// 处理Hsv到Rgb的转换

///

///

///

///

public void HsvToRgb(int H,int S,int V)

{

H=Convert.ToInt32(Convert.ToDecimal( H )/360*255);

S=Convert.ToInt32(Convert.ToDecimal( S )/100*255);

V=Convert.ToInt32(Convert.ToDecimal( V )/100*255);

if (S==0)

{

R=0;

G=0;

B=0;

}

decimal fractionalSector;

decimal sectorNumber;

decimal sectorPos;

sectorPos = (Convert.ToDecimal(H)/255*360)/60;

sectorNumber = Convert.ToInt32(Math.Floor(Convert.ToDouble(sectorPos)));

fractionalSector = sectorPos - sectorNumber;

decimal p;

decimal q;

decimal t;

decimal r=0;

decimal g=0;

decimal b=0;

decimal ss=Convert.ToDecimal(S)/255;

decimal vv=Convert.ToDecimal(V)/255;

p =vv * (1 - ss);

q = vv * (1 - (ss * fractionalSector));

t = vv * (1 - (ss * (1 - fractionalSector)));

switch (Convert.ToInt32(sectorNumber))

{

case 0:

r =vv;

g = t;

b = p;

break;

case 1:

r = q;

g = vv;

b = p;

break;

case 2:

r = p;

g = vv;

b = t;

break;

case 3:

r = p;

g = q;

b = vv;

break;

case 4:

r = t;

g = p;

b = vv;

break;

case 5:

r = vv;

g = p;

b = q;

break;

}

R= Convert.ToInt32(r * 255);

G= Convert.ToInt32(g * 255);

B= Convert.ToInt32(b * 255);

}

///

/// RgbtoCmy

///

///

///

///

public void RgbToCmyk(decimal R,decimal G,decimal B)

{

C = Convert.ToInt32((255 - Math.Abs(R)) * 100 / 255);

M = Convert.ToInt32((255 - Math.Abs(G)) * 100 / 255);

Y = Convert.ToInt32((255 - Math.Abs(B)) * 100 / 255);

}

}

}

python数字图像处理、色彩空间类型转换

转载的,留下学习!

using System;

using System.Drawing;

namespace EngTyping.Class

{

///

/// ColorHandle 处理Rgb|Hsv的相互转换

///

internal class ColorHandle

{

public int H,S,V;

public int R,G,B;

public int C,M,Y,K;

///

/// 处理Rgb到Hsv的转换

///

///

///

///

///

public void RgbToHsv(int R,int G,int B)

{

Color MyColor=Color.FromArgb(R,G,B);

H=Convert.ToInt32(MyColor.GetHue());

//奇怪——用微软自己的方法获得的S值和V值居然不对

//S=Convert.ToInt32(MyColor.GetSaturation()/255*100);

//V=Convert.ToInt32(MyColor.GetBrightness()/255*100);

decimal min;

decimal max;

decimal delta;

decimal R1=Convert.ToDecimal(R)/255;

decimal G1=Convert.ToDecimal(G)/255;

decimal B1=Convert.ToDecimal(B)/255;

min = Math.Min(Math.Min(R1, G1), B1);

max = Math.Max(Math.Max(R1, G1), B1);

V = Convert.ToInt32(max*100);

delta = (max - min)*100;

if (max == 0 || delta == 0)

S = 0;

else

S = Convert.ToInt32(delta / max);

}

public void RgbToHsv(Color MyColor)

{

//Color MyColor=Color.FromArgb(R,G,B);

int R = MyColor.R;

int G = MyColor.G;

int B = MyColor.B;

H=Convert.ToInt32(MyColor.GetHue());

//奇怪——用微软自己的方法获得的S值和V值居然不对

//S=Convert.ToInt32(MyColor.GetSaturation()/255*100);

//V=Convert.ToInt32(MyColor.GetBrightness()/255*100);

decimal min;

decimal max;

decimal delta;

decimal R1=Convert.ToDecimal(R)/255;

decimal G1=Convert.ToDecimal(G)/255;

decimal B1=Convert.ToDecimal(B)/255;

min = Math.Min(Math.Min(R1, G1), B1);

max = Math.Max(Math.Max(R1, G1), B1);

V = Convert.ToInt32(max*100);

delta = (max - min)*100;

if (max == 0 || delta == 0)

S = 0;

else

S = Convert.ToInt32(delta / max);

}

///

/// 处理Hsv到Rgb的转换

///

///

///

///

public void HsvToRgb(int H,int S,int V)

{

H=Convert.ToInt32(Convert.ToDecimal( H )/360*255);

S=Convert.ToInt32(Convert.ToDecimal( S )/100*255);

V=Convert.ToInt32(Convert.ToDecimal( V )/100*255);

if (S==0)

{

R=0;

G=0;

B=0;

}

decimal fractionalSector;

decimal sectorNumber;

decimal sectorPos;

sectorPos = (Convert.ToDecimal(H)/255*360)/60;

sectorNumber = Convert.ToInt32(Math.Floor(Convert.ToDouble(sectorPos)));

fractionalSector = sectorPos - sectorNumber;

decimal p;

decimal q;

decimal t;

decimal r=0;

decimal g=0;

decimal b=0;

decimal ss=Convert.ToDecimal(S)/255;

decimal vv=Convert.ToDecimal(V)/255;

p =vv * (1 - ss);

q = vv * (1 - (ss * fractionalSector));

t = vv * (1 - (ss * (1 - fractionalSector)));

switch (Convert.ToInt32(sectorNumber))

{

case 0:

r =vv;

g = t;

b = p;

break;

case 1:

r = q;

g = vv;

b = p;

break;

case 2:

r = p;

g = vv;

b = t;

break;

case 3:

r = p;

g = q;

b = vv;

break;

case 4:

r = t;

g = p;

b = vv;

break;

case 5:

r = vv;

g = p;

b = q;

break;

}

R= Convert.ToInt32(r * 255);

G= Convert.ToInt32(g * 255);

B= Convert.ToInt32(b * 255);

}

///

/// RgbtoCmy

///

///

///

///

public void RgbToCmyk(decimal R,decimal G,decimal B)

{

C = Convert.ToInt32((255 - Math.Abs(R)) * 100 / 255);

M = Convert.ToInt32((255 - Math.Abs(G)) * 100 / 255);

Y = Convert.ToInt32((255 - Math.Abs(B)) * 100 / 255);

}

}

}

发布评论

评论列表 (0)

  1. 暂无评论