减少每个像素的级别数,创建镜像,C++
Reduce number of levels per pixel, creating a mirror image, C++
我有一张分辨率为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。你使用HEIGHT
和WIDTH
是非常不寻常的。我怀疑你把名字颠倒了。
相关文章:
- 在python docker镜像上安装simstring(SWIG)
- 将移动设备(iOS和Android)连接到带有QT / C++和镜像显示的VNC服务器
- 将迭代器从原始容器镜像到其副本
- 更快的图像镜像算法
- C++;镜像并从 std::basic_istream<> 中穿过?
- VTK-如何翻转镜像
- C++中是否有一个功能可以创建一个像Python的"numpy.bmat"一样简单的块矩阵?
- 仅使用位操纵来获得镜像的六角形
- 为什么我的 iFFT 图像带有镜像副本
- 在此镜像代码中,可以是什么样的矩阵转换
- 函数来创建镜像c++
- 如何交换像素以镜像图像c++
- 减少每个像素的级别数,创建镜像,C++
- 为什么 qopenglshader程序在 qt 中显示镜像
- 如何镜像矩阵的一个维度
- 有什么方法可以在内部镜像Microsoft的符号服务器吗?
- 生成彼此不是镜像的排列
- 实现镜像
- 将控制台输出镜像到c++中的文件
- 创建具有特定格式的位图以反转/镜像该位图