2015-03-31 68 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