Screwy OpenCV Manipulations

I’ve been playing more with OpenCV and I think I’m missing something. I can’t do any manipulations on the image data without really screwing it up. The only thing that doesn’t seem to wash out the data out is moving pixels around without changing them. Not sure what I’m missing. Here’s the few different manipulations and what they look like when they wash out.

Monochrome

1
2
3
4
5
6
7
8
9
10
11
for(int i = 0; i < frame->height; i++) {
  int offset = i*frame->width*3;
  for(int j = 0; j < frame->width; j++) {
    uchar temp = frame->imageData[offset+(j*3)]*0.114
                    + frame->imageData[offset+(j*3)+1]*0.587
                    + frame->imageData[offset+(j*3)+2]*0.299;
    frame->imageData[offset+(j*3)] = temp;
    frame->imageData[offset+(j*3)+1] = temp;
    frame->imageData[offset+(j*3)+2] = temp;
  }
}



Memory
This one just keeps five frames and then adds them in to create a faded composite, should be simple.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if(0 == memory_frameCounter)
  memory_frames[0] = cvCloneImage(frame);
else if(2 == memory_frameCounter)
  memory_frames[1] = cvCloneImage(frame);
else if(4 == memory_frameCounter)
  memory_frames[2] = cvCloneImage(frame);
else if(6 == memory_frameCounter)
  memory_frames[3] = cvCloneImage(frame);
else if(8 == memory_frameCounter)
  memory_frames[4] = cvCloneImage(frame);
else if(10 <= memory_frameCounter)
  memory_frameCounter = -1;
 
memory_frameCounter++;
 
for(int i = 0; i < frame->height; i++) {
  for(int j =0; j < frame->width*3; j++) {
    memory_agg = frame->imageData[(i*frame->width*3)+j];
    for(int k = 0; k < 5; k++) {
      memory_agg = (memory_agg + memory_frames[k]->imageData[(i*frame->width*3)+j])/2;
    }
    frame->imageData[(i*frame->width*3)+j] = memory_agg;
  }
}

I just can’t figure out what I’m doing wrong here.

Posted March 2nd, 2008 - Permalink
Categories: C++ - CameraBooth - Graphics - OpenCV - Programming - Projects
You can leave a comment, or trackback from your own site.
 
Adjacent Posts
« « Creepy Flowers
libcvfx » »
 
Comments

3 Responses to “Screwy OpenCV Manipulations”

  1. [OpenCV] Washed out images : OpenCV Says:

    [...] I have some code and images here: http://www.velvetcache.org/2008/03/02/screwy-opencv-manipulations [...]

  2. giulio Says:

    try using cvSet2D and cvGet2D, or using img->widthstep instead of (img->width)*3.. often it’s not the same

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    IplImage* img = cvLoadImage(.....); //your image 
    int width = img->width; 
    int height = img->height;
    for (int j=0; j < height; j++){
           for (int i=0; i < width; i++){
     
              CvScalar rgb = cvGet2D(img, j, i);
              int gray = rgb[0]*0.114 + rgb[1]*0.587 +
              rgb[2]*0.299;
            }
    }
  3. john Says:

    Works like a charm! Here’s the code I ended up with for it.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    int width = frame->width;
    int height = frame->height;
    for (int j=0; j < height; j++){
      for (int i=0; i < width; i++){
        rgb = cvGet2D(frame, j, i);
        int gray = rgb.val[0]*0.114 + rgb.val[1]*0.587 +
                   rgb.val[2]*0.299;
        rgb.val[0] = gray;
        rgb.val[1] = gray;
        rgb.val[2] = gray;
        cvSet2D(frame,j,i,rgb);
      }
    }