图像处理应用程序中的线程 [英] Threads in Image processing applications

查看:86
本文介绍了图像处理应用程序中的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在一个图像处理项目中.由于应用程序的速度,我必须使用线程.在第一个线程中,我获取图像,在第二个线程中,我处理图像.这些必须同时进行.我想我必须使用多个缓冲区.但是如何??您有任何想法或示例代码可以实现吗?

最好的问候...

Hello everybody,

I''m working in an image processing project. I must use thread because of application''s speed. In first thread, I take my image and in second thread I process my image. These must be simultaneously. I think I must use multiple buffer. But how??? Do you have any idea or sample code to do it.

Best regards...

推荐答案

我已经测量过.
I have measured.
  typedef struct
  {
    BITMAPINFO*      pBmp;
    unsigned char*  lpBytes;
    unsigned int    cbBytes;
    unsigned int    firstline;
    unsigned int    linecount;
  } PROCESS_STRUCT;
  static unsigned long FAR PASCAL  __process24(void* p)
  {
    PROCESS_STRUCT*  ps = (PROCESS_STRUCT*)p;
    unsigned int    bpl = ((ps->pBmp->bmiHeader.biWidth*3)+3) & ~3;
    unsigned char*  lpl = ps->lpBytes + bpl * ps->firstline;
    unsigned char*  lpp;
    unsigned int    x,y;
    for(y=0;y<ps->linecount;y++)
    {
      lpp = lpl;
      for(x=0;(int)x<ps->pBmp->bmiHeader.biWidth;x++,lpp+=3)
      {
        lpp[0] = 
        lpp[1] = 
        lpp[2] = (unsigned char)(((unsigned int)lpp[0]+(unsigned int)lpp[1]+(unsigned int)lpp[2])/3);
      }
      lpl += bpl;
    }
    return 0;
  }
  static unsigned long FAR PASCAL  __process32(void* p)
  {
    PROCESS_STRUCT*  ps = (PROCESS_STRUCT*)p;
    unsigned int    bpl = ps->pBmp->bmiHeader.biWidth * 4;
    unsigned char*  lpl = ps->lpBytes + bpl * ps->firstline;
    unsigned char*  lpp;
    unsigned int    x,y;
    for(y=0;y<ps->linecount;y++)
    {
      lpp = lpl;
      for(x=0;(int)x<ps->pBmp->bmiHeader.biWidth;x++,lpp+=4)
      {
        lpp[0] = 
        lpp[1] = 
        lpp[2] = (unsigned char)(((unsigned int)lpp[0]+(unsigned int)lpp[1]+(unsigned int)lpp[2])/3);
      }
      lpl += bpl;
    }
    return 0;
  }

  static void __ProcessBitmap(const unsigned int nthreads,BITMAPINFO* pBmp,unsigned char* lpBytes,unsigned int cbBytes)
  {
    HANDLE              ah[32]; // number of processors == best time
    unsigned int        ih;
    unsigned long        tid;
    PROCESS_STRUCT      pst[32];
    unsigned int        lpp = pBmp->bmiHeader.biHeight / nthreads;

    ASSERT((0<nthreads) && ((sizeof(ah)/sizeof(ah[0]))>=nthreads));

    switch(pBmp->bmiHeader.biBitCount)
    {
      case 24:
        pst[0].firstline = 0;
        for(ih=0;ih<nthreads;ih++)
        {
          if((1+ih)==nthreads) pst[ih].linecount = pBmp->bmiHeader.biHeight-pst[ih].firstline;
          else pst[ih].linecount = min(lpp,pBmp->bmiHeader.biHeight-pst[ih].firstline);
          pst[ih].pBmp    = pBmp;
          pst[ih].lpBytes = lpBytes;
          pst[ih].cbBytes = cbBytes;
          ah[ih] = CreateThread(0,0,__process24,&pst[ih],0,&tid);
          if((1+ih)<nthreads) pst[ih+1].firstline = pst[ih].firstline + pst[ih].linecount;
        }
        WaitForMultipleObjects(ih,ah,1,INFINITE);
      break;
      case 32:
        pst[0].firstline = 0;
        for(ih=0;ih<nthreads;ih++)
        {
          if((1+ih)==nthreads) pst[ih].linecount = pBmp->bmiHeader.biHeight-pst[ih].firstline;
          else pst[ih].linecount = min(lpp,pBmp->bmiHeader.biHeight-pst[ih].firstline);
          pst[ih].pBmp    = pBmp;
          pst[ih].lpBytes = lpBytes;
          pst[ih].cbBytes = cbBytes;
          ah[ih] = CreateThread(0,0,__process32,&pst[ih],0,&tid);
          if((1+ih)<nthreads) pst[ih+1].firstline = pst[ih].firstline + pst[ih].linecount;
        }
        WaitForMultipleObjects(ih,ah,1,INFINITE);
      break;
    }
  }


void ProcessImage(BITMAPINFO* pBmp,unsigned char* lpBytes,unsigned int cbBytes)
{
  // start time measurement
  __ProcessBitmap(_max_threads,pBmp,lpBytes,cbBytes);
  // stop time measurement and add to statistics
}


_max_threads的结果:


the results for _max_threads:

  // 1 == 2.64 ms
  // 2 == 1.80 ms
  // 3 == 2.18 ms
  // 4 == 2.00 ms


网络摄像头的位图是32位640x480.
在具有2个物理核心的i3上.
问候.


bitmap is 32 bit 640x480 from the webcam.
on i3 that has 2 physical cores.
Regards.


一种简单的实现方法...每次有新映像时在堆上分配内存,并将指针传递给处理线程.然后,处理线程在完成处理后需要重新分配内存.您可能将公共访问点设置为图像指针的链接列表,而仅将访问控制设置为互斥体或类似内容.
Simple way of doing it... allocate memory on the heap every time there''s a new image and pass a pointer to the processing thread. The processing thread then needs to deallocate the memory after its done with it. You can probably have the common access point be a linked list of image pointers and just have the access control be a mutex or something similar.


这篇关于图像处理应用程序中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆