我有一個灰度圖像,其分辨率爲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