Sunteți pe pagina 1din 6

Homework 1 in Machine Vision and Image Processing

Setup Homework 1:

- Installing .NET Framework Version 2.0 from \Homework1\dotNetFramework2\


- Running file setup.exe from \Homework1\ImageProcessing\Setup_Homework1\Debug\

To explain source codes:


/// RGB and CMYK color space
public IplImage RGB_CMYK(IplImage image, byte channel)
{
int i;
IplImage[] planes = new IplImage[3];

for (i = 0; i < 3; i++)


planes[i] = cvlib.CvCreateImage(new CvSize(image.width, image.height), 8, 1);

/// create an output image


IplImage output = cvlib.CvCreateImage(new CvSize(image.width, image.height),8,3);

/// separate color image into 3 channels: Blue – Green - Red


cvlib.CvSplit(ref image, ref planes[0], ref planes[1], ref planes[2]);

/// creat an temp image


IplImage temp = cvlib.CvCloneImage(ref planes[0]);

switch (channel)
{
//Blue: set Red channel and Green channel to zero
case 0: cvlib.CvSetZero(ref planes[1]);
cvlib.CvSetZero(ref planes[2]);
break;
//Green: set Red channel and Blue channel to zero
case 1: cvlib.CvSetZero(ref planes[0]);
cvlib.CvSetZero(ref planes[2]);
break;
//Red: set Blue channel and Green channel to zero
case 2: cvlib.CvSetZero(ref planes[0]);
cvlib.CvSetZero(ref planes[1]);
break;
//Yellow: set Blue channel to zero
case 3: cvlib.CvSetZero(ref planes[0]);
break;
//Magenta: set Green channel to zero
case 4: cvlib.CvSetZero(ref planes[1]);
break;
//Cyan: set Red channel to zero
case 5: cvlib.CvSetZero(ref planes[2]);
break;
//Black: K = min{R,G,B}
case 6:
cvlib.CvMin(ref planes[0], ref planes[1], ref temp);
cvlib.CvMin(ref planes[2], ref temp, ref temp);
cvlib.CvCvtColor(ref temp, ref output, cvlib.CV_GRAY2BGR);
break;
}
if (channel < 6)
cvlib.CvMerge(ref planes[0], ref planes[1], ref planes[2], ref output);

/// release unmanaged ressources


for (i = 0; i < 3; i++) cvlib.CvReleaseImage(ref planes[i]);
cvlib.CvReleaseImage(ref temp);

return output;
}

Nguyen Van Thai – ID: 97669033 1|Page


Homework 1 in Machine Vision and Image Processing

/// convert RGB to HSV


public IplImage RGBtoHSV(IplImage image,byte channel)
{
int i;
IplImage[] planes = new IplImage[3];

for (i = 0; i < 3; i++)


planes[i] = cvlib.CvCreateImage(new CvSize(image.width, image.height),8,1);

/// create an output image


IplImage output = cvlib.CvCloneImage(ref image);

/// create an hsv image


IplImage hsv = cvlib.CvCreateImage(new CvSize(image.width, image.height),8, 3);

/// create an temp image


IplImage temp = cvlib.CvCreateImage(new CvSize(image.width, image.height),8,3);

/// convert RGB image to HSV image


cvlib.CvCvtColor(ref image, ref hsv, cvlib.CV_BGR2HSV);

/// separate HSV image into 3 channels: Hue – Saturation - Value


cvlib.CvSplit(ref hsv, ref planes[0], ref planes[1], ref planes[2]);

switch (channel)
{
//Hue: is planes[0]
case 0: cvlib.CvCvtColor(ref planes[0], ref output, cvlib.CV_GRAY2BGR);
break;

//Saturation: is planes[1]
case 1: cvlib.CvCvtColor(ref planes[1], ref output, cvlib.CV_GRAY2BGR);
break;

//Value: is planes[2]
case 2: cvlib.CvCvtColor(ref planes[2], ref output, cvlib.CV_GRAY2BGR);
break;
}

/// release unmanaged ressources


for (i = 0; i < 3; i++) cvlib.CvReleaseImage(ref planes[i]);

return output;
}

Nguyen Van Thai – ID: 97669033 2|Page


Homework 1 in Machine Vision and Image Processing

/// convert RGB to YCrCb


public IplImage RGBtoYCrCb(IplImage image, byte channel)
{
int i;
IplImage[] planes = new IplImage[3];

for (i = 0; i < 3; i++)


planes[i] = cvlib.CvCreateImage(new CvSize(image.width, image.height),8,1);

/// create an output image


IplImage output = cvlib.CvCloneImage(ref image);

/// create an YCrCb image


IplImage YCrCb = cvlib.CvCreateImage(new CvSize(image.width,image.height),8,3);

/// create an temp image


IplImage temp = cvlib.CvCreateImage(new CvSize(image.width, image.height),8,3);

/// convert color image to YCrCb image


cvlib.CvCvtColor(ref image, ref YCrCb, cvlib.CV_BGR2YCrCb);

/// separate YCrCb image into 3 channels: Y – Cr - Cb


cvlib.CvSplit(ref YCrCb, ref planes[0], ref planes[1], ref planes[2]);

switch (channel)
{
//Y (Y is the luma component, or the brightness)
case 0: cvlib.CvCvtColor(ref planes[0], ref output, cvlib.CV_GRAY2RGB);
break;

//Cr (Cr is the red-difference chroma component)


case 1: cvlib.CvCvtColor(ref planes[1], ref output, cvlib.CV_GRAY2RGB);
break;

//Cb (Cb is the the blue-difference chroma component)


case 2: cvlib.CvCvtColor(ref planes[2], ref output, cvlib.CV_GRAY2RGB);
break;
}

// release unmanaged ressources


for (i = 0; i < 3; i++) cvlib.CvReleaseImage(ref planes[i]);

return output;
}

Nguyen Van Thai – ID: 97669033 3|Page


Homework 1 in Machine Vision and Image Processing

/// convert RGB to XYZ


public IplImage RGBtoXYZ(IplImage image, byte channel)
{
int i;
IplImage[] planes = new IplImage[3];

for (i = 0; i < 3; i++)


planes[i] = cvlib.CvCreateImage(new CvSize(image.width, image.height),8,1);

/// create an output image


IplImage output = cvlib.CvCloneImage(ref image);

/// create an XYZ image


IplImage XYZ = cvlib.CvCreateImage(new CvSize(image.width, image.height),8, 3);

/// create an temp image


IplImage temp = cvlib.CvCreateImage(new CvSize(image.width, image.height),8,3);

/// convert color image into XYZ image


cvlib.CvCvtColor(ref image, ref XYZ, cvlib.CV_BGR2XYZ);

/// separate XYZ image into 3 channels: X – Y – Z


cvlib.CvSplit(ref XYZ, ref planes[0], ref planes[1], ref planes[2]);

switch (channel)
{
//X, which can be compared to red
case 0: cvlib.CvCvtColor(ref planes[0], ref output, cvlib.CV_GRAY2BGR);
break;
//Y, which can be compared to green
case 1: cvlib.CvCvtColor(ref planes[1], ref output, cvlib.CV_GRAY2BGR);
break;
//Z, which can be compared to blue
case 2: cvlib.CvCvtColor(ref planes[2], ref output, cvlib.CV_GRAY2BGR);
break;
}

// release unmanaged ressources


for (i = 0; i < 3; i++) cvlib.CvReleaseImage(ref planes[i]);

return output;
}

Nguyen Van Thai – ID: 97669033 4|Page


Homework 1 in Machine Vision and Image Processing

Results:

Red Green Green

Cyan Magenta Yellow

Black

Hue Saturation Value

Nguyen Van Thai – ID: 97669033 5|Page


Homework 1 in Machine Vision and Image Processing

Y Cr Cb

CIE_X CIE_Y CIE_Z

Nguyen Van Thai – ID: 97669033 6|Page

S-ar putea să vă placă și