Emgu CV4图像处理之对比度亮度调整与通道分离9(C#)

本文测试环境:

win10  64位

vistual studio 2019  

Emgu CV 4.6.0

环境配置准备:

1 新增控制台项目,.net framework为4.7.2

2  把win-x64目录的native目录下的文件全部拷贝到项目的运行目录Debug目录下

3  项目选择x64

4 添加项目引用Emgu.CV.dll、Emgu.CV.Platform.NetFramework.dll、System.Drawing.dll和System.Runtime.InteropServices.RuntimeInformation.dll  

具体配置参考:

Emgu CV4图像处理之环境搭建1(C#)_zxy2847225301的博客-CSDN博客

下面的内容转自:EmguCV-第10讲-对比度亮度调整与通道分离_YADONCHEN的博客-CSDN博客

1  对比度和亮度调整

在这里插入图片描述

代码如下:

using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmguCVDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
            Image<Bgr, Byte> chunfen1 = new Image<Bgr, Byte>("chunfen1.png");

            Image<Bgr, Byte> outPutImage = new Image<Bgr, Byte>(new System.Drawing.Size(chunfen1.Width,chunfen1.Height));
            double contract = 1.2;
            int brighness = 2;

            for (int i = 0; i < chunfen1.Rows; i++)
            {
                for (int j = 0; j <chunfen1.Cols; j++)
                {
                   int B=(int)((int)chunfen1.Data[i, j, 0] * contract) + brighness;
                   int G = (int)((int)chunfen1.Data[i, j,1] * contract) + brighness;
                   int R = (int)((int)chunfen1.Data[i, j,2] * contract) + brighness;
                   outPutImage.Data[i, j, 0] =(byte)B;
                   outPutImage.Data[i, j, 1] = (byte)G;
                   outPutImage.Data[i, j, 2] = (byte)R;

                }
            }
            CvInvoke.Imshow("outPutImage", outPutImage);

            CvInvoke.WaitKey(0);
            Console.ReadLine();
        }
    }
}

chunfen1.png原图为:

程序运行结果为:

2  通道分离

在这里插入图片描述

代码如下:

 

using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmguCVDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
            Mat chunfen1 = CvInvoke.Imread("chunfen1.png");
            Mat []channelArray=chunfen1.Split();
            //显示B通道
            CvInvoke.Imshow("B", channelArray[0]);
            CvInvoke.WaitKey(0);

            //显示G通道
            CvInvoke.Imshow("G", channelArray[1]);
            CvInvoke.WaitKey(0);

            //显示R通道
            CvInvoke.Imshow("R", channelArray[2]);
            CvInvoke.WaitKey(0);

            CvInvoke.WaitKey(0);
            Console.ReadLine();
        }
    }
}

 chunfen1.png原图为:

 

程序运行结果如下:

看结果,B、G、R三通道单独拿出来显示,显示为灰度图

3 通道合并

在这里插入图片描述

 

 代码如下:

 //二值化处理,第一个参数为输入图片,第二个参数是输出图片,100为二值化的临界值,255为二值化的最大值
CvInvoke.Threshold(channelArray[0], channelArray[0], 100, 255, Emgu.CV.CvEnum.ThresholdType.Binary);

using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmguCVDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
            Mat chunfen1 = CvInvoke.Imread("chunfen1.png");
            Mat []channelArray=chunfen1.Split();

            Mat outPutMat = new Mat();
            //二值化处理,第一个参数为输入图片,第二个参数是输出图片,100为二值化的临界值,255为二值化的最大值
            CvInvoke.Threshold(channelArray[0], channelArray[0], 100, 255, Emgu.CV.CvEnum.ThresholdType.Binary);
            CvInvoke.Threshold(channelArray[1], channelArray[1], 100, 255, Emgu.CV.CvEnum.ThresholdType.Binary);
            CvInvoke.Threshold(channelArray[2], channelArray[2], 100, 255, Emgu.CV.CvEnum.ThresholdType.Binary);


            VectorOfMat vectorOfMat = new VectorOfMat();
            vectorOfMat.Push(channelArray[0]);
            vectorOfMat.Push(channelArray[1]);
            vectorOfMat.Push(channelArray[2]);


            CvInvoke.Merge(vectorOfMat, outPutMat);

            CvInvoke.Imshow("outPutMat", outPutMat);
            CvInvoke.WaitKey(0);
            Console.ReadLine();
        }
    }
}

 

运行结果如下:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用EMGU.CV进行图像分类训练和测试的C#代码示例: ```csharp using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.ML; using Emgu.CV.Structure; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ImageClassification { class Program { static void Main(string[] args) { // Load the training data Matrix<float> trainingData = new Matrix<float>(new float[,] { { 100, 200 }, { 150, 250 }, { 200, 300 }, { 250, 350 }, { 300, 400 }, { 350, 450 }, { 400, 500 }, { 450, 550 } }); // Load the labels for the training data Matrix<float> labels = new Matrix<float>(new float[,] { { 0 }, { 0 }, { 0 }, { 0 }, { 1 }, { 1 }, { 1 }, { 1 } }); // Initialize the SVM model SVM model = new SVM(); // Set the SVM parameters var svmParams = new MCvSVMParams() { KernelType = SVM_KERNEL_TYPE.LINEAR, C = 1, Gamma = 0.5, TermCrit = new MCvTermCriteria(100, 0.00001) }; // Train the SVM model model.Train(trainingData, labels, null, null, svmParams); // Load the test data Matrix<float> testData = new Matrix<float>(new float[,] { { 125, 225 }, { 225, 325 }, { 325, 425 }, { 425, 525 } }); // Predict the labels for the test data Matrix<float> predictedLabels = new Matrix<float>(testData.Rows, 1); model.Predict(testData, predictedLabels); // Output the predicted labels Console.WriteLine("Predicted Labels:"); for (int i = 0; i < predictedLabels.Rows; i++) { Console.WriteLine(predictedLabels[i, 0]); } // Wait for user input Console.ReadLine(); } } } ``` 上面的代码演示了如何使用EMGU.CV进行图像分类的训练和测试。在此示例中,我们使用了一个简单的线性SVM模型来预测输入数据的标签。我们首先加载训练数据和标签,然后使用SVM.Train方法训练模型。一旦我们训练好了模型,我们就可以加载测试数据,并使用SVM.Predict方法预测测试数据的标签。最后,我们可以输出预测的标签以进行检查。 需要注意的是,此示例是基于简单的数值输入数据进行训练和测试的,实际上在图像分类中需要使用更复杂的特征提取和预处理方法,以及更大的数据集来训练和测试模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值