Hello Mat

 找回密码
 立即注册
查看: 580|回复: 0

Halide与opencv混编

[复制链接]

11

主题

12

帖子

40

金钱

版主

Rank: 7Rank: 7Rank: 7

积分
80
发表于 2024-4-16 20:26:35 | 显示全部楼层 |阅读模式
Halide与opencv混编:
【1】C#代码
  1. // See https://aka.ms/new-console-template for more information
  2. using OpenCvSharp;
  3. using System.Runtime.InteropServices;
  4. using ConsoleApp1;
  5. using System;

  6. void printhello()
  7. {
  8.     Console.WriteLine("Hello, World!");
  9. }
  10. printhello();
  11. int a = 42;
  12. int b = 119;
  13. int c = a + b;
  14. Console.WriteLine(c);

  15. string imagepath = @"C:\\AIOCRProjects\\Image\\1 (1).jpg";
  16. Mat src = Cv2.ImRead(imagepath, ImreadModes.Grayscale);
  17. // 获取灰度图像的宽度、高度和每像素的字节数  
  18. int width = src.Cols;
  19. int height = src.Rows;
  20. int bytesPerPixel = src.ElemSize1();

  21. // 计算图像数据的总字节大小  
  22. CallAIDLL.BlurHal2_test(src.Width, src.Height, src.Channels(), src.Data);
  23. // 锁定Mat对象以获取其数据指针
  24. Console.WriteLine(src.Channels());
  25. Console.WriteLine("done");

  26. Mat ColorMat = Cv2.ImRead(imagepath, ImreadModes.Color);
  27. var bytes = new byte[ColorMat.Total() * 3];//这里必须乘以通道数,不然数组越界,也可以用w*h*c,差不多
  28. //var bytes = new byte[ColorMat.Width * ColorMat.Height * ColorMat.Channels()];//这里必须乘以通道数,不然数组越界,也可以用w*h*c,差不多
  29. //Console.WriteLine(ColorMat.Total() * 3);
  30. //Console.WriteLine(ColorMat.Width * ColorMat.Height * ColorMat.Channels());
  31. Marshal.Copy(ColorMat.Data, bytes, 0, bytes.Length);
  32. IntPtr p0 = Marshal.AllocHGlobal(ColorMat.Width * ColorMat.Height * ColorMat.Channels());
  33. Marshal.Copy(bytes, 0, p0, ColorMat.Width * ColorMat.Height * ColorMat.Channels());
  34. Console.WriteLine(ColorMat.Channels());
  35. Console.WriteLine(ColorMat.Width);
  36. Console.WriteLine(ColorMat.Height);
  37. CallAIDLL.BlurHal2_test(ColorMat.Width, ColorMat.Height, ColorMat.Channels(), p0);

  38. Console.ReadKey();
复制代码
【2】C++代码:
  1. Halide::Func AIpredict::BlurWiti3(Halide::Param<int32_t> aWidth, Halide::Param<int32_t> aHeight, Halide::Param<int32_t> aChannel, Halide::ImageParam input)
  2. {
  3.         //Mat inputMat = Intptr2Mat(aWidth, aHeight, aChannel, aBytes); // 灰度图像
  4.         //Halide::Buffer<uint8_t> input = Mat2Buffer(inputMat);         // GrayImage
  5.         Halide::Var x, y, c;
  6.         Halide::Func clamped;
  7.         Halide::Expr clamped_x = Halide::clamp(x, 0, input.width() - 1);
  8.         Halide::Expr clamped_y = Halide::clamp(y, 0, input.height() - 1);
  9.         //std::cout << "image cols: " << input.dimensions() << std::endl;
  10.         //if (aChannel.get() == 3)
  11.         //{
  12.                 input.dim(0).set_stride(3);     // stride in dimension 0 (x) is three
  13.                 input.dim(2).set_stride(1);     // stride in dimension 2 (c) is one
  14.                 input.dim(2).set_bounds(0, 3);  // Dimension 2 (c) starts at 0 and has extent 3.
  15.         //}
  16.         clamped(x, y, c) = input(clamped_x, clamped_y, c);
  17.         Halide::Func input_16;
  18.         input_16(x, y, c) = cast<uint16_t>(clamped(x, y, c));
  19.         Halide::Func blur_x;
  20.         blur_x(x, y, c) = (input_16(x - 1, y, c) + 2 * input_16(x, y, c) + input_16(x + 1, y, c)) / 4;
  21.         Halide::Func blur_y;
  22.         blur_y(x, y, c) = (blur_x(x, y - 1, c) + 2 * blur_x(x, y, c) + blur_x(x, y + 1, c)) / 4;
  23.         //Buffer<uint8_t> result = output.realize({ input.width(), input.height() });  // GrayImage
  24.         //Mat output_image_last = Buffer2Mat(result);
  25.         //return (Mat2UnsignedChar(output_image_last));

  26.         // Schedule
  27.         //blur_x.compute_root().vectorize(x, 8).parallel(y);
  28.         //blur_y.compute_at(blur_x, y).vectorize(x, 8);

  29.         Halide::Func output;
  30.         output(x, y, c) = cast<uint8_t>(blur_y(x, y, c));

  31.         output.compile_jit();
  32.         return output;
  33. }
  34. Halide::Func AIpredict::BlurWiti2(Halide::Param<int32_t> aWidth, Halide::Param<int32_t> aHeight, Halide::Param<int32_t> aChannel, Halide::ImageParam input)
  35. {
  36.         //Mat inputMat = Intptr2Mat(aWidth, aHeight, aChannel, aBytes); // 灰度图像
  37.         //Halide::Buffer<uint8_t> input = Mat2Buffer(inputMat);         // GrayImage
  38.         Halide::Var x, y, c;
  39.         Halide::Func clamped;
  40.         Halide::Expr clamped_x = Halide::clamp(x, 0, input.width() - 1);
  41.         Halide::Expr clamped_y = Halide::clamp(y, 0, input.height() - 1);
  42.         //std::cout << "image cols: " << input.dimensions() << std::endl;
  43.         //if (aChannel.get() == 3)
  44.         //{
  45.         //input.dim(0).set_stride(3);     // stride in dimension 0 (x) is three
  46.         //input.dim(2).set_stride(1);     // stride in dimension 2 (c) is one
  47.         //input.dim(2).set_bounds(0, 3);  // Dimension 2 (c) starts at 0 and has extent 3.
  48.         //}
  49.         clamped(x, y, c) = input(clamped_x, clamped_y, c);
  50.         Halide::Func input_16;
  51.         input_16(x, y, c) = cast<uint16_t>(clamped(x, y, c));
  52.         Halide::Func blur_x;
  53.         blur_x(x, y, c) = (input_16(x - 1, y, c) + 2 * input_16(x, y, c) + input_16(x + 1, y, c)) / 4;
  54.         Halide::Func blur_y;
  55.         blur_y(x, y, c) = (blur_x(x, y - 1, c) + 2 * blur_x(x, y, c) + blur_x(x, y + 1, c)) / 4;
  56.         //Buffer<uint8_t> result = output.realize({ input.width(), input.height() });  // GrayImage
  57.         //Mat output_image_last = Buffer2Mat(result);
  58.         //return (Mat2UnsignedChar(output_image_last));

  59.         // Schedule
  60.         //blur_x.compute_root().vectorize(x, 8).parallel(y);
  61.         //blur_y.compute_at(blur_x, y).vectorize(x, 8);

  62.         Halide::Func output;
  63.         output(x, y, c) = cast<uint8_t>(blur_y(x, y, c));

  64.         output.compile_jit();
  65.         return output;
  66. }

  67. void AIpredict::BlurHal2_test(int aWidth, int aHeight, int aChannel, unsigned char* aBytes)
  68. {
  69.         //cv::Mat inputMat2;
  70.         //inputMat2 = inputMat.clone();
  71.         Halide::Param<int32_t> WidthHal;
  72.         Halide::Param<int32_t> HeightHal;
  73.         Halide::Param<int32_t> ChannelHal;
  74.         Halide::ImageParam GrayHal(type_of<uint8_t>(), 3);  // third channel = 1
  75.         //Halide::ImageParam RGBHal(type_of<uint8_t>(), 3); // third channel = 3
  76.         if (aChannel == 1)
  77.         {
  78.                 BlurWiti_Pipeline = BlurWiti2(WidthHal, HeightHal, ChannelHal, GrayHal);
  79.         }
  80.         else
  81.         {
  82.                 BlurWiti_Pipeline = BlurWiti3(WidthHal, HeightHal, ChannelHal, GrayHal);
  83.         }

  84.         cv::Mat inputMat;
  85.         inputMat = Intptr2Mat(aWidth, aHeight, aChannel, aBytes);
  86.         /*std::cout << "image channel: " << inputMat.channels() << std::endl;
  87.         std::cout << "image dims: " << inputMat.dims << std::endl;
  88.         cv::imwrite("D:\\230.jpg", inputMat);
  89.         cv::Mat inputMat2;
  90.         inputMat2 = cv::imread("C:\\AIOCRProjects\\Image\\1 (1).jpg", 1);
  91.         std::cout << "image channel: " << inputMat2.channels() << std::endl;
  92.         std::cout << "image dims: " << inputMat2.dims << std::endl;*/

  93.         Halide::Buffer<uint8_t> input = Mat2Buffer(inputMat); // GrayImage, default dimension = 3
  94.         //Halide::Buffer<uint8_t> input = Halide::Runtime::Buffer<uint8_t>::make_interleaved(aBytes, aWidth, aHeight, aChannel);
  95.         //Halide::Buffer<uint8_t> input2 = load_image("C:\\AIOCRProjects\\Image\\1 (1).jpg"); // GrayImage
  96.         //std::cout << "image channel: " << input.channels() << std::endl;
  97.         //std::cout << "image dimensions: " << input.dimensions() << std::endl;
  98.         //std::cout << "image channel: " << input2.channels() << std::endl;
  99.         //std::cout << "image dimensions: " << input2.dimensions() << std::endl;
  100.         //Halide::Buffer<uint8_t> input3 = input(:, : , 0);
  101.         //Halide::Tools::save_image(input, "D:\\brighter.png");

  102.         //Halide::Buffer<uint8_t> input = load_image("D:\\brighter.png"); // GrayImage
  103.         //input = Halide::Tools::load_image("D:\\brighter.png"); // GrayImage

  104.         WidthHal.set(aWidth);
  105.         HeightHal.set(aHeight);
  106.         ChannelHal.set(aChannel);
  107.         GrayHal.set(input);

  108.         //std::cout << "image cols: " << input.width() << std::endl;
  109.         //std::cout << "image rows: " << input.height() << std::endl;
  110.         //std::cout << "image channel: " << input.channels() << std::endl;

  111.         Halide::Buffer<uint8_t> output(aWidth, aHeight, aChannel);
  112.         BlurWiti_Pipeline.realize(output);

  113.         cv::Mat outImage = Buffer2Mat(output);
  114.         cv::imwrite("D:\\23.jpg", outImage);
  115.         //return Buffer2Mat(output);
  116. }
复制代码







回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-5-17 19:34 , Processed in 0.229981 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表