由于opencv没有将rgb转换为hsi的颜色,因此我正在尝试实现补色图像处理,不幸的是我发现了问题。
这是一段代码,我尝试将rgb转换为hsi
for( i = 0 ; i < src.rows ; i++){
for( j = 0 ; j < src.cols ; j++){
var pixel = src.ucharPtr(i,j);
r = pixel[0];
g = pixel[1];
b = pixel[2];
intensity = (b+g+r)/3;
var min_val = Math.min(r,g,b);
s = 1 - 3*(min_val/(b + g + r));
if(s < 0.00001)
{
s = 0;
}else if(s > 0.99999){
s = 1;
}
if(s != 0)
{
h = 0.5 * ((r - g) + (r - b)) / Math.sqrt(((r - g)*(r - g)) + ((r - b)*(g - b)));
h = Math.acos(h);
if(b <= g)
{
h = h;
} else{
h = 360 - h;
}
}
pixel[0] = h;
pixel[1] = s;
pixel[2] = intensity;
这是我尝试将其转换回rgb的代码
for(i = 0 ; i < src.rows ; i++){
for(j = 0 ; j < src.cols ; j++){
var pixel = src.ucharPtr(i,j);
h = pixel[0];
s = pixel[1];
intensity = pixel[2];
if(h < 120){
b = intensity*(1-s);
r=intensity*((1.0+((s*Math.cos(h))/(Math.cos(60-h)))));
g=1-(r+b);
}
else if(h < 240){
h -= 120;
r=intensity*(1-s);
g=intensity*((1.0+((s*Math.cos(h))/(Math.cos(60-h)))));
b=1-(r+g);
}
else{
h=h-240;
g=intensity*(1-s);
b=intensity*((1.0+((s*Math.cos(h))/(Math.cos(60-h)))));
r=1-(g+b);
}
pixel[0] = r;
pixel[1] = g;
pixel[2] = b;
}
}
我只是遵循RBGtoHSI算法,反之亦然。我是用正确的方式做的还是我错过了某些事情?
谢谢!