我有一个灰度图像,其分辨率为150 x 200像素。减少每像素级别的数量,创建一个镜像,C++
任务:
创建一个函数来计算图像中的黑像素的数量,并显示此。
创建一个反转图像的函数。
创建一个函数,可以减少每个像素的级数,即 - 使用按位AND运算符将每个像素的低6位设置为0。该函数应该将每像素使用的级别数减少到四级。
创建将创建一个镜像图像的功能,那就是 - 使得对于每行,在元素0的像素值应该与像素值在交换元件149,和元件1与元件148等...
到目前为止,我已经完成了前两部分任务,但是我正努力为后两个部分创建正确的工作函数(用星号字符显示),有什么想法?
我的代码:
#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])
{
***************************
}
'std :: swap(PixelGrid [x] [y],PixelGrid [x] [HEIGHT-y-1])'在一些适当的循环中,沿着这些线? – 2015-03-31 13:24:10
PixelGrid [row] [col]&= 0xc0; //使用按位AND运算符将每个像素的低6位设置为0。 – willll 2015-03-31 13:26:23
需要一些清晰的“颠倒”是什么意思?如果意味着反转颜色,那么它是x = 255 - x(黑到白,白到黑)。 – Robinson 2015-03-31 14:00:32