减少每个像素的级别数,创建镜像,C++

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

本文关键字:创建 镜像 C++ 像素      更新时间:2023-10-16

我有一张分辨率为150 x 200像素的灰度图像。

任务:

创建一个函数来计算图像中黑色像素的数量并显示此值。

创建一个将反转图像的函数。

创建一个函数来减少每个像素的级别数,即使用逐位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])

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

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

要将每个像素中较低的6位设置为0,只需执行以下操作。。。

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

注意,0xc0以二进制形式扩展到1100 0000。Windows计算器可以在"程序员模式"下使用,这可能对这些东西有用。

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

创建一个将创建镜像的函数,也就是说,对于每一行,元素0中的像素值应与元素149中的像素值相交换,元素1应与元素148等相交换

对于每一行。。。for(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++还提供了CCD_ 6函数,该函数可以更快/更具表现力。


PS。你使用HEIGHTWIDTH是非常不寻常的。我怀疑你把名字颠倒了。