【问题标题】:How to access a RGB image data如何访问 RGB 图像数据
【发布时间】:2010-02-25 06:56:43
【问题描述】:

我希望将像素处的数据与颜色进行比较,然后我想找到轮廓然后取轮廓的质心点,所以我使用这样的方法来查找 countourdata 我在这个语句中是否错了

int pos = i * w * Channels + j; //channels is 3 as rgb 
        // if any data exists
        if (data->imageData[pos]>0)

代码是这样的

for (int i = x; i < x+h; i++) //height of frame pixels
{
    for (int j = y; j < y+w; j++)//width of frame pixels
    {
        int pos = i * w * Channels + j; //channels is 3 as rgb 
        // if any data exists
        if (data->imageData[pos]>0) //Taking data (here is the problem how to take)
        {
            xPos += j;
            yPos += i;
            nPix++;
        }
    }
}

【问题讨论】:

  • 嘿,你能不能把代码作为一个“代码块”...它很难阅读,看看你想做什么
  • 可能是自己的问题:stackoverflow.com/questions/2325576/…
  • 是的 - 重复自己的问题 stackoverflow.com/questions/2325576 并且似乎没有阅读为上一个问题提供的答案
  • 我在答案 3 处发布了我的确切代码,请查看它,你重复了,因为我没有得到确切的答案仍然有疑问
  • 以后不要因为你的老问题没有得到好的答案就开始新的问题。其次,如果回复以添加有关您的问题的信息,您应该编辑您的问题。

标签: c++ c opencv


【解决方案1】:

我使用如下代码结构

/** 
 * @brief Calculate greeness from an RGB image
 *
 * Performs the greeness pixelwise transform on the input image.
 * Greeness is defined as
 * Greeness = 255*G/sqrt(R^2+G^2+B^2)
 * The function assumes that the resolution of the two images are identical.
 *
 * @param imSrc Input RGB image.
 * @param imDst Output grayscale (greeness) image.
 */
void rgbToGreeness( IplImage *imSrc , IplImage* imDst) {
    // Allocate variables
    int tmp_pix;
    uchar * _SrcPtr, * _DstPtr;

    // Iterate over the image line by line
    for(int y = 0 ; y < imSrc->height ; y++ )
    {
        // Locate pointers to the first data element in the current line
        _SrcPtr = ( uchar* )( imSrc->imageData + y * imSrc->widthStep );
        _DstPtr = ( uchar* )( imDst->imageData + y * imDst->widthStep );

        // Iterate over the elements in the current line
        for( int x = 0 ; x < imSrc->width ; x++ )
        {
            //2*G-B-R - Excessive green
            tmp_pix = (int) (255*_SrcPtr[3*x+1]/pow(pow((float)_SrcPtr[3*x],2) + pow((float)_SrcPtr[3*x+1], 2) + pow((float)_SrcPtr[3*x+2], 2), (float) 0.5));

            //If value is larger than 255, set it to 255 and lower than 0 set it to 0
            _DstPtr[x] = (uchar) ( ( tmp_pix < 0 ) ? 0 : ( ( tmp_pix > 255 ) ? 255 : tmp_pix ) );
        }
    }
}

【讨论】:

    【解决方案2】:

    这是一些访问图像中像素的 RGB 数据的代码

    IplImage* img=cvLoadImage(fileName);
    CvScalar s;
    s=cvGet2D(img,i,j); // get the (i,j) pixel value
    s.val[0]=111; // B-channel
    s.val[1]=111; // G-channel
    s.val[2]=111; // R-channel
    cvSet2D(img,i,j,s); // set the (i,j) pixel value
    

    来源(稍作修改):http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00053000000000000000

    【讨论】:

      【解决方案3】:

      这里的要求是我想从轮廓计算质心的确切代码 我的确切代码是这样的 1)以RGB图像为输入 2) x=0,y=0,w=帧的宽度,h=帧的高度。是数据通过

      void cRecursiveCentroids::ComputeCentroid(int x, int y, int w, int h, IplImage *data, bool splitOnUpDown, int level, int id, int addToId){

      if (level == m_Levels-1 ) return;
      int Channels = data->nChannels; // Number of channels
          std::cout << "Channels: " << Channels << "\n"; 
      
      int xPos = 0;
      int yPos = 0;
      int nPix = 0;
      
      
      for (int i = x; i < x+h; i++)                      //Tracing the contour 
      {
          for (int j = y; j < y+w; j++)
          {
                  int pos = i * m_Wid * Channels + j; // Here may be the error i am thinking
                          // if any data exists 
              if (data->imageData[pos]>0)
              {
                  xPos += j;
                                  //std::cout << "xPos: " << xPos << "\n";
                                  yPos += i;
                                 // std::cout << "yPos: " << yPos << "\n"; 
                  nPix++;
                  }
          }
      }
      
      Check = nPix;
      
      if (nPix > 0){                                           // Calculating Position 
      
          xPos = (int)((float)xPos / (float)nPix);
          yPos = (int)((float)yPos / (float)nPix);
          int num = ( id + addToId ) > 16 ? 16 : (id+addToId);
          m_Cent[num].posx    = xPos;
          m_Cent[num].posy    = yPos;
          m_Cent[num].level   = level;
      
                  splitOnUpDown = !splitOnUpDown;
          level = level+1;
          if (splitOnUpDown)                   //Recursive calling for centroids
          {
              id *= 2;
              ComputeCentroid(x,y,w,(yPos - y), data,  splitOnUpDown, level, id, addToId);
              ComputeCentroid(x,yPos,w,h-(yPos-y),  data,  splitOnUpDown, level, id+1, addToId);
          } else {
              id *= 2;
              ComputeCentroid(x,y,(xPos-x),h,  data,  splitOnUpDown, level, id, addToId);
              ComputeCentroid(xPos,y,w - (xPos-x),h,   data,  splitOnUpDown, level, id+1, addToId);
          }
      
      }
      
      DrawCentroidPoints();                               //Draw Centroid Points
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-28
        相关资源
        最近更新 更多