C#调用C++的dll,调节图片对比度亮度

1 目标

C#调用C++的dll,调节图片对比度亮度

2 生成C++的dll

2.1建立C++ dll工程

用VS2015,建立C++的dll工程。具体步骤见下图。
Image 3.png
Image 2.png
Image 5.png

2.2代码

头文件:CPlusPlusTest.h
dll的API函数:opencvtest

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

// 下列 ifdef 块是创建使从 DLL 导出更简单的
// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 CPLUSPLUSTEST_EXPORTS
// 符号编译的。在使用此 DLL 的
// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
// CPLUSPLUSTEST_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
// 符号视为是被导出的。
#ifdef CPLUSPLUSTEST_EXPORTS
#define CPLUSPLUSTEST_API __declspec(dllexport)
extern "C"
{
    CPLUSPLUSTEST_API int opencvtest(unsigned char* src, int w, int h, int channel, float contrastValue, int brightValue);
}
#else
#define CPLUSPLUSTEST_API __declspec(dllimport)
#endif

// 此类是从 CPlusPlusTest.dll 导出的
class CPLUSPLUSTEST_API CCPlusPlusTest {
public:
	CCPlusPlusTest(void);
	// TODO:  在此添加您的方法。
    
};

extern CPLUSPLUSTEST_API int nCPlusPlusTest;

CPLUSPLUSTEST_API int fnCPlusPlusTest(void);

源文件:CPlusPlusTest.cpp

// CPlusPlusTest.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "CPlusPlusTest.h"
//-----------------------------------【命名空间声明部分】---------------------------------------
//	描述:包含程序所使用的命名空间
//-----------------------------------------------------------------------------------------------   
using namespace std;
using namespace cv;

int CPLUSPLUSTEST_API opencvtest(unsigned char* src, int w, int h, int channel, float contrastValue, int brightValue)
{
    if (!src)
    {
        return 0;
    }
    int format = CV_8UC3;
    switch (channel)
    {
    case 1:
        format = CV_8UC1;
        break;
    case 2:
        format = CV_8UC2;
        break;
    case 3:
        format = CV_8UC3;
        break;
    default:
        return 0;
        break;
    }

    Mat img(h, w, format, src);

    // 三个for循环,执行运算 g_dstImage(i,j) = a*g_srcImage(i,j) + b
    for (int y = 0; y < img.rows; y++)
    {
        for (int x = 0; x < img.cols; x++)
        {
            for (int c = 0; c < channel; c++)
            {
                switch (channel)
                {
                case 1:
                    img.at<uchar>(y, x) = saturate_cast<uchar>((contrastValue)*(img.at<uchar>(y, x)) + brightValue);
                    break;
                case 2:
                    img.at<Vec2b>(y, x)[c] = saturate_cast<uchar>((contrastValue)*(img.at<Vec2b>(y, x)[c]) + brightValue);
                    break;
                case 3:
                    img.at<Vec3b>(y, x)[c] = saturate_cast<uchar>((contrastValue)*(img.at<Vec3b>(y, x)[c]) + brightValue);
                    break;
                default:
                    break;
                }
            }
        }
    }

    return 1;
}

// 这是导出变量的一个示例
CPLUSPLUSTEST_API int nCPlusPlusTest = 0;

// 这是导出函数的一个示例。
CPLUSPLUSTEST_API int fnCPlusPlusTest(void)
{
    return 42;
}

// 这是已导出类的构造函数。
// 有关类定义的信息,请参阅 CPlusPlusTest.h
CCPlusPlusTest::CCPlusPlusTest()
{
    return;
}

3. C#调用C++的dll

将C++生成的dll复制粘贴到C#的debug文件夹中。
C++生成的dll文件如下图:
Image 3.png
C#的debug文件夹如下图:
Image 2.png

3.1建立C#工程

建立C#工程,具体如下图。
Image 6.png

Image 8.png

Image 10.png

Image 11.png

Image 12.png

Image 14.png

Image 15.png

3.2代码

文件:Form1.Designer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace CShartTest
{
    partial class Form1
    {
        [DllImport("CPlusPlusTest.dll", EntryPoint = "opencvtest", SetLastError = true, CharSet = CharSet.Ansi)]
        public extern static int opencvtest(byte[] src, int w, int h, int channel, float contrastValue, int brightValue);

        Bitmap bmp;

        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            bmp = new Bitmap("E:\\VS2015 OpenCV3\\CSharp\\2.jpg");
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(128, 5);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(666, -4);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(31, 34);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(368, 243);
            this.pictureBox1.TabIndex = 2;
            this.pictureBox1.TabStop = false;
            
            this.pictureBox1.Image = bmp;
            // 
            // pictureBox2
            // 
            this.pictureBox2.Location = new System.Drawing.Point(488, 34);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(344, 243);
            this.pictureBox2.TabIndex = 3;
            this.pictureBox2.TabStop = false;
            int stride;
            byte[] source = GetBGRValues(bmp, out stride);
            int channel = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
            // 在原来的基础上,对比度和亮度加多少
            opencvtest(source, bmp.Width, bmp.Height, channel, (float)(0.3), 0); //C++ opencv对内存操作
            this.pictureBox2.Image = getBitmapByBytes(source, bmp.Width, bmp.Height, stride, bmp.PixelFormat);  //从byte[]数组中获取到的图像已经发生对比度,亮度的变换,说明成功

            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(881, 321);
            this.Controls.Add(this.pictureBox2);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            this.ResumeLayout(false);

        }

        /// <summary>
        /// 生成灰度图的调色板
        /// </summary>
        /// <returns></returns>
        public static ColorPalette getGrayPalette()
        {
            // 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度 
            ColorPalette tempPalette;
            using (Bitmap tempBmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format8bppIndexed))
            {
                tempPalette = tempBmp.Palette;
            }
            for (int i = 0; i < 256; i++)
            {
                tempPalette.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
            }
            return tempPalette;
        }

        /// <summary>
        /// 从图像Byte数组获取Bitmap
        /// </summary>
        /// <param name="src"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <param name="stride"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static Bitmap getBitmapByBytes(byte[] src, int w, int h, int stride, PixelFormat format)
        {
            IntPtr ptr = getBytesPtr(src);

            int rowBytes = w * Image.GetPixelFormatSize(format) / 8;
            byte[] rgbValues = new byte[stride * h];
            for (var i = 0; i < h; i++)
            {
                //int len = stride - (stride-rowBytes);
                Marshal.Copy(ptr, rgbValues, i * stride, rowBytes);   //对齐
                ptr += rowBytes; // next row
            }
            //Marshal.FreeHGlobal(ptr);
            ptr = getBytesPtr(rgbValues);

            Bitmap bitmap = new Bitmap(w, h, stride, format, ptr);
            if (format == PixelFormat.Format8bppIndexed)
            {
                //bitmap.Palette = getGrayPalette();   //如果是单通道灰度图,默认的bitmap调色板不对,需要手动设置
            }
            return bitmap;
        }

        /// <summary>
        /// 获取bytes数组的头指针,非托管
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static IntPtr getBytesPtr(byte[] bytes)
        {
            GCHandle hObject = GCHandle.Alloc(bytes, GCHandleType.Pinned);
            return hObject.AddrOfPinnedObject();
        }

        /// <summary>
        /// 获取bitmap的byte数据,并输出stride
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="stride"></param>
        /// <returns></returns>
        public static byte[] GetBGRValues(Bitmap bmp, out int stride)
        {
            var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            var bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
            stride = bmpData.Stride;
            //int channel = bmpData.Stride / bmp.Width; 
            var rowBytes = bmpData.Width * Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
            var imgBytes = bmp.Height * rowBytes;
            byte[] rgbValues = new byte[imgBytes];
            IntPtr ptr = bmpData.Scan0;
            for (var i = 0; i < bmp.Height; i++)
            {
                Marshal.Copy(ptr, rgbValues, i * rowBytes, rowBytes);   //对齐
                ptr += bmpData.Stride; // next row
            }
            bmp.UnlockBits(bmpData);

            return rgbValues;

        }
        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.PictureBox pictureBox2;
    }
}

总结

1.上面的代码,我把整个头文件和源文件的代码都粘贴上去了,其中有很多是建立工程时自动生成的。
2.要把图片地址改为自己电脑里图片的地址。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值