reading and writing the viewport - page 2 - General Discussion and Assistance - CHDK Forum  

reading and writing the viewport

  • 28 Replies
  • 10596 Views
*

Offline GrAnd

  • ****
  • 916
  • [A610, S3IS]
    • CHDK
Re: reading and writing the viewport
« Reply #10 on: 24 / April / 2008, 14:14:26 »
Advertisements
BTW. In the viewport buffer you can't assign different colors to each pixel horizontally. Only to a group of 4 pixels.
CHDK Developer.

Re: reading and writing the viewport
« Reply #11 on: 25 / April / 2008, 06:51:11 »
I hope to get some comments and corrections for those places where I have got it wrong.

Code: [Select]
// alternatively I comment out the above line and uncomment the following line           
//               load_viewport(img, viewport_size, "A/VPDATA/ViewPortData0");                   }
            histogram_stage=0;   
         }
            break;

should be :-
Code: [Select]
// alternatively I comment out the above line and uncomment the following line           
//               load_viewport(img, viewport_size, "A/VPDATA/ViewPortData0");                   }
            histogram_stage=0;   
         }
       }
            break;


I have not created the ImageJ plugin yet.

Do we only capture the Playback viewport ?


David

Re: reading and writing the viewport
« Reply #12 on: 25 / April / 2008, 08:08:58 »
I have a better idea, let me explain the 'problem' ....

When taking two images for stereo with a single camera, you take the first photo and note what the nearest subject is.

You then move the camera a certain amount sideways and swivel the camera so that the nearest subject coincides with its position on the first photo (you have to guess that).

The most distant points do not coincide, they are separated by a certain amount (the deviation value), that is what creates the stereo depth.
If that separation is too great, it will hurt your eyes.

SDM already has overlay guide-lines that indicate the deviation when using a pair of cameras, we want a visual indication of deviation when moving a single camera.

Let us assume that a new mode 'Overlay Previous Image' has been selected.

On shutter full-press, the viewport buffer is saved together with the image and immediately reloaded as an overlay image.

It will have to be partly transparent.

We can now move the camera, overlap the near points and see the deviation of the far points.
On fully-pressing the shutter for the second , only the image is saved (not the viewport).

Typical acceptable maximum deviations would be about 14 pixels, but maybe 16 pixels is a more suitable value and a red line that length could be included on the overlay to serve as a guide.

The bottom of this page shows two guide-lines separated by that typical amount :-

http://stereo.jpn.org/eng/sdm/grids.htm


So, will this work ?

How do I make the saved/loaded buffer image monochrome (probably) and partially transparent ?


David

Re: reading and writing the viewport
« Reply #13 on: 25 / April / 2008, 10:26:45 »
[
Do we only capture the Playback viewport ?

In record mode the display is continually updated so you can't display a saved image, but you might be able to save an image.  I haven't tried this.  Are there special time constraints in record mode?

For your stereo application there is no need to save the viewport to disk, you can malloc a ram buffer and save it there.  That will be faster.

It seems the pallet of the overlay screen is limited, but includes many transparent colors.  So you shoot the first picture.  Save it to a ram buffer.  Enhance it (I think some contrast and edge enhancement would help before reducing colors) and reduce it to however many colors will work for the overlay, then display as an overlay.  In fact maybe an edge image would work best.  Threshold the edge image and use the high edge areas for the overlay, make the low edge areas transparent.

If this takes too much time in record mode it could be done in play mode after the first image was shot.

I am a little busy now, but when I get some time I will write an ImageJ macro to display and convert the viewport data.

Jon



« Last Edit: 25 / April / 2008, 10:30:54 by hiker_jon »


*

Offline GrAnd

  • ****
  • 916
  • [A610, S3IS]
    • CHDK
Re: reading and writing the viewport
« Reply #14 on: 25 / April / 2008, 10:36:26 »
Are there special time constraints in record mode?

Yes, there are. But they are big enough for saving viewport.
CHDK Developer.

Re: reading and writing the viewport
« Reply #15 on: 25 / April / 2008, 16:22:01 »
Here is an ImageJ macro that decodes the viewport data:
Code: (cpp) [Select]
//ViewportToRGB.txt
//This macro can read Canon viewport data and convert to RGB format. 
//For my camera, the A720IS, the viewport data is 360x240 x 3bytes.
//Every 6 bytes in the data is encoded as U,Y1,V,Y2,Y3,Y4 in the YUV color system.
//This routine decodes that into 4 RGB pixels.  So the output for my camera is
//720x240.  The correct aspect ratio is 360x240.


run("Raw...", "open=[] image=[24-bit RGB] width=360 height=240 offset=0 number=1 gap=0 little-endian");
setBatchMode(true);

id0 = getImageID();
width = getWidth();
height = getHeight();

line = newArray(width * 2);

newImage("Converted", "RGB", width * 2, height, 1);
id1 = getImageID();

for (h=0;h<height;h++) {
w1 = 0;
selectImage(id0);
for(w=0;w<width;w+=2) {
        p = getPixel(w,h);
            u = (p>>16)&0xff;
if ((u&0x80) != 0) u = u - 255;
            y = (p>>8)&0xff;
v = p &0xff;
if ((v&0x80) != 0)  v = v - 255;
r = clip(((y * 4096)          + v*5743 + 2048)/4096); // R    
g = clip(((y * 4096) - u*1411 - v*2925 + 2048)/4096); // G
b = clip(((y * 4096) + u*7258          + 2048)/4096); // B

line[w1++] = (r << 16)  + (g << 8) + b;

p = getPixel(w+1,h);

y = (p>>16) & 0xff;
y1 = (p>>8) & 0xff;
y2 = p & 0xff;

r = clip(((y1 * 4096)          + v*5743 + 2048)/4096); // R    
g = clip(((y1 * 4096) - u*1411 - v*2925 + 2048)/4096); // G
b = clip(((y1 * 4096) + u*7258          + 2048)/4096); // B

line[w1++] = (r << 16)  + (g << 8) + b;

r = clip(((y1 * 4096)          + v*5743 + 2048)/4096); // R    
g = clip(((y1 * 4096) - u*1411 - v*2925 + 2048)/4096); // G
b = clip(((y1 * 4096) + u*7258          + 2048)/4096); // B

line[w1++] = (r << 16)  + (g << 8) + b;

r = clip(((y2 * 4096)          + v*5743 + 2048)/4096); // R    
g = clip(((y2 * 4096) - u*1411 - v*2925 + 2048)/4096); // G
b = clip(((y2 * 4096) + u*7258          + 2048)/4096); // B

line[w1++] = (r << 16)  + (g << 8) + b;
}

selectImage(id1);
for(w=0;w<width * 2; w++) setPixel(w,h,line[w]);
}
setBatchMode(false);

   function clip(a) {
      if(a < 0) a = 0;
      if(a > 255) a = 255;
      return a;
   }
« Last Edit: 27 / April / 2008, 02:17:35 by hiker_jon »

*

Offline fudgey

  • *****
  • 1705
  • a570is
Re: reading and writing the viewport
« Reply #16 on: 25 / April / 2008, 16:50:14 »
You might want to use the "insert code" button to paste C code, those smileyfaces look a bit funky up there...

Re: reading and writing the viewport
« Reply #17 on: 25 / April / 2008, 17:49:21 »
Here is an imageJ macro to convert a 720x240 RGB image to viewport format
Code: (cpp) [Select]
//RGBtoViewport.txt
//This macro can convert an RGB image to Canon viewport data. 
//For my camera, the A720IS, the viewport data is 360x240 x 3bytes.
//Every 6 bytes in the data is encoded as U,Y1,V,Y2,Y3,Y4 in the YUV color system.
//This routine takes 4 RGB pixels and converts them to 6 bytes in the output data 
//The input RGB image for this function is resized to  720x240. 
//The output is a 360x240 RGB image saved in raw format

open();
setBatchMode(true);
run("Scale...", "x=[] y=[] width=720 height=240 interpolate create title=VPinput");
id0 = getImageID();
width = getWidth();
height = getHeight();

line = newArray(width / 2);

newImage("Viewport", "RGB", width / 2, height, 1);
id1 = getImageID();

for (h=0;h<height;h++) {
w1 = 0;
selectImage(id0);
for(w=0;w<width;w+=4) {
        p = getPixel(w,h);
                r = (p >> 16) & 0xff;
                g = (p >> 8) & 0xff;
                b = p & 0xff;
         
                y = ((1225 * r) + (2400 * g) + (471 * b) + 2048)/4096; // Y
                        u = clips(floor((2314 * (b - y) + 2048)/4096)); // U
                v = clips(floor((2925 * (r - y) + 2048)/4096)); // V
         
                line[ w1++] = (((u&0xff) << 16)&0xff0000)  + (((y&0xff) << 8)&0xff00) + (v&0xff);
         
          p = getPixel(w+1,h);
                r = (p >> 16) & 0xff;
                g = (p >> 8) & 0xff;
                b = p & 0xff;
                y = ((1225 * r) + (2400 * g) + (471 * b) + 2048)/4096; // Y
         
        p = getPixel(w+2,h);
                r = (p >> 16) & 0xff;
                g = (p >> 8) & 0xff;
                b = p & 0xff;
                y1 = ((1225 * r) + (2400 * g) + (471 * b) + 2048)/4096; // Y
         
        p = getPixel(w+3,h);
                r = (p >> 16) & 0xff;
                g = (p >> 8) & 0xff;
                b = p & 0xff;
                y2 = ((1225 * r) + (2400 * g) + (471 * b) + 2048)/4096; // Y
         
                line[ w1++] = (y << 16)  + (y1 << 8) + y2;
}

selectImage(id1);
for(w=0;w<width /2; w++) setPixel(w,h,line[w]);
}

saveAs("Raw Data", "");

setBatchMode(false);

   function clips(a) {
      if(a < -127) a = -127;
      if(a > 127) a = 127;
      return a;
   }
« Last Edit: 26 / April / 2008, 19:44:23 by hiker_jon »


Re: reading and writing the viewport
« Reply #18 on: 25 / April / 2008, 18:31:52 »
Reading the viewport data did not get ver far  ......

path = File.openDialog("Choose a File");   gives 'undefined variable' for 'File'.


I do not use the most recent version of ImageJ because it breaks some of my existing applications.


OK, I have installed 1.40.

')' expected in line 30 and many other lines.
y=(p 8 & 255 <;>

will correct it.


That now works fine, I have cinemascope images !
« Last Edit: 25 / April / 2008, 19:09:23 by Microfunguy »

Re: reading and writing the viewport
« Reply #19 on: 25 / April / 2008, 19:22:05 »
Creating the RAW file seemed to be OK but when reading it gave strange results (just a pattern, no image).

Maybe I did it wrong, will check tomorrow.

A number of brackets also missing from that macro, some caused by the 'smiley'.


David


Aaah  .. my input image has to be resized to 720x280 (maybe the macro can do it ?)
« Last Edit: 25 / April / 2008, 19:23:37 by Microfunguy »

 

Related Topics