- 使用CImg库
题目:
Reducing the Number of Gray Levels in an Image
- (a) Write a computer program capable of reducing the number of gray levels in a image from 256 to 2, in integer powers of 2. The desired number of gray levels needs to be a variable input to your program.
- (b) Download Fig. 2.21(a) and duplicate the results shown in Fig. 2.21 of the book.
// image2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <algorithm>
#include "pch.h"
#include <iostream>
#include "CImg.h"
#include <Eigen3/Eigen/Dense>
#include <string>
#include <cstring>
using namespace cimg_library;
using namespace std;
using namespace Eigen;
int main()
{
for (int i = 0; i < 8; i++) {
CImg<int> SrcImg("E:/Desktop/picture_process/Lenna/lena512.bmp");
SrcImg.resize(SrcImg.width(), SrcImg.height(), 1, 1);
//SrcImg.display();
string picture = "picture";
string a = picture + to_string(i+1);
char* m = (char*)a.c_str();
int huidu = 256;
switch (i)
{
case 0:
huidu = 256;
cout << i << endl;
break;
case 1:
huidu = 128;
cout << i << endl;
break;
case 2:
huidu = 64;
break;
case 3:
huidu = 32;
break;
case 4:
huidu = 16;
break;
case 5:
huidu = 8;
break;
case 6:
huidu = 4;
break;
case 7:
huidu = 2;
break;
default:
break;
}
if (huidu == 256) {
SrcImg.display(m);
}
else {
cimg_forXY(SrcImg, x, y) {
int level = SrcImg(x, y) / (256 / huidu);
SrcImg(x, y) = level*255/(huidu-1);
}
SrcImg.display(m);
}
}
}
运行结果: