代码拉取完成,页面将自动刷新
同步操作将从 明月心/OpenCvSharpStudy 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
using OpenCvSharp;
namespace OpenCvSharpStudy
{
public class OpencvHelper
{
public static Mat LoadFile(string fileName)
{
Mat mat= Cv2.ImRead(fileName, ImreadModes.Color);
return mat;
}
public static Mat ToGray(Mat mat)
{
Mat gray= new Mat();
Cv2.CvtColor(mat, gray, ColorConversionCodes.BGR2GRAY);
//Cv2.ImShow("mat", mat);
//Cv2.ImShow("gray", gray);
//Cv2.WaitKey();
return gray;
}
public static Mat Resize(Mat mat)
{
Mat result = new Mat();
// Cv2.Resize(mat, result, new OpenCvSharp.Size(300,300));
Cv2.Resize(mat, result, new OpenCvSharp.Size(0, 0), 2,2);
//Cv2.ImShow("mat", mat);
//Cv2.ImShow("result", result);
//Cv2.WaitKey();
return result;
}
public static Mat ConvertToBlackBinary(Mat mat)
{
//Cv2.ImShow("mat", mat);
Mat gray = new Mat();
Cv2.CvtColor(mat, gray, ColorConversionCodes.BGR2GRAY);//转灰度图
//Cv2.ImShow("gray", gray);
Scalar scalar = Cv2.Mean(gray);//计算灰度图平均值
Cv2.Threshold(gray, gray, scalar.Val0, 255, ThresholdTypes.Binary);//二值化
//Cv2.ImShow("mat", mat);
//Cv2.ImShow("result", gray);
Cv2.WaitKey();
return gray;
}
public static Mat Add(Mat mat)
{
Cv2.ImShow("mat", mat);
Mat gray = ConvertToBlackBinary(mat);
Cv2.BitwiseNot(gray, gray);//颜色反转,所有点按照x=255-x运算
// Cv2.ImShow("gray", gray);
Mat mat1= new Mat();
Cv2.CvtColor(gray, mat1, ColorConversionCodes.GRAY2BGR);//灰度图转彩色图,
// Cv2.BitwiseAnd(mat, mat1, mat1);
// Cv2.BitwiseOr(mat, mat1, mat1);
Cv2.BitwiseXor(mat, mat1, mat1);
Mat mat2 = mat.Clone();
mat2.SetTo( new Scalar(2,2,2));
Cv2.Multiply(mat, mat2, mat2);
Cv2.ImShow("mat2", mat2);
Cv2.WaitKey();
return mat1;
}
public static Mat GetColor(Mat mat)
{
// HSV:H颜色范围
// Orange 0 - 22
//Yellow 22 - 38
// Green 38 - 75
// Blue 75 - 130
// Violet 130 - 160
// Red 160 - 179
//Cv2.ImShow("mat", mat);
Mat hsv = new Mat();//HSV格式对象
Cv2.CvtColor(mat, hsv, ColorConversionCodes.BGR2HSV);//颜色转换
var mask = hsv.InRange(new Scalar(0, 46, 46), new Scalar(25, 255, 255));//红色区域
var mask2 = hsv.InRange(new Scalar(165, 46, 46), new Scalar(180, 255, 255));//红色区域
Cv2.BitwiseOr(mask, mask2, mask);//范围大叠加
Cv2.BitwiseNot(mask, mask);//取反
//Cv2.ImShow("mask", mask);
Mat mat1 = new Mat();
Cv2.CvtColor(mask, mat1, ColorConversionCodes.GRAY2BGR);//灰度图转彩色图,
Cv2.Add(mat, mat1, mat1);//与原图相加,获取红色的区域
//Cv2.ImShow("mat1", mat1);
Cv2.WaitKey();
return mat1;
}
public static Mat GetHoughLinesP(Mat mat)
{
Mat gray = ToGray(mat);
var kenal3 = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(1, 1));
Cv2.Dilate(gray, gray, kenal3);//
Cv2.Canny(gray, gray, 50, 150);
var lines = Cv2.HoughLinesP(gray, 1, Math.PI / 180, 100, 250, 50);
if (lines.Length > 0)
{
for (int i = 0; i < lines.Length; i++)
{
Cv2.Line(mat, lines[i].P1, lines[i].P2, new Scalar(255,0,0),1, LineTypes.AntiAlias);
}
}
return mat;
}
public static Mat Test(Mat mat)
{
return mat;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。