2010-02-25 7 views
-2

나는 픽셀의 데이터를 색상과 비교하기를 원합니다. 그런 다음 컨투어를 찾으려면 등고선의 중심 점을 가져야합니다. 그래서 countourdata를 찾으려면이 방법을 사용하고 있습니다. 이 사항RGB 이미지 데이터에 액세스하는 방법

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++; 
     } 
    } 
} 
+0

이봐, 당신은 "코드 블록"... 같은 코드를 삽입 할 수있는 그것, 그것을 읽을 당신이 – Aishwar

+0

을 수행 할 작업을보기는 어렵다 아마도 자신의 질문을 속이는 : http://stackoverflow.com/questions/2325576/how-to-access-image-data-from-a-rgb-image-3channel-image-in-opencv – erelender

+0

예 - 자체 질문의 사본 http://stackoverflow.com/questions/2325576 이전 질문에 제공된 대답을 읽을 것 같지 않습니다 –

답변

0

같은 것은 화상

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 
픽셀의 RGB 데이터에 액세스하는 어떤 코드 17,451,515,

소스 (약간 수정) : 나는 다음과 같은 코드 구조를 사용 http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00053000000000000000

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)); 
     } 
    } 
} 
0

1) RGB 이미지를 입력으로 사용 2) x = 0, y = 0, w = 프레임 너비, h = 프레임 높이.

void cRecursiveCentroids :: ComputeCentroid (int x, int y, int w, int h, IplImage * 데이터, bool splitOnUpDown, int 수준, INT 아이디, 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 

}

관련 문제