1、OpencvSharp 颜色空间转换 Cv2.CvtColor()
CvtColor(),是Opencv里的颜色空间转换函数,可以实现RGB颜色向HSV,HSI等颜色空间的转换,也可以转换为灰度图像。
1:参数RGB2GRAY是RGB到gray。
2:参数 GRAY2RGB是gray到RGB。等
private void CVTColor()
{
if (cmBoxCVTColor.Text == "")
{
return;
}
ColorConversionCodes aaa = (ColorConversionCodes)Enum.Parse(typeof(ColorConversionCodes), cmBoxCVTColor.Text);
try
{
Cv2.CvtColor(src_img, dst, aaa); //src_img :原图 dst :输出图 aaa:ColorConversionCodes枚举类型
picBoxShowDel.Image = dst.ToBitmap();
}
catch (Exception e)
{
Console.WriteLine(e);
} }
2、OpencvSharp 二值化 Cv2.Threshold()
Threshold()图像的二值化,就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的只有黑和白的视觉效果。灰度值0:黑,灰度值255:白
1:thresh是设定的阈值
2:maxval是当灰度值大于(或小于)阈值时将该灰度值赋成的值
3:ThresholdTypes当前二值化的方式
private void Threshold()
{
if (cmBoxThre.Text == "")
{
return;
}
ThresholdTypes aaa = (ThresholdTypes)Enum.Parse(typeof(ThresholdTypes), cmBoxThre.Text);
try
{
Cv2.Threshold(src_img, dst, tkBarThresh.Value, tkBarMaxValue.Value, aaa);//阈值二值化 //src_img :原图 dst :输出图 tkBarThresh.Value:阈值 tkBarMaxValue.Value: 超过阈值的设置值 aaa: ThresholdTypes枚举类型
picBoxShowDel.Image = dst.ToBitmap();
}
catch (Exception e)
{
Console.WriteLine(e);
} }
3、OpencvSharp 形态学 膨胀Cv2.Dilate()、腐蚀Cv2.Erode()
Dilate(): 膨胀,通过使用特定的结构元素来扩展图像。
Erode(): 腐蚀,通过使用特定的结构元素来侵蚀图像。
private void Dilate()
{
if (cmBoxMorph.Text == "")
{
return;
} try
{
OpenCvSharp.Point point = new OpenCvSharp.Point(SharpPoint.Value, SharpPoint.Value);
MorphShapes aaa = (MorphShapes)Enum.Parse(typeof(MorphShapes), cmBoxMorph.Text);
Mat structuringElement = Cv2.GetStructuringElement(aaa, new OpenCvSharp.Size(tkBarPZFS.Value, tkBarPZFS.Value), point);
// 1:aaa为MorphShapes shape 结果元素的形状 2:Size ksize 结构元素的大小 为奇数
3:Point anchor 结构元素中心点 */
if (rdBtnPZ.Checked)
{ Cv2.Dilate(src_img, dst, structuringElement, point, DilateCount.Value); //膨胀
/* 参数:1:src_img 源图像
2: dst 输出图像
3:structuringElement 结构元素,奇数
4:point 锚点位置,默认是null
5:DilateCount.Value应用膨胀的次数。[默认情况下这是1] */
}
else
{
Cv2.Erode(src_img, dst, structuringElement, point, DilateCount.Value); //腐蚀
/* 1:src_img 源图像
2: dst 输出图像
3:structuringElement 结构元素,奇数
4:point 锚点位置,默认是null
5:DilateCount.Value应用膨胀的次数。[默认情况下这是1] */
}
picBoxShowDel.Image = dst.ToBitmap();
}
catch (Exception e)
{
Console.WriteLine(e); }
}
4、OpencvSharp 形态学 开运算、闭运算、顶帽、黑帽、膨胀、腐蚀..Cv2.MorphologyEx()
MorphTypes.Erode:腐蚀。
MorphTypes.Dilate:膨胀。
MorphTypes.Open:先腐蚀后膨胀,可以去掉小的对象。
MorphTypes.Close:先膨胀后腐蚀,可以填充图像的噪点。
MorphTypes.TopHat:原图像与开操作之间的差值操作。
MorphTypes.BlackHat:闭操作图像与原图像的差值图像。
MorphTypes.Gradient:内部是膨胀减去腐蚀。
MorphTypes.Gradient:击中击不中变换。(不明白)
private void MorphologyEx()
{
if (cmBoxMorphologyEx.Text == "")
{
return;
}
if (cmBoxMorphType.Text == "")
{
return;
}
MorphShapes aaa = (MorphShapes)Enum.Parse(typeof(MorphShapes), cmBoxMorphologyEx.Text);
MorphTypes bbb = (MorphTypes)Enum.Parse(typeof(MorphTypes), cmBoxMorphType.Text);
OpenCvSharp.Point point = new OpenCvSharp.Point(SharpPoint.Value, SharpPoint.Value);
try
{
InputArray kernel = Cv2.GetStructuringElement(aaa, new OpenCvSharp.Size(tkBarMorphology.Value, tkBarMorphology.Value), point); //结构元素
/* 参数 1:aaa MorphShapesshape结果元素 2:Size 结构元素的大小
3:point 结构元素的锚点(中心点) */
Cv2.MorphologyEx(src_img, dst, bbb, kernel, point, MorpholoCount.Value); //形态学运算
/* 参数 1:src_img源图像 2:dst输出图像 3:bbb形态操作类型 4:kernel结构数组 5:point锚点位置(中心点) 6:MorpholoCount.Value 应用腐蚀和膨胀的次数。[默认情况下这是1] 7:borderType 边缘处理方法 */
picBoxShowDel.Image = dst.ToBitmap();
}
catch (Exception e)
{
Console.WriteLine(e); }
}