标签:style sha logo ptr add 获取 ribbon ice family
Bitmap^ GOClrClass::testMethod(cli::array<unsigned char>^ pCBuf1)
{
pin_ptr<System::Byte> p1 = &pCBuf1[0];
unsigned char* pby1 = p1;
cv::Mat img_data1(pCBuf1->Length,1,CV_8U,pby1);
cv::Mat img_object = cv::imdecode(img_data1,IMREAD_UNCHANGED);//获得数据到img_object中去
//////////////////////////////////处理过程///////////////////////////////////////
cvtColor(img_object,img_object,40);
/////////////////////////////////////////////////////////////////////////////////
Bitmap^ bb = MatToBitmap(img_object);
if (!img_object.data)
return nullptr;
std::vector<uchar> buf;
cv::imencode(".jpg", img_object, buf);
return bb;
}
System::Drawing::Bitmap^ MatToBitmap(const cv::Mat& img)
{
if (img.type() != CV_8UC3)
{
throw gcnew NotSupportedException("Only images of type CV_8UC3 are supported for conversion to Bitmap");
}
//create the bitmap and get the pointer to the data
PixelFormat fmt(PixelFormat::Format24bppRgb);
Bitmap ^bmpimg = gcnew Bitmap(img.cols, img.rows, fmt);
BitmapData ^data = bmpimg->LockBits(System::Drawing::Rectangle(0, 0, img.cols, img.rows), ImageLockMode::WriteOnly, fmt);
//byte *dstData = reinterpret_cast<byte*>(data->Scan0.ToPointer());
Byte *dstData = reinterpret_cast<Byte*>(data->Scan0.ToPointer());
unsigned char *srcData = img.data;
for (int row = 0; row < data->Height; ++row)
{
memcpy(reinterpret_cast<void*>(&dstData[row*data->Stride]), reinterpret_cast<void*>(&srcData[row*img.step]), img.cols*img.channels());
}
bmpimg->UnlockBits(data);
return bmpimg;
}
Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip);
// If the image is upsidedown
b.RotateFlip(RotateFlipType.RotateNoneFlipY);
srcImage = b;
if (picPreview.Image != null)
picPreview.Image.Dispose();
//调用clr+opencv图像处理模块
MemoryStream ms = new MemoryStream();
b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.GetBuffer();
Bitmap bitmap = client.testMethod(bytes);
String^ Class1::Method(cli::array<unsigned char>^ pCBuf1)
{
pin_ptr<System::Byte> p1 = &pCBuf1[0];
unsigned char* pby1 = p1;
cv::Mat img_data1(pCBuf1->Length,1,CV_8U,pby1);
cv::Mat img_object = cv::imdecode(img_data1,IMREAD_UNCHANGED);
//////////////////////////////////处理过程/////////
cvtColor(img_object,img_object,40);
/////////////////////////////////////////////////////////////////////////////////
if (!img_object.data)
return nullptr;
//获得目录,保存文件
cv::imwrite("c:/Method.jpg",img_object);
return "c:/Method.jpg";
}
String^ Class1::Method2(cli::array<unsigned char>^ pCBuf1)
{
pin_ptr<System::Byte> p1 = &pCBuf1[0];
unsigned char* pby1 = p1;
cv::Mat img_data1(pCBuf1->Length,1,CV_8U,pby1);
cv::Mat img_object = cv::imdecode(img_data1,IMREAD_UNCHANGED);
//////////////////////////////////处理过程///////////////////////
cvtColor(img_object,img_object,6);
/////////////////////////////////////////////////////////////////////////////////
if (!img_object.data)
return nullptr;
//获得目录,保存文件
cv::imwrite("c:/Method2.jpg",img_object);
return "c:/Method2.jpg";
}
class GOCsharpHelper
{
Class1 client = new Class1();
string strResult1 = null;
string strResult2 = null;
//输入参数是string或bitmap
public Bitmap ImageProcess(string ImagePath){
Image ImageTemp = Bitmap.FromFile(ImagePath);
return ImageProcess(ImageTemp);
}
//输出结果是bitmap
public Bitmap ImageProcess(Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.GetBuffer();
strResult1 = client.Method(bytes);
Image ImageResult = Bitmap.FromFile(strResult1);
return (Bitmap)ImageResult;
}
public Bitmap ImageProcess2(string ImagePath)
{
Image ImageTemp = Bitmap.FromFile(ImagePath);
return ImageProcess2(ImageTemp);
}
//输出结果是bitmap
public Bitmap ImageProcess2(Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.GetBuffer();
strResult2 = client.Method2(bytes);
Image ImageResult = Bitmap.FromFile(strResult2);
return (Bitmap)ImageResult;
}
public void Clear()
{
if (File.Exists(strResult1))
File.Delete(strResult1);
if (File.Exists(strResult2))
File.Delete(strResult2);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
pictureBox1.Image.Dispose();
if (pictureBox2.Image != null)
pictureBox2.Image.Dispose();
Image image1 = gocsharphelper.ImageProcess(" E:/sandbox/logo.jpg");
pictureBox1.Image = image1;
Image image2 = gocsharphelper.ImageProcess2("E:/sandbox/lena.jpg");
pictureBox2.Image = image2;
}
【4opencv】为基于OpenCV的图像处理程序编写界面—关于QT\MFC\CSharp的选择以及GOCW的介绍
标签:style sha logo ptr add 获取 ribbon ice family
原文地址:https://www.cnblogs.com/jsxyhelu/p/11437489.html