OpenCV+C#+VNC

用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;
                }
            }                        
        }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值