1 目標
C#調用C++的dll,調節圖片對比度亮度
2 生成C++的dll
2.1建立C++ dll工程
用VS2015,建立C++的dll工程。具體步驟見下圖。
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文件如下圖:
C#的debug文件夾如下圖:
3.1建立C#工程
建立C#工程,具體如下圖。
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.要把圖片地址改爲自己電腦裏圖片的地址。