RGB到HSV转换会产生噪声图像

时间:2016-01-01 00:21:59

标签: c# image rgb hsv

我的应用程序正在进行颜色分割,但它无法正常工作。右下角的图像是HSV图像,但它非常嘈杂和奇怪,我不明白为什么。

Image

这是我的代码:

Image<Bgr, Byte> frame_drone = null; //Original do drone
Image<Hsv, Byte> frame_drone_hsv = new Image<Hsv, Byte>(1280, 720); //processado drone hsv
Image<Gray,Byte> frame_drone_processado = new Image<Gray, Byte>(1280, 720); //processado drone
Image<Gray, Byte> frame_drone_canny = new Image<Gray, Byte>(1280, 720); //canny drone       

int erosao = 3;
int dilatacao = 9;             
StructuringElementEx elemento_erosao = new StructuringElementEx(erosao, erosao, 1, 1, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT); //elemento kernelx,kernely,anchorx,anchory
StructuringElementEx elemento_dilatacao = new StructuringElementEx(dilatacao, dilatacao, 1, 1, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT); //elemento kernelx,kernely,anchorx,anchory            

frame_drone.SmoothMedian(3); //Filtro mediana antes da passagem para HSV
frame_drone_hsv = frame_drone.Convert<Hsv, Byte>().PyrDown().PyrUp(); //Converte a imagem da camera RGB para HSV             
frame_drone_processado = frame_drone_hsv.InRange(new Hsv(hl.Value, sl.Value, vl.Value), new Hsv(hh.Value, sh.Value, vh.Value)); //utiliza as trackbars HSV para ver a cor pretendida 

//Dilatacao seguida de erosao para fechar o ruido
CvInvoke.cvDilate(frame_drone_processado, frame_drone_processado, elemento_dilatacao, 1);
CvInvoke.cvErode(frame_drone_processado, frame_drone_processado, elemento_erosao, 1);

frame_drone_processado.SmoothGaussian(9); //Filtro gaussiano na imagem binaria        

frame_drone_canny = frame_drone_processado.Canny(new Gray(50), new Gray (150)); //Canny

//Show Pictureboxes
picturebox_canny.Image = frame_drone_canny.Bitmap; // Mostra imagem canny
picturebox_hsv.Image = frame_drone_hsv.Bitmap; //mostra imagem hsv
processadamaster.Image = frame_drone_processado.Bitmap; //Mostra a imagem processada
originalmaster.Image = frame_drone.Bitmap; // Mostra a imagem original

发生了什么事?任何的想法? :/

编辑:我知道色调范围是0-255。我只是测试当我拍照时不是0-360

编辑2:我尝试使用CvtColor函数,程序关闭。

 CvInvoke.cvCvtColor(frame_drone, frame_drone_hsv, COLOR_CONVERSION.CV_BGR2HSV); 

错误: Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.dll

但如果我这样做

  

CvInvoke.cvCvtColor(frame_drone,frame_drone,COLOR_CONVERSION.CV_BGR2HSV);

效果很好,但我的原始图像上有HSV图像,我无法使用:

frame_drone_processado = frame_drone_hsv.InRange(new Hsv(hl.Value, sl.Value, vl.Value), new Hsv(hh.Value, sh.Value, vh.Value)); //utiliza as trackbars HSV para ver a cor pretendida 

因为它说:

Cannot Convert Bgr to Hsv

1 个答案:

答案 0 :(得分:0)

我知道这很愚蠢但可能对某人有所帮助。它的工作原理如下:

// Convert image from RGB to HSV
frame_drone_hsv = frame_drone.Convert<Hsv, Byte>().PyrDown().PyrUp(); 
CvInvoke.cvCvtColor(frame_drone_hsv, frame_drone_hsv, COLOR_CONVERSION.CV_BGR2HSV); 

但仍然有很多噪音。有没有比我更好的方法?