用C#编写软件控制多个VNC窗口,并抓取窗口中的特征红色“X”,主要OpenCV代码如下:
//Red_X_M-2
public bool TargetRedX_Detection2(Bitmap bmpSrc, string modelInfo, double t, bool imshow)
{
//Master
using (Mat originalMat = bmpSrc.ToMat())
using (Mat oriMat = new Mat())
//originalMat.Channels.
//Model
using (Mat mat = new Mat(modelInfo, ImreadModes.AnyColor))
using (Mat modelMat = new Mat())
{
Cv2.CvtColor(originalMat, oriMat, ColorConversionCodes.BGR2GRAY);
Cv2.CvtColor(mat, modelMat, ColorConversionCodes.BGR2GRAY);
OutputArray outArray;
outArray = OutputArray.Create(oriMat);
Cv2.MatchTemplate(oriMat, modelMat, outArray, TemplateMatchModes.CCoeffNormed);//进行匹配(1母图,2模版子图,3返回的result,4匹配模式 //最好匹配为1,值越小匹配越差)
OpenCvSharp.Point minLocation, maxLocation;
double min_value = 0;
double max_value = 0;
Cv2.MinMaxLoc(InputArray.Create(outArray.GetMat()), out minLocation, out maxLocation);
Cv2.MinMaxLoc(InputArray.Create(outArray.GetMat()), out min_value, out max_value);
CompareValue = max_value;
if (max_value > t)
{
//originalMat.ConvertTo(originalMat,MatType.CV_8UC3);
Cv2.Rectangle(originalMat, maxLocation, new OpenCvSharp.Point(maxLocation.X + modelMat.Width, maxLocation.Y + modelMat.Height),new Scalar(0,0,255,255), 2); //画出匹配的矩
//alpha的定义,0是全透明,255是不透明,因为设成全透明,颜色没了,白线还会绘制
//{ Mat[720 * 820 * CV_8UC4, IsContinuous = True, IsSubmatrix = False, Ptr = 0x59aa358, Data = 0xacc6040]}
pictureBox1.Image = Image.FromStream(new MemoryStream(originalMat.ToBytes()));
PersonTrue = true;
//CV_8UC1、CV_8UC3、CV_8UC4:
//8U代表了8位unsigned int,
//C表示图片通道,
//后面的数字表示通道数,
//1是单通道灰度图,
//3是RGB三通道彩色图像,
//4是四通道带有Alpha通道的RGB图像。
//Cv2.ImShow("srcImage:", originalMat);
return true;
}
else
{
if (imshow)
{
Cv2.Rectangle(originalMat, maxLocation, new OpenCvSharp.Point(maxLocation.X + modelMat.Width, maxLocation.Y + modelMat.Height), new Scalar(0, 0, 255, 255), 2);
}
PersonTrue = false;
//pictureBox1.Image = Image.FromStream(new MemoryStream(originalMat.ToBytes()));
pictureBox1.Image = Image.FromStream(new MemoryStream(originalMat.ToBytes()));
return false;
}
}
}