mardi 31 mars 2015

Reduce number of levels per pixel, creating a mirror image, C++

I have a greyscale image that has a resolution of 150 x 200 pixels.


Task:


Create a function to count the number of black pixels in the image and display this.


Create a function that will invert the image.


Create a function that will reduce the number of levels per pixel , that is - setting the lower 6 bits in each pixel to 0 using the bitwise AND operator. The function should reduce the number of levels used per pixel to four.


Create a function that will create a mirror image, that is - so that for every row, pixel value in element 0 should be swapped with pixel value in element 149, and element 1 with element 148 etc...


So far, I have completed the first two parts of the task, however I am struggling with creating correct working functions for the last two (shown by asterisk characters) , any ideas?


My code:



#include <QCoreApplication>
#include <iostream>
#include "ImageHandle.h"

using namespace std;


int CountBlackPixels (unsigned char PixelGrid[WIDTH][HEIGHT]);


void InvertImage (unsigned char PixelGrid[WIDTH][HEIGHT]);


void ReducePixelLevel (unsigned char PixelGrid[WIDTH][HEIGHT]);


void MirrorImage (unsigned char PixelGrid[WIDTH][HEIGHT]);


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

unsigned char PixelGrid[WIDTH][HEIGHT]; // Image loaded from file


// If the file "Parrot.png" cannot be loaded ...
if (!loadImage(PixelGrid, "Parrot.png"))
{
// Display an error message
cout << "Error loading file \"Parrot.png\"" << endl;
}
else
{
cout << "File \"Parrot.png\" opened successfully" << endl;

// Demo of use of saveImage - to create a copy as "ParrotCopy.png"
// This should be modified to save the new images as specified
if (saveImage(PixelGrid, "ParrotCopy.png"))
{
cout << "File \"ParrotCopy.png\" saved successfully" << endl;
}
else
{
cout << "Could not save \"ParrotCopy.png\"" << endl;
}
}

// Display number of black pixels ...
cout << "\nNumber of black pixels : " << CountBlackPixels(PixelGrid) << endl;

InvertImage(PixelGrid);

{
if (saveImage(PixelGrid, "ParrotInv.png"))
{
cout << "\nFile \"ParrotInv.png\" saved successfully" << endl;
}
else
{
cout << "\nCould not save \"ParrotInv.png\"" << endl;
}

}

ReducePixelLevel(PixelGrid);

{
if (saveImage(PixelGrid, "Parrot4level.png"))
{
cout << "\nFile \"Parrot4level.png\" saved successfully" << endl;
}
else
{
cout << "\nCould not save \"Parrot4level.png\"" << endl;
}

}

MirrorImage (PixelGrid);

{
if (saveImage(PixelGrid, "ParrotMirror.png"))
{
cout << "\nFile \"ParrotMirror.png\" saved successfully" << endl;
}
else
{
cout << "\nCould not save \"ParrotMirror.png\"" << endl;
}

}
return a.exec();
}


int CountBlackPixels(unsigned char PixelGrid[WIDTH][HEIGHT])
{
int row;
int col;
int count = 0;

for (row = 0; row < WIDTH; row++)
{
for (col = 0; col < HEIGHT; col++)
{
if (PixelGrid[row][col] == 0)
count++;
}
}
return count;
}


void InvertImage (unsigned char PixelGrid[WIDTH][HEIGHT])
{
int row;
int col;

for (row = 0; row < WIDTH; row++)
{
for (col = 0; col < HEIGHT; col++)
{
PixelGrid[row][col] = ~PixelGrid[row][col];
}
}
}


void ReducePixelLevel (unsigned char PixelGrid[WIDTH][HEIGHT])


{
int row;
int col;

for (row = 0; row < WIDTH; row++)
{
for (col = 0; col < HEIGHT; col++)
{
*************************
}
}
}


void MirrorImage (unsigned char PixelGrid[WIDTH][HEIGHT])


{
***************************
}

Aucun commentaire:

Enregistrer un commentaire