close

C# call a c++ method, which is called GetSourceImage(…)

The following is the way how we get the bitmap data by copying srcBmpData to dstBmpdata,

and showing them on a pictureBox.

The copy sequence is just the same as OpenCV BGR, it looks as though we create a Format32bppArgb image.

,

   1: this.Invoke((MethodInvoker)delegate
   2: {
   3:     int width, height, channel, step;
   4:     unsafe
   5:     {
   6:         byte* srcBmpData = cVideo.GetSourceImage(&width, &height, &channel, &step);
   7:         Result_lbl.Text = $"{width},{height},{channel},{step}";
   8:         Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
   9:         var data = bmp.LockBits(new Rectangle(0, 0, width, height),
  10:                                 System.Drawing.Imaging.ImageLockMode.WriteOnly,
  11:                                 System.Drawing.Imaging.PixelFormat.Format32bppRgb);
  12:         byte* dstBmpData = (byte*)data.Scan0.ToPointer();
  13:         int hei = height;
  14:         int wid = width;
  15:         int stepsize = step;
  16:      
  17:  
  18:         for (int y = 0; y < height; y++)
  19:         {
  20:             for (int x = 0; x < width; x++)
  21:             {
  22:                 dstBmpData[y * width * 4 + 4 * x] = srcBmpData[y * step + channel * x];
  23:                 dstBmpData[y * width * 4 + 4 * x + 1] = srcBmpData[y * step + channel * x + 1];
  24:                 dstBmpData[y * width * 4 + 4 * x + 2] = srcBmpData[y * step + channel * x + 2];
  25:             }
  26:         }
  27:         bmp.UnlockBits(data);
  28:         pictureBox1.Image = bmp;
  29:     }

30: });


c++ GetSourceImage method

   1: uchar* CVideo::GetSourceImage(int &width, int &height, int &channel, int &step)
   2: {
   3:     uchar* data = img.data;
   4:     
   5:     width   = img.size().width;
   6:     height  = img.size().height;
   7:     channel = img.channels();
   8:     step    = img.step;
   9:     return data;
  10: }

Another parrallel for-loop version

   1: Parallel.For(0, hei, y =>
   2: {
   3:     Parallel.For(0, wid, x =>
   4:     {
   5:         dstBmpData[y * wid * 4 + 4 * x] = srcBmpData[y * stepsize + 3 * x];
   6:         dstBmpData[y * wid * 4 + 4 * x + 1] = srcBmpData[y * stepsize + 3 * x + 1];
   7:         dstBmpData[y * wid * 4 + 4 * x + 2] = srcBmpData[y * stepsize + 3 * x + 2];
   8:     });
   9: });


In fact, the best way to show a bitmap is to use OpenCV imshow command rather than transfer the bitmap data to the windows form. Because OpenCV utilize game engine which has lower CPU loading and it can display an image more effectively.


   1: this.Invoke((MethodInvoker)delegate
   2: {
   3:     cVideo.ShowLiveImage(0);
   4: });

c++

   1: void CVideo::ShowLiveImage(int cameraIndex)
   2: {
   3:     char labelCamera[4];
   4:     sprintf(labelCamera, "%d", cameraIndex);
   5:     namedWindow(labelCamera, WindowFlags::WINDOW_KEEPRATIO);
   6:     if(!img.empty())
   7:     imshow(labelCamera, img);
   8:     waitKey(1);
   9: }
arrow
arrow
    全站熱搜

    me1237guy 發表在 痞客邦 留言(0) 人氣()