jsyz07935 于 2015.06.05 00:22 提问

c#和matlab混合编程时遇到了一点问题，求帮助

4个回答

Trent1985   2015.06.05 09:38

jsyz07935   2015.06.05 00:23

function [J]=defog(img)

kenlRatio = .01;
minAtomsLight = 240;
%figure,imshow(uint8(img)), title('src');

sz=size(img);

w=sz(2);

h=sz(1);

dc = zeros(h,w);

for y=1:h

for x=1:w

dc(y,x) = min(img(y,x,:));

end

end

winodwsize = floor(max([3, w*kenlRatio, h*kenlRatio]));

dc2 = minfilt2(dc, [winodwsize,winodwsize]);

dc2(h,w)=0;

t = 255 - dc2;

t_d=double(t)/255;

A = min([minAtomsLight, max(max(dc2))]);

J = zeros(h,w,3);

img_d = double(img);

r = winodwsize*4;
eps = 10^-6;

filtered = guidedfilter_color((double(img))/255, t_d, r, eps);

t_d = filtered;

J(:,:,1) = (img_d(:,:,1) - (1-0.95*t_d)*A)./t_d;

J(:,:,2) = (img_d(:,:,2) - (1-0.95*t_d)*A)./t_d;

J(:,:,3) = (img_d(:,:,3) - (1-0.95*t_d)*A)./t_d;

J=enhancement(J);

img_d(1,3,1)

J=uint8(J);

end

jsyz07935   2015.06.05 00:23

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using MathWorks.MATLAB.NET.Arrays;
using defog;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(@"C:\Users\User\Desktop\sample.bmp");
byte[, ,] myImage = ImageToBytes(bm);

defog.Class1 output = new defog.Class1();
MWNumericArray img = new MWNumericArray(myImage);
MWNumericArray result = (MWNumericArray)output.defog(img);
MessageBox.Show("succeed");
DisposeImage(myImage, bm);
pictureBox1.Image = bm;

}

private byte[,,] ImageToBytes(Bitmap img)
{
Rectangle lockRect = new Rectangle(0, 0, img.Width, img.Height);
BitmapData imgData = img.LockBits(lockRect, ImageLockMode.ReadOnly, img.PixelFormat);

byte[,] rband = new byte[img.Height, img.Width]; // 彩色图片的R、G、B三层分别构造一个二维数组
byte[,] gband = new byte[img.Height, img.Width];
byte[,] bband = new byte[img.Height, img.Width];
int rowOffset = imgData.Stride - img.Width * 3;

unsafe
{
byte* imgPtr = (byte*)imgData.Scan0.ToPointer();

for (int i = 0; i < img.Height; ++i)
{
for (int j = 0; j < img.Width; ++j)
{
rband[i, j] = imgPtr[2]; // 每个像素的指针是按BGR的顺序存储的
gband[i, j] = imgPtr[1];
bband[i, j] = imgPtr[0];

imgPtr += 3; // 偏移一个像素
}
imgPtr += rowOffset; // 偏移到下一行
}
}

img.UnlockBits(imgData);
byte[, ,] myImage = new byte[img.Height, img.Width, 3];

for(int i=0;i<img.Height;i++)
{
for(int j=0;j<img.Width;j++)
{
myImage[i, j, 0] = rband[i, j];
myImage[i, j, 1] = gband[i, j];
myImage[i, j, 2] = bband[i, j];
}
}

return myImage;
}

private void DisposeImage(byte [,,] myImage,Bitmap img)
{
Rectangle lockRect = new Rectangle(0, 0, img.Width, img.Height);
BitmapData imgData = img.LockBits(lockRect, ImageLockMode.ReadOnly, img.PixelFormat);

int rowOffset = imgData.Stride - img.Width * 3;

unsafe
{
byte* imgPtr = (byte*)imgData.Scan0.ToPointer();

for (int i = 0; i < img.Height; ++i)
{
for (int j = 0; j < img.Width; ++j)
{
imgPtr[2] = myImage[i, j, 0];
imgPtr[1] = myImage[i, j, 1];
imgPtr[0] = myImage[i, j, 2];

imgPtr += 3; // 偏移一个像素
}
imgPtr += rowOffset; // 偏移到下一行
}
}
img.UnlockBits(imgData);

}
}
}

qq_24013105   2015.06.05 09:45