自动调整灯光

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Threading;
namespace Mask掩码练习
{
public partial class Form1 : Form
{
bool debug = false;
int global_threshvalue = 90;
FrameSource camera = Cv2.CreateFrameSource_Camera(0);
//void camer_run(object sender, EventArgs e);

    void camer_run()
    {
       /* Mat pic = new Mat();
        Mat src = new Mat();
       src =  Cv2.ImRead("rain2.jpg");
        Cv2.CvtColor(src, pic, ColorConversionCodes.BGR2GRAY);
        Cv2.Threshold(pic, pic, 90, 255, ThresholdTypes.Binary);
        
        
        OpenCvSharp.Point[][] contours;
        HierarchyIndex[] hierarchly;
        int count = 0;
        Cv2.FindContours(pic, out contours, out hierarchly, RetrievalModes.External, ContourApproximationModes.ApproxNone);
        for(int i =0;i<contours.Length;i++)
        {
            if (Cv2.ContourArea(contours[i],false)>=50)
            {
                Cv2.DrawContours(src, contours, i, Scalar.Red);
                count++;
            }
        }
        Cv2.PutText(src, count.ToString(), new OpenCvSharp.Point(80, 80), HersheyFonts.HersheyComplex, 1.2, Scalar.White, 1);

        Cv2.ImShow("img", src);
        Cv2.WaitKey(10);*/
        
        Mat src = new Mat();
        Mat oldframe = new Mat();
        Mat newframe = new Mat();
        Mat gray1 = new Mat();
        Mat gray2 = new Mat();
        Mat thresh1 = new Mat();
        Mat thresh2 = new Mat();
        Mat diff = new Mat();
        //Mat src1 = new Mat();
        //int thresh = 90;
        Cv2.NamedWindow("window");
        Cv2.CreateTrackbar("thresh", "window", ref global_threshvalue, 255);
       
        camera.NextFrame(newframe);
        while(true)
        {

            newframe.CopyTo(oldframe);
            camera.NextFrame(newframe);
          //  
            Cv2.CvtColor(oldframe, gray1, ColorConversionCodes.BGR2GRAY);
            Cv2.CvtColor(newframe, gray2, ColorConversionCodes.BGR2GRAY);
           
            Cv2.Threshold(gray1, thresh1, global_threshvalue, 255, ThresholdTypes.Binary);
            Cv2.Threshold(gray2, thresh2, global_threshvalue, 255, ThresholdTypes.Binary);
         
            Cv2.BitwiseXor(thresh2, thresh1, diff);
         


            Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5,5), new OpenCvSharp.Point(-1, -1));
           Cv2.Erode(diff, diff, se);//图像的腐蚀
       //     Cv2.Dilate(diff, diff, se);//图像的膨胀
            

            OpenCvSharp.Point[][] contours;
            HierarchyIndex[] hierarchly;

            Cv2.FindContours(diff, out contours, out hierarchly, RetrievalModes.External, ContourApproximationModes.ApproxNone);
            

            int count = 0;
            for (int i=0;i< contours.Length;i++)
            {
                if(Cv2.ContourArea(contours[i])>0)
                {
                    count++;
                    Cv2.DrawContours(diff, contours, i, Scalar.White);
                }

            }
            Cv2.PutText(diff, count.ToString(), new OpenCvSharp.Point(80, 80), HersheyFonts.HersheySimplex, 2.0,
                Scalar.White,2,LineTypes.Link8);
            Cv2.ImWrite("1.jpg", diff);
            Cv2.ImShow("diff", diff);
            //label1.Text = count.ToString();

            Cv2.WaitKey(10);
            


        }






        /* 
         *            等待
         * 
         * OpenCvSharp. Size mysize = new OpenCvSharp.Size(5, 5);
          Mat pic = Cv2.ImRead("a.bmp");

          Mat mask = Mat.Zeros(mysize,MatType.CV_8U);
          Cv2.Rectangle(pic, new OpenCvSharp.Rect(200, 200,10,10), Scalar.Red);
          Cv2.Circle(pic, 200, 200, 3, Scalar.Red);
          Cv2.ImShow("mask", mask);
          Cv2.ImShow("orgin", pic);
          Cv2.WaitKey(0);*/
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        //camer_run();
    }
    public Form1()
    {
        InitializeComponent();
    }
    Thread t;
    private void button1_Click(object sender, EventArgs e)
    {
        if(button1.Text=="开启主线程")
        {
            button1.Text = "关闭主线程";
            t = new Thread(camer_run);
            t.Start();

        }
        else
        {
            button1.Text = "开启主线程";
            t.Abort();

        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Mat src = new Mat();
        Mat oldframe = new Mat();
        Mat newframe = new Mat();
        Mat gray1 = new Mat();
        Mat gray2 = new Mat();
        Mat thresh1 = new Mat();
        Mat thresh2 = new Mat();
        Mat diff = new Mat();
        //Mat src1 = new Mat();
        int thresh = 90;
        Cv2.NamedWindow("window");
        //Cv2.CreateTrackbar("thresh", "window", ref thresh, 255);
        camera.NextFrame(newframe);
        bool isget = false;
        for (int threshvalue=90;threshvalue<200;threshvalue++)
        {
            for (int times = 0; times < 10; times++) 
            //for(int i=0;i<10;i++)
            {
                newframe.CopyTo(oldframe);
                camera.NextFrame(newframe);
                //  
                Cv2.CvtColor(oldframe, gray1, ColorConversionCodes.BGR2GRAY);
                Cv2.CvtColor(newframe, gray2, ColorConversionCodes.BGR2GRAY);

                Cv2.Threshold(gray1, thresh1, threshvalue, 255, ThresholdTypes.Binary);
                Cv2.Threshold(gray2, thresh2, threshvalue, 255, ThresholdTypes.Binary);

                Cv2.BitwiseXor(thresh2, thresh1, diff);



                Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5), new OpenCvSharp.Point(-1, -1));
                Cv2.Erode(diff, diff, se);//图像的腐蚀
                                          //     Cv2.Dilate(diff, diff, se);//图像的膨胀


                OpenCvSharp.Point[][] contours;
                HierarchyIndex[] hierarchly;

                Cv2.FindContours(diff, out contours, out hierarchly, RetrievalModes.External, ContourApproximationModes.ApproxNone);
                Cv2.PutText(diff, threshvalue.ToString(),new OpenCvSharp.Point(80,80),HersheyFonts.HersheyComplex,1.2,Scalar.White);
                Cv2.ImShow("diff", diff);
                Cv2.WaitKey(10);
                int count = 0;
                for (int i = 0; i < contours.Length; i++)
                {
                    if (Cv2.ContourArea(contours[i]) > 0)
                    {
                        count++;
                        Cv2.DrawContours(diff, contours, i, Scalar.White);
                    }

                }

                // 如果有一次发现轮廓则退出
                if (count>0)
                    break;
                if (times == 9 && count == 0)
                { isget = true;
                    global_threshvalue = threshvalue;

                }
                   


            }
            if(isget==true)
            {
                break;
                
            }
            


        }
    }
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值