数字图像处理 || c++实现半色调程序

  • 利用c++的CImg库和用于矩阵处理的Eigen库来实现
  • 编译工具:visual stdio(建议使用,之前我也使用sublime来配置c++的各种库,总是各种bug)

题目:

Image Printing Program Based on Halftoning The following figure shows ten shades of gray approximated by dot patterns. Each gray level is represented by a 3 x 3 pattern of black and white dots. A 3 x 3 area full of black dots is the approximation to gray-level black, or 0. Similarly, a 3 x 3 area of white dots represents gray level 9, or white. The other dot patterns are approximations to gray levels in between these two extremes. A gray-level printing scheme based on dots patterns such as these is called “halftoning.” Note that each pixel in an input image will correspond to 3 x 3 pixels on the printed image, so spatial resolution will be reduced to 33% of the original in both the vertical and horizontal direction. Size scaling as required in (a) may further reduce resolution, depending on the size of the input image.

  • (a) Write a halftoning computer program for printing gray-scale images based on the dot patterns just discussed. Your program must be able to scale the size of an input image so that it does not exceed the area available in a sheet of size 8.5 x 11 inches (21.6 x 27.9 cm). Your program must also scale the gray levels of the input image to span the full halftoning range.

  • (b) Write a program to generate a test pattern image consisting of a gray scale wedge of size 256 x 256, whose first column is all 0’s, the next column is all 1’s, and so on, with the last column being 255’s. Print this image using your gray-scale printing program.

  • © Print book Figs. 2.22(a) through © using your gray-scale printing program. Do your results agree with the conclusions arrived at in the text in pgs. 61-62 and Fig. 2.23? Explain. You will need to download Figs. 2.22(a) through ©.

    在这里插入图片描述

// image2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <algorithm>
#include "pch.h"
#include <iostream>
#include "CImg.h"
#include <Eigen3/Eigen/Dense>

using namespace cimg_library;
using namespace std;
using namespace Eigen;
int main()
{
	CImg<int> SrcImg;
	SrcImg.load_bmp("E:/Desktop/picture_process/Lenna/general_img.bmp");
	double r = (double)SrcImg.height() / 272;
	double c = (double)SrcImg.width() / 352;
	double scale = max(r, c);
	if (scale > 1) {
		double s = (double)1.0 / scale;
		SrcImg.resize(s*SrcImg.height(), s*SrcImg.width(),1,1,5);
	}
	MatrixXd m(3*SrcImg.height(), 3*SrcImg.width());
	MatrixXd dot_mat[10];
	for (int i = 0; i < 10; i++) {
		dot_mat[i] = MatrixXd::Zero(3, 3);
	}
	dot_mat[1] << 0, 255, 0,
		0, 0, 0,
		0, 0, 0;
	dot_mat[2] << 0, 255, 0,
		0, 0, 0,
		0, 0, 255;
	dot_mat[3] << 255, 255, 0,
		0, 0, 0,
		0, 0, 255;
	dot_mat[4] << 255, 255, 0,
		0, 0, 0,
		255, 0, 255;
	dot_mat[5] << 255, 255, 255,
		0, 0, 0,
		255, 0, 255;
	dot_mat[6] << 255, 255, 255,
		0, 0, 255,
		255, 0, 255;
	dot_mat[7] << 255, 255, 255,
		0, 0, 255,
		255, 255, 255;
	dot_mat[8] << 255, 255, 255,
		255, 0, 255,
		255, 255, 255;
	dot_mat[9] << 255, 255, 255,
		255, 255, 255,
		255, 255, 255;

	cimg_forXY(SrcImg, x, y) {
		SrcImg(x, y) = (int)(SrcImg(x, y) / 25.6);
	}
	//cout << SrcImg(0, 0, 0) << endl;
	for (int i = 0; i < SrcImg.height(); i++) {
		for (int j = 0; j < SrcImg.width(); j++) {
			int level = SrcImg(i, j, 0);
			m.block<3,3>(i*3,j*3) <<  dot_mat[level];
		}
	}
	CImg<int> tmp(m.rows(), m.cols(), 1, 1);
	cimg_forXY(tmp, x, y) {
		tmp(x, y) = m(x, y);
	}
	tmp.display();
	return 0;
}

原图
在这里插入图片描述

处理后的图片:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/perry0528/article/details/82854322