2015-03-31 64 views
0

我有一个灰度图像,其分辨率为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]) 


{ 
    *************************** 
} 
+0

'std :: swap(PixelGrid [x] [y],PixelGrid [x] [HEIGHT-y-1])'在一些适当的循环中,沿着这些线? – 2015-03-31 13:24:10

+0

PixelGrid [row] [col]&= 0xc0; //使用按位AND运算符将每个像素的低6位设置为0。 – willll 2015-03-31 13:26:23

+0

需要一些清晰的“颠倒”是什么意思?如果意味着反转颜色,那么它是x = 255 - x(黑到白,白到黑)。 – Robinson 2015-03-31 14:00:32

回答

0

创建一个函数,这将减少每个像素电平的数目,即 - 在每个像素中的低6位使用位AND运算符设置为0。该函数应该将每像素使用的级别数减少到四级。

在每个象素设置的低6位为0,你只是这样做......

pixel &= 0xc0;pixel = pixel & 0xc0

注意0xc0扩展出1100 0000二进制。 Windows计算器可以在“程序员模式”中使用,这可能对此有用。

C中的&运算符被称为按位和。解释操作的含义超出了这个答案的范围,但我强烈建议用Google搜索它。

创建一个函数,它将创建一个镜像,即 - 对于每一行,元素0中的像素值应与元素149中的像素值交换,元素1与元素148等...

对于每一行... 对(INT行= 0;行< WITDH; ++行) {

在元素0

像素值应与像素值在元件149被交换,并元素1与元素148等

// Careful to not swap everything twice 
    for (int col = 0; col < HEIGHT/2; ++col) 
    { 
    int swap_col = HEIGHT - 1 - col; // Mirror pixel 

    // Canonical swap idiom. 
    unsigned char temp = PixelGrid[row][col]; 
    PixelGrid[row][col] = PixelGrid[row][swap_col]; 
    PixelGrid[row][swap_col] = temp; 
    } 

C++还提供了可能更快/更具表现力的std::swap函数。


PS。您使用HEIGHTWIDTH是非常不寻常的。我怀疑你的名字是相反的。

+0

谢谢!然而,上面的功能似乎并不反映我的形象,是否有可能我换了两次?我确切地说,你有什么建议略有不同的变量名称.. @QuestionC – Darren08 2015-04-02 07:33:16