ImageDemo.cpp

From IPRE Wiki
Jump to: navigation, search
#include <Myro.h>
#include <GrayPicture.h>
#include <iostream>
#include <cmath> // For floor()
using namespace std;

#define round(x) floor((x)+0.5)

int main(){
    connect();
    Picture* image = robot.takePicture("grey");
    cout << "Showing Grey Image." << endl;
    show(image);

    // This uglyness is to overcome a bug in Myro-C++ 2.2.2
    // What we should be able to do is just do 
    //    Picture* negImage = clone(image);
    // The "clone" method doesn't work with Gray Images, only Color ones
    // This is fixed in the "trunk" version of code
    Picture* negImage = new GrayPicture(*(GrayPicture*)image);
    Pixel c,nc;
    for (int row=0; row < getHeight(image); row++){
        for(int col=0; col < getWidth(image); col++){
            c = getPixel(image,col,row);
            nc.R = 255-c.R;
            nc.G = 255-c.G;
            nc.B = 255-c.B;
            setPixel(negImage,col,row,nc);
        }
    }
    cout << "Showing the Negative Image." << endl;
    show(negImage);

    const int edgeKernel[3][3] = {{-1,-1,-1},
                                {-1, 9,-1},
                                {-1,-1,-1}};

    // Calculate and display an edge-enhanced image
    Picture* edgeImage = new GrayPicture(*(GrayPicture*)image);
    for(int y=1; y<getHeight(image)-1; y++){
        for(int x=1; x<getHeight(image)-1;x++){
            int sum = 0;
            for(int ky = -1; ky<2; ky++){
                for(int kx = -1; kx<2; kx++){
                    int val = getPixelValue_grey(image, x+kx, y+ky);
                    sum += edgeKernel[ky+1][kx+1] * val;
                }
            }

            sum = min( max( sum, 0 ), 255 );
            // There's unfortunatly no setPixelValue_grey, so we have to do
            // this... For a grey "pixel" it only looks at the "RED" value
            Pixel pix;
            pix.R = pix.G = pix.B = round(sum);
            setPixel(edgeImage, x,y, pix);
        }
    }
    cout << "Showing an edge-enhanced Image" << endl;
    show(edgeImage);

    Picture* blurImg = new GrayPicture(*(GrayPicture*)image);
    const float v = 1.0 / 9.0;
    const float blurKernel[3][3] = {{v,v,v},{v,v,v},{v,v,v}};
    for(int y=1; y<getHeight(image)-1; y++){
        for(int x=1; x<getHeight(image)-1;x++){
            int sum = 0;
            for(int ky = -1; ky<2; ky++){
                for(int kx = -1; kx<2; kx++){
                    // Calculate the adjacent pixel for this kernel point
                    // int pos = (y + ky)*img.width + (x + kx)
                    // Image is grayscale, red/green/blue are identical
                    int val = getPixelValue_grey(image,x+kx,y+ky);
                    sum += blurKernel[ky+1][kx+1]*val;
                }
            }
            // For this pixel in the new image, set the grey value
            // based on the sum from the kernel
            sum = min( max( sum, 0 ), 255 );
            // There's unfortunatly no setPixelValue_grey, so we have to do
            // this... For a grey "pixel" it only looks at the "RED" value
            Pixel pix;
            pix.R = pix.G = pix.B = round(sum);
            setPixel(blurImg, x,y, pix);
        }
    }
    cout << "Showing a Blurred Image" << endl;
    show(blurImg);

    // Create an X in the negative image and display it
    for (int y=0; y<getHeight(image); y++){
        setPixelColor(negImage,y,y,255,0,0);
        setPixelColor(negImage,getHeight(negImage)-y,y,255,0,0);
    }
    cout << "Showing the negative image with an X in it" << endl;
    show(negImage);

    disconnect();
}