Escolar Documentos
Profissional Documentos
Cultura Documentos
H 50
Now H = 0.05m = 50 mm, t = 8mm, l = 25mm, D l 25 156.25 mm
t 8
so the distance between the object and the camera is 156.25 mm.
question 3: Please write a C++ function with viLib to read in an JPEG image and
display it on screen.
Answer:
int ReadinAndDisplayImage( char * filename )
{
unsigned char * ImageData; // point to an array of image data
int Width, Height, PixelFormat = 8; // size of image
int returnvalue; // return value
// allocate memory for image data
viGetImageWH( filename, Width, Height ); // obtain the size of image
ImageData = new unsigned char [ Width * Height * PixelFormat/8];
returnvalue = viReadImageFile( filename, ImageData, Width, Height,
PixelFormat ); // read in image data
if ( returnvalue == 0 ) return( ‐2 );
viNamedWindow( "Source Image", 1 ); // display image
viShowImage( "Source Image", ImageData, Width, Height, PixelFormat, 0 );
viWaitKey(0); // wait for a key pressed to return the function
viDestroyWindow( "Source Image" ); // destroy windows
delete [] ImageData; // clean up
return 0;
}
question 4: Please calculate the histogram of following image
Answer: Since the range of gray‐scale is 0 ‐ 6, therefore, we should first set an array
with 7 elements. Then, count the number for each gray‐value and get a table show as
follows:
gray‐scale 0 1 2 3 4 5 6
# 0 11 7 19 10 5 2
question 5: Please write a C++ function to perform image equalization for the
image in question 4.
Answer: The general process for image equalization is listed as follows:
1. Calculate the histogram: ni , i 0,1,..., N 1
s
2. Calculate the cumulative histogram: H s ni , s 0,1,..., N 1
i 0
N 1 s
3. Calculate t = T(s): t T ( s) ni
n i 0
4. Perform histogram equalization according to t = T(s). Here, n is the total
number of pixels in the image, N is the total gray levels (256, in general), s is original
gray value, t is the gray value after equalization.
C++ function:
// Histogram equalization
void HistogramEqu( unsigned char * InImage, int Width, int Height, unsigned char *
OutImage )
{
int h[256], H[256], i, k, total, LookupTable[256];
memset( h, 0, 256*sizeof(int) ); // set memory to 0
memset( H, 0, 256*sizeof(int) );
total = Width*Height;
// 1) calculate histogram of the image
for ( i = 0; i < total; i++ )
{
k = InImage[i];
h[k]++;
}
// 2) work out cumulative histogram
H[0] = h[0];
for ( k = 1; k < 256; k++ ) H[k] = H[k‐1] + h[k];
// 3) calculate lookup table
for ( k = 0; k < 256; k++ )
LookupTable[k] = (int)( H[k]*255/total+0.5 );
// 4) perform histogram equalization
for ( i = 0; i < total; i++ )
{
k = InImage[i];
OutImage[i] = LookupTable[k];
}
}
question 6: Please convolute following image with template T.
And please write a C++ function to realize above process.
Answer: Because the template is 3x3, the boundaries are extreme left and right
columns and top and bottom rows. We can skip all those pixels in boundaries.
According to convolutional operation, we deal with the first non‐boundary pixel:
g (1,1) 1 0 3 1 2 1 1 0 3 2 2 1 5 1 3 0 2 0 =18
Similar operation are performed on the remaining pixels and finally we can get
following results:
1 3 2 4 5 6 3 3 3
1 18 15 18 24 27 18 19 4
5 16 12 12 19 21 19 23 4
3 14 10 11 16 20 20 21 4
1 12 9 11 18 13 17 25 4
1 1 1 3 3 3 5 5 5
C++ function is as follows:
// 2D convolution
void Convolution2Db( int * input_image, int * output_image,
int height, int width,
int * templates,
int t_height, int t_width )
{
int x, y, x1, y1;
int marginx, marginy;
int sum;
marginx = t_width/2; // calculate boundary
marginy = t_height/2;
memcpy( output_image, input_image, sizeof(int)*height*width );
for ( y = marginy; y < height‐marginy; y++ )
for ( x = marginx; x < width‐marginx; x++ )
{
sum = 0;
for ( y1 = 0; y1 < t_height; y1++ )
for ( x1 = 0; x1 < t_width; x1++ )
{
sum += input_image[ (y‐marginy+y1)*width+(x‐marginx+x1) ]
* templates[ y1*t_width+x1 ];
}
output_image[ y*width+x ] = sum;
}
return;
}
question 7: Please use the left filter to perform median filtering on the right image
(you can skip those boundary pixels). Also please write a C++ function to realize the
process.
Answer: We skip left 2 columns and right 2 columns since they are boundaries. Then,
scan first line, select first 5 elements, for instance, { 1, 1, 2, 20, 1 }. Sort these five
elements, we get {1, 1, 1, 2, 20}, and extract median element to replace the original
one, i.e. 2 to get filtered result. Repeat above process, we can get the result image:
1 1 1 2 5 12 5 5 5 0
1 2 2 4 4 4 4 4 3 1
0 0 0 3 3 3 3 2 2 1
C++ function is as follows:
// median filter
int medianvalue( int * data, int N )
{
for ( int i = 0; i < N‐1; i++ )
for ( int j = i+1; j < N; j++ )
if (data[i] > data[j])
{
int t = data[i];
data[i] = data[j];
data[j] = t;
}
return( data[N/2] );
}
void medianfilter()
{
int Image[] = { 1, 1, 2, 20, 1, 5, 12, 12, 5, 0,
1, 2, 2, 4, 4, 10, 5, 4, 3, 1,
0, 0, 0, 3, 3, 3, 2, 3, 2, 1 };
int Result[30];
int w = 10, h = 3;
int data[5];
memcpy( Result, Image, sizeof(int)*30 );
for ( int y = 0; y < h; y++ )
for (int x = 2; x < w ‐ 2; x++)
{
for (int i = 0; i < 5; i++)
data[i] = Image[y*w+(x‐2)+i];
Result[y*w+x] = medianvalue(data, 5);
}
}
question 8: Please enlarge following image by transformation (1), and use nearest
neighbor method for interpolating brightness values.
x 1.5 x '
(1)
y 2.5 y '
Answer:
1) x' is from 0 to 3, y' is from 0 to 3, therefore xmin = 0, xmax = 1.5x3 ‐> 4, ymin = 0, ymax
= 2.5x3 ‐> 7. So, x is from 0 to 7, while y is from 0 to 4.
x 1.5 x '
2) convert forward transform to backward projection form, i.e.
y 2.5 y '
x ' 23 x
y ' 52 y
3) then calculate each grid pixel, for example, the x = 1, y =1, we can get x' = 0.667, y'
= 0.4, the nearest point to point (0.667, 0.4) in x'‐y' plane is ( 1, 0), the pixel value for
(1, 0) is just I'(1,0) = 5, therefore, I(x,y) = 5.
Repeat above 3 steps, we can work out all the pixel values for new image. The result
image is shown as follows:
4 5 5 3 1
4 5 5 3 1
0 1 1 3 5
0 1 1 3 5
4 5 5 4 3
4 5 5 4 3
4 5 5 4 3
0 2 2 4 3