Header Information

iscas2007.org

y - ISCAS 2007

Header Information

• OpenCV reads in the image and stores the metadata

in the image’s header

• Let’s print out some header information


#include

#include

#include "cv.h"

// all done

}

int main(int argc, char* argv[]) {

// load a color image

IplImage* image=cvLoadImage(argv[1], 1);

// print some info about the image from the header

printf("Width is %d; \theight is %d\n", image->width, image->height);

printf("depth is %d; \twidthStep is %d\n",

image->depth, image->widthStep);

printf("nChannels is %d; \timageSize is %d\n",

image->nChannels, image->imageSize);

printf("origin is %d; \tdataOrder is %d\n",

image->origin, image->dataOrder);

printf("nSize is %d; \tID is %d\n", image->nSize, image->ID);

printf("align is %d; \talphaChannel is %d\n",

image->align, image->alphaChannel);


#include

#include

#include "cv.h"

int main(int argc, char* argv[]) {

if (argc < 2) {

fprintf(stderr, "Usage: %s image-file-name\n", argv[0]);

exit(1);

}

// load a color image

IplImage* image=cvLoadImage(argv[1], 1);

// print some info about the image from the header

printf("Width is %d; \theight is %d\n", image->width, image->height);

printf("depth is %d; \twidthStep is %d\n", image->depth, image->widthStep);

printf("nChannels is %d; \timageSize is %d\n",

image->nChannels, image->imageSize);

printf("origin is %d; \tdataOrder is %d\n", image->origin, image->dataOrder);

printf("nSize is %d; \tID is %d\n", image->nSize, image->ID);

printf("align is %d; \talphaChannel is %d\n", image->align, image->alphaChannel);

// all done

}


#include

#include

#include "cv.h"

int main(int argc, char* argv[]) {

if (argc < 2) {

fprintf(stderr, "Usage: %s image-file-name\n", argv[0]);

exit(1);

}

// load a color image

IplImage* image=cvLoadImage(argv[1], 1);

// print some info about the image from the header

printf("Width is %d; \theight is %d\n", image->width, image->height);

printf("depth is %d; \twidthStep is %d\n", image->depth, image->widthStep);

printf("nChannels is %d; \timageSize is %d\n",

image->nChannels, image->imageSize);

printf("origin is %d; \tdataOrder is %d\n", image->origin, image->dataOrder);

printf("nSize is %d; \tID is %d\n", image->nSize, image->ID);

printf("align is %d; \talphaChannel is %d\n", image->align, image->alphaChannel);

// all done

}


#include

#include

#include "cv.h"

int main(int argc, char* argv[]) {

if (argc < 2) {

fprintf(stderr, "Usage: %s image-file-name\n", argv[0]);

exit(1);

}

// load a color image

IplImage* image=cvLoadImage(argv[1], 1);

Fields in the image header

// print some info about the image from the header

printf("Width is %d; \theight is %d\n", image->width, image->height);

printf("depth is %d; \twidthStep is %d\n", image->depth, image->widthStep);

printf("nChannels is %d; \timageSize is %d\n",

image->nChannels, image->imageSize);

printf("origin is %d; \tdataOrder is %d\n", image->origin, image->dataOrder);

printf("nSize is %d; \tID is %d\n", image->nSize, image->ID);

printf("align is %d; \talphaChannel is %d\n", image->align, image->alphaChannel);

// all done

}


Compile the program on a CACS machine

gcc -I/usr/include/opencv -lhighgui \

-lcvaux -lcv -lml -lcxcore \

-o headerinfo headerinfo.c


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$

This is what we get

when we run the

program to print out the

header information


280 pixels

[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$

490 pixels


3 channels;

1 byte/channel

[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$

This is the number of bytes

OpenCV uses to store the image

data


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$

Check the arithmetic…

490*280*3=411600


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$

490*280*3=411600

They do not agree!


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$

Quick arithmetic:

412160-411600 = 560

560/280 = 2

490*3 = 1470

1472–1470 = 2

So we are using an extra 2

bytes per row


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$

Quick arithmetic:

412160-411600 = 560

560/280 = 2

490*3 = 1470

1472–1470 = 2

So we are using an extra 2

bytes per row


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$

What are these?


addnoise(IplImage* image) {

double drand48();

int row, column;

int channel;No

uchar* thisrow;

double rval;

for (row=0; rowheight; row++) {

Not the best way to get to the

beginning of a row!

// put pointer at the beginning of a row

thisrow = (image->imageData + row * (image->nChannels) * (image->width) );

for (column=0; column < (image->width); column++) {

}

}

rval = drand48(); // drand48 returns a number between 0 and 1

if (rval < 0.05) { // zap the pixel to black

for (channel=0; channelnChannels; channel++) {

thisrow[(image->nChannels)*column + channel] = 0;

}

}

else if (rval > 0.95) { // zap the pixel to white

for (channel=0; channelnChannels; channel++) {

thisrow[(image->nChannels)*column + channel] = 255;

}

}

// done addnoise

}


addnoise(IplImage* image) {

double drand48();

int row, column;

int channel;No

uchar* thisrow;

double rval;

for (row=0; rowheight; row++) {

The correct way to get to the

beginning of a row

// put pointer at the beginning of a row

thisrow = (image->imageData + row * (image->widthStep) );

for (column=0; column < (image->width); column++) {

}

}

rval = drand48(); // drand48 returns a number between 0 and 1

if (rval < 0.05) { // zap the pixel to black

for (channel=0; channelnChannels; channel++) {

thisrow[(image->nChannels)*column + channel] = 0;

}

}

else if (rval > 0.95) { // zap the pixel to white

for (channel=0; channelnChannels; channel++) {

thisrow[(image->nChannels)*column + channel] = 255;

}

}

// done addnoise

}


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$

=0 if interleaved

=1 if separate channels


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$


IplImage

• OpenCV is not a file format

• What does a header mean?

• “Header information” refers to the information read

from the header of an image file

• OpenCV stores that information plus the image data

as a structure called IplImage


struct

“Structures are a way of storing many different values

in variables of potentially different types under the

same name.”

Example:

struct point2d {

};

int screen_x;

int screen_y;

float world_x;

float world_y;


struct

struct IplImage {

int nChannels;

int depth;


char *imageData;


char colorModel[4];

}

Size of this structure is nSize


[cice@bud opencv]$ ./headerinfo bullpen.jpg

Width is 490; height is 280

depth is 8; widthStep is 1472

nChannels is 3; imageSize is 412160

origin is 0; dataOrder is 0

nSize is 144; ID is 0

align is 4; alphaChannel is 0

[cice@bud opencv]$


Color in Pictures


Color in Pictures

• Three components

– Red, Green Blue

• Any color can be represented as

C=rR+gG+bB


Color in Pictures

Write as a vector equation












































1

0

0

0

1

0

0

0

1

b

g

r

b

g

r


Color Components


Color Components


Color in Pictures

• Many other representations used in different

applications

– Cyan, Magenta, Yellow

– Hue, Saturation, Intensity


Colors in Pictures

• Correlation of color components


Color Components


Colors in Pictures

• Correlation of color components

• A pixel has red, green, blue components:

– If we know the red component of a pixel, can we

predict the green component? Etc.


Predicting Another Variable’s Values

y

Suppose you observe (x,y) pairs and

they fall onto a line as shown.

Given a particular value of x, can you

predict the value of y?

x


Predicting Another Variable’s Values

y

Suppose you observe (x,y) pairs and

they are distributed as shown.

Given a particular value of x, can you

predict the value of y?

x


Predicting Another Variable’s Values

y

Suppose you observe (x,y) pairs and

they are distributed as shown.

Given a particular value of x, can you

predict the value of y?

x


Predicting Another Variable’s Values

y

Suppose you observe (x,y) pairs and

they are distributed as shown.

Given a particular value of x, can you

predict the value of y?

x


Correlation

• The more correlated two variables are, the easier it

is to predict one from the other

• How well can we predict one color component from

another?

– Plot two color components against each other


Colors in Pictures

• Correlation of color components

• Are there representations that are less

correlated?


Luminance,Chrominance

• Luminance

– Intensity, brightness of pixel

• Chrominance

– Color of pixel


Correlation and Prediction

• Why do we care if the color components are

correlated?

• Correlated components are redundant


Luminance, Chrominance

• YUV

• Y = Luminance

• U, V = Chrominance


Luminance, Chrominance

At each pixel, the (r, g, b) values are combined

linearly to form the luminance value:

y


k

r

r


k

g

g


k

b

b


Luminance, Chrominance

The luminance value:

y


k

r

r


k

g

g


k

b

b

k r

k g

k b

0.2220

0.2126

0.2989

0.7067

0.7152

0.5866

0.0713

0.0722

0.1145


Luminance, Chrominance

The chrominance values are then calculated as

u

v

b

r

y

y


Luminance, Chrominance

The chrominance values can be written in terms of

(r, g, b) as

u b y b ( krr

k

v r y r ( k r k

r

g

g

g k

g

k

b

b

b)

b)

I.e.,

u

v

k

r

(1 k

r k

r

g

) r k

g (1 k

g

g k

b

b

) b

b


Luminance, Chrominance

The (y, u, v) values can be written in terms of (r, g,

b) as

y


k

r

r


k

g

g


k

b

b

u

k

r

r


k

g

g

(1

k

b

) b

v


(1

k

r

) r


k

g

g


k

b

b


Luminance, Chrominance

The (y, u, v) values can be written in terms of (r, g,

b) as

y



u



v


kr


kr


1

k

r



k

g

k

k

g

g

1

k


k

b

k

b

b

r





g





b


Luminance, Chrominance

The (r, g, b) values can be recovered from (y, u, v)

by

r




g



b


kr


kr


1

k

r



k

g

k

k

g

g

1

k


k

b

k

b

b






1

y



u



v


Luminance, Chrominance

• Variations of YUV used in

– Television standard (YIQ, YUV)

– Image and video compression (YCrCb)


Luminance, Chrominance

(y, cr, cb) defined using (y, u, v) values

y



cr


cb






1



0


0

0

s

r

0

0

0

s

b

y




u




v


Luminance, Chrominance

(y, cr, cb) defined using (y, u, v) values
















































b

g

r

k

k

k

k

k

k

k

k

k

s

s

c

c

y

b

g

r

b

g

r

b

g

r

b

r

b

r

1

1

0

0

0

0

0

0

1






































b

g

r

k

s

k

s

k

s

s

k

s

s

k

s

k

s

k

k

k

c

c

y

b

b

g

b

r

b

b

b

r

r

g

r

r

r

b

g

r

b

r


Luminance, Chrominance

(y, cr, cb) defined using (y, u, v) values

y



cr


cb






1



0


0

0

s

r

0

0 y

0




u


s


v

b


0




0.5



0.5


Luminance, Chrominance

(y, cr, cb) defined using (r, g, b) values

y



cr



c

b


0.2989



0.5000


0.1688

0.5866

0.4183

0.3312

0.1145 r


0.0817




g


0.5000


b


0




0.5



0.5


Luminance, Chrominance


Luminance, Chrominance

(r, g, b) recovered using (y, cr, cb) values

r




g



b


1.0000



1.0000


1.0000

1.4022

0.7145

0.0000

0.0000

y


0.3458





cr


1.7710




c

b


0





0.5





0.5


Luminance, Chrominance

• Luminance, chrominance values are less

correlated

• Human visual system not very sensitive to

distortion in chrominance values


OpenCV Implementation

cvCvtColor(source, destination, code);

code:

CV_RGB2GRAY

CV_RGB2YCrCb

CV_RGB2HSV

CV_RGB2HLS

Etc.


Make a color picture

CvCvtColor Example

• 512 by 512

• Create picture in YCrCb domain

// clamp Y to 128

// set Cr to vary every column

// set Cb to vary every row

• Convert picture from YCrCb to RGB

• Write picture


cvCvtColor

int main(int ac, char* av[]) {

// create an image 512 by 512, 3 channels, 8 bits/channel

IplImage* img = cvCreateImage(cvSize(512, 512),

IPL_DEPTH_8U, 3);

// picture is going to be YCrCb

makepicture(img);

// convert picture to RGB

cvCvtColor(img, img, CV_YCrCb2RGB);

// write color image

if (!cvSaveImage(av[1], img)) printf("Writing %s failed\n",

av[2]);

// all done

}


makepicture(IplImage* image) {

int row, column;

uchar yval, crval, cbval;

}

yval = 128; // clamp yval to 128

for (row=0; rowheight; row++) {

// put pointer at the beginning of a row

uchar* thisrow = (uchar*) (image->imageData + row *

(image->widthStep));

cbval = 255 * row /(float)(image->height);

for (column=0; column < image->width; column++) {

crval = 255 * column /(float)(image->width);

}

}

thisrow[3*column] = yval;

thisrow[3*column + 1] = crval;

thisrow[3*column + 2] = cbval;


cvCvtColor


Is the result correct?

Make a color picture

• 512 by 512

• Create picture in YCrCb domain

// clamp Y to 128

// set Cr to vary every column

// set Cb to vary every row

• Convert picture from YCrCb to RGB

• Write picture

Expect “red” component to increase from

left to right and “blue” component to

increase from top to bottom


Is the result correct?

Make a color picture

• 512 by 512

• Create picture in YCrCb domain

// clamp Y to 128

// set Cr to vary every column

// set Cb to vary every row

• Convert picture from YCrCb to RGB

• Write picture


What do we expect?

At top left, Y=128, Cr=0, Cb=0

At top right, Y=128, Cr=0, Cb=254

At lower left, Y=128, Cr=254, Cb=0

At lower right, Y=128, Cr=254, Cb=254


Luminance, Chrominance

(r, g, b) recovered using (y, cr, cb) values

r




g



b


1.0000



1.0000


1.0000

1.4022

0.7145

0.0000

0.0000

y


0.3458





cr


1.7710




c

b


0





0.5





0.5


What did we get?

• Use “printf” to write out the corner RGB values and

compare


OpenCV Storage

• BGR instead of RGB!

• Interleaved data are stored as “bgrbgrbgr…”


cvCvtColor

int main(int ac, char* av[]) {

// create an image 512 by 512, 3 channels, 8 bits/channel

IplImage* img = cvCreateImage(cvSize(512, 512),

IPL_DEPTH_8U, 3);

// picture is going to be YCrCb

makepicture(img);

// convert picture to BGR

cvCvtColor(img, img, CV_YCrCb2BGR);

// write color image

if (!cvSaveImage(av[1], img)) printf("Writing %s failed\n",

av[2]);

// all done

}

More magazines by this user
Similar magazines