1 CS4402 – Parallel Computing Lecture 11 Image Processing.

Post on 17-Jan-2016

216 views 0 download

Transcript of 1 CS4402 – Parallel Computing Lecture 11 Image Processing.

1

CS4402 – Parallel Computing

Lecture 11

Image Processing

2

Images

Image = rectangular area of colored pixel.

A colour c is defined by four channels c=(a,r,g,b)

where 0<=a,r,g,b<=255.

Important colours:

(0,0,0) is black; (255,255,255) is white.

Digital Image: - A matrix c=(c[i][j],i=0,1,…,w-1;j=0,1,…,h-1). - Four matrices a, r, g, b representing a channel.

3

Image Data Problem: Given an image grab the image data?

Several C, Java classes available to extract a,r,g,b from gif, jpg etc.

ppm / pnm format for uncompress images has

- header to give the nature, width, height, max gray

- a,r,g,b values for each pixel. http://netpbm.sourceforge.net/doc/ppm.html

Matrix2D is a class to extract the a,r,g,b matrices from ppm images.

e.g. Mat2D_LoadRGBFrom PNM() find the R,G,B data.

4

Classification of IP

Type of processing: - Low level processing: the pixel data is directly processed.

- High level processing: the pixel data is processed through filters.

Nature of processing:- Point transfs: preserve position and change colour.

- Geometrical transfs: preserve colour and change position.

- Spatial transfs: pixel data is given by some pixel’s neighbors.

- Global transfs: all the pixels contribute a a pixel value.

5

Parallel Algorithms for IP

IP transformations are examples of embarrassingly comp.- Split the image matrix(ces) on processors.

- Transform each pixel data according to transf’s equation.

- Collect the transformed matrix(ces).

P0 P1 ... P

6

General Structure of a || Transf

Image1 Image 2

pixelsR

Scatter

grabRGB (P0)

Gather

createImage (P0)

Transform

pixelsG

pixelsB

pixelsR pixelsR pixelsR

pixelsG pixelsG pixelsG

pixelsB pixelsB pixelsB

P0 P1 Pp

newPixelsR

newPixelsG

newPixelsB

newPixelR newPixelR newPixelR

newPixelG newPixelG

newPixelB

newPixelG

newPixelB newPixelB

P0 P1 Pp

numerically

7

Some PT Image Processing Inversion: Colors are transformed to opposite colors.

R=255-r; G=255-g; B=255-b;

Value = 255 – value;

Brightening up: Colors are stretched toward white.Value = (int)(value * scale) or more correctly

Value = min((int)(value * scale),255)

Stretching colors between min and maxValue = min+(int)(value - min)/(max-min)

Threshold Value = (value<threshold)?0:value.

8

Inverting a ppm image

Use Matrix2D objects to extract the r,g,b values from a ppm image.1. If Processor 0 then

extract the matrices.

2. Scatter the matrices on the processors.

3. Process the values of each local matrix.

4. Gather the matrices

5. If Processor 0 then create the new ppm image from the new matrices.

9

Inverting a ppm imageUse Matrix2D objects to extract the r,g,b values from a ppm image.

If Processor 0 then extract the matrices.Scatter the matrices on the processors.Process the values of each local matrix.Gather the matricesIf Processor 0 then create the new ppm image from the new matrices.

if (rank == 0){matR= Mat2D_createNull(); matG= Mat2D_createNull(); matB= Mat2D_createNull();Mat2D_loadRGBFromPNM(matR, matG, matB,"./im1.ppm"); // extract the r,g,b in 3 Matrix2D objects

numRows = Mat2D_getnRows(matG); numCols = Mat2D_getnCols(matG);rData = Mat2D_getDataChar(matR); // get the matrices from the 3 Matrix2D objectsgData = Mat2D_getDataChar(matG);bData = Mat2D_getDataChar(matB);

numPixels = numRows * numCols; pixelsPerProc = numPixels/size;}

10

Inverting a ppm imageUse Matrix2D objects to extract the r,g,b values from a ppm image.

If Processor 0 then extract the matrices.Scatter the matrices on the processors.Process the values of each local matrix.Gather the matricesIf Processor 0 then create the new ppm image from the new matrices.

MPI_Bcast(&pixelsPerProc, 1, MPI_INT, 0, MPI_COMM_WORLD);

MPI_Scatter(*rData,pixelsPerProc,MPI_UNSIGNED_CHAR,recvbufR,pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);MPI_Scatter(*gData,pixelsPerProc,MPI_UNSIGNED_CHAR,recvbufG,pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);MPI_Scatter(*bData,pixelsPerProc,MPI_UNSIGNED_CHAR,recvbufB,pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);

for (i=0; i<pixelsPerProc; i++){recvbufR[i] =(unsigned char) 255 - recvbufR[i];recvbufG[i] =(unsigned char) 255 - recvbufG[i];recvbufB[i] =(unsigned char) 255 - recvbufB[i];

}

MPI_Gather(recvbufR,pixelsPerProc,MPI_UNSIGNED_CHAR,*rData,pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);MPI_Gather(recvbufG,pixelsPerProc,MPI_UNSIGNED_CHAR,*gData,pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);MPI_Gather(recvbufB,pixelsPerProc,MPI_UNSIGNED_CHAR,*bData,pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);

11

Inverting a ppm imageUse Matrix2D objects to extract the r,g,b values from a ppm image.

If Processor 0 then extract the matrices.Scatter the matrices on the processors.Process the values of each local matrix.Gather the matricesIf Processor 0 then create the new ppm image from the new matrices.

if (rank == 0){

matR->data = rData;matG->data = gData;matB->data = bData;

Mat2D_saveRGBToPNM(matR,matG,matB,"./outColourInvert.ppm");

}

12

13

14

Parallel Code for the Histogram

1. if rank ==0 then

- grab rData, gData, bData.

2. Scatter the matrices rData, gData, bData.

3. Generate the distributions rHist, gHist, bHist

4. Reduce the array rHist, gHist, bHist

5. Visualise the histograms

15

Histogram Equalisation

Histograms have statistical information:

- least and most used value

- peeks, valleys, etc

- similar distribution of color flat histograms

Histogram Equalisation

- the histogram is stretched over the whole band

16

17

18

19

20

21

22

23

24

25

Example: Averagingif (rank == 0){

matR= Mat2D_createNull(); matG= Mat2D_createNull(); matB= Mat2D_createNull();Mat2D_loadRGBFromPNM(matR, matG, matB,"./im1.ppm");// extract the r,g,b in 3 Matrix2D objects

numRows = Mat2D_getnRows(matG); numCols = Mat2D_getnCols(matG); // get the matrices from the 3 Matrix2D objects

rData = Mat2D_getDataChar(matR);gData = Mat2D_getDataChar(matG);bData = Mat2D_getDataChar(matB);

}

26

MPI_Bcast(&numRows, 1, MPI_INT, 0, MPI_COMM_WORLD);MPI_Bcast(&numCols, 1, MPI_INT, 0, MPI_COMM_WORLD);pixelsPerProc = numRows * numCols / size;

// allocate the local matricesrLocalData = alloc_matrix(numRows/size,numCols); rNewLocalData = alloc_matrix(numRows/size,numCols);gLocalData = alloc_matrix(numRows/size,numCols); gNewLocalData = alloc_matrix(numRows/size,numCols);bLocalData = alloc_matrix(numRows/size,numCols); bNewLocalData = alloc_matrix(numRows/size,numCols);

// scatter the image data onto processorsMPI_Scatter(&rData[0][0],pixelsPerProc,MPI_UNSIGNED_CHAR,&rLocalData[0][0], pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);MPI_Scatter(&gData[0][0],pixelsPerProc,MPI_UNSIGNED_CHAR,&gLocalData[0][0], pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);MPI_Scatter(&bData[0][0],pixelsPerProc,MPI_UNSIGNED_CHAR,&bLocalData[0][0], pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);

// do image processing ... give the numerical transformations of the pixel valuesfor (i=0; i<nrRows/size; i++)for (j=0; j<nrCols; j++){

rNewLocalData[i][j] =average(i, j, numRows/size, numCols, rLocalData[i][j]); gNewLocalData[i][j] =average(i, j, numRows/size, numCols, gLocalData[i][j]); bNewLocalData[i][j] =average(i, j, numRows/size, numCols, bLocalData[i][j]);

}

27

MPI_Gather(&rNewLocalData[0][0],pixelsPerProc,MPI_UNSIGNED_CHAR,&rData[0][0], pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);

MPI_Gather(&gNewLocalData[0][0],pixelsPerProc,MPI_UNSIGNED_CHAR,&gData[0][0], pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);

MPI_Gather(&bNewLocalData[0][0],pixelsPerProc,MPI_UNSIGNED_CHAR,&bData[0][0], pixelsPerProc,MPI_UNSIGNED_CHAR,0,MPI_COMM_WORLD);

if (rank == 0){// update the image data in the Matrix2d OBJECTS

matR->data = rData;matG->data = gData;matB->data = bData;

// Create the output image.Mat2D_saveRGBToPNM(matR,matG,matB,"./outIm1.ppm");

}

MPI_Finalize();

28

int average(int i0, int j0, int n, int m, int ** data){

int s = 0, nr=0, j, i;for(i=-1;i<2;i++)for(j=-1;j<2;j++){

if(i0+i<0 || i0+i>=n ||j0+j<0 || j0+j>=m)continue;

nr++;s+=data[i0+i][j0+j];

}return (int)s/nr;}