数字图像处理实验(1):PROJECT 02-01, Image Printing Program Based on Halftoning

实验要求:

Image Printing Program Based on Halftoning
Objective:
To know in principle what is “halftoning”, the definition of resolution, and how to print an image in a mono-chromosome printer.
Main requirements:
Ability of programming with C, C++, or Matlab.
Instruction manual:
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.
(c) Print book Figs. 2.22(a) through (c) 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 (c).

实验要求是英文的,不懂的词查查字典就行了,这里就不翻译了。
这里简单介绍一下:
Halftoning, 意思是半色调(技术),而在前面的英文实验要求中详细介绍了其原理。
我们主要看下面这幅图:
这里写图片描述
假设一幅灰度图像,那么我们知道它的每个像素都有一个对应的灰度值,通常这个灰度的取值范围是 0 ~ 255,即 8 bit 灰度级。而半色调技术的意思就是,如图中所示,将灰度级压缩,划分成 10 个灰度级,取值范围是 0 ~ 9。我们想将以前的图像的一个像素,用现在的一个 3 * 3的矩阵表示,它在原图像中的像素值是一个 0 ~ 255 的数, 在 0 ~ 255 内10等分,即像素灰度值除以25.6,则其像素值就会对应图中的某一种情况,那么就用那个矩阵来表示这个像素。比如灰度是 0, 那么那个像素就用图中 0 的情况的矩阵表示。

程序实现不难,不多说直接上代码吧:

%%
    clear all;
    clc;
    close all;

%% 生成一个大小为 256 *256 的简便灰度图像
    s = 256;
    y = zeros(s,s);
    % 每行对应一个灰度级
    for (i=1:s)
        y(i,:)=(255-i-1)*ones(1,s);
    end
    y=uint8(y);
    imwrite(y,'general_img.jpg');

%% 基于半色调计数的图像打印程序
%     x = imread('general_img.jpg');  %读取图片
    x = imread('gray_image.jpg');  %读取图片

    figure(1)
    imshow(x);
    title('original_image');

    [r, c] = size(x);   % 获取图片大小

    % 判断图片大小是否超过 272 * 352, 若超过则进行调整
    r_scale = double(r) / 272;  
    c_scale = double(c) / 352;

    scale = max(r_scale, c_scale);  % 找出较大的,后面进行压缩

    % 若图像过大,进行压缩
    if(scale > 1)
       x = imresize(x, 1.0 / scale ); 
       imwrite(x, 'adjusted_img.jpg');
    end

    figure(2)
    imshow(x);
    title('adjusted_image');

%% 将256灰度级量化成10灰度级别
    [ry, cy] = size(x);     % 获取图片大小
    qim = fix( double(x) / 25.6 );  % 四舍五入。灰度级减小到10灰度级,取整数形式。
    y = zeros(ry*3, cy*3);  % 创建新图片

    % 构造点模式表示10个灰度级
    dot_mat(:,:,1)=zeros(3,3);
    dot_mat(:,:,2)=[0 255 0;0 0 0;0 0 0];
    dot_mat(:,:,3)=[0 255 0;0 0 0;0 0 255];
    dot_mat(:,:,4)=[255 255 0;0 0 0;0 0 255];
    dot_mat(:,:,5)=[255 255 0;0 0 0;255 0 255];
    dot_mat(:,:,6)=[255 255 255;0 0 0,;255 0 255];
    dot_mat(:,:,7)=[255 255 255;0 0 255,;255 0 255];
    dot_mat(:,:,8)=[255 255 255;0 0 255;255 255 255];
    dot_mat(:,:,9)=[255 255 255;255 0 255;255 255 255];
    dot_mat(:,:,10)=[255 255 255;255 255 255;255 255 255];

    % 对每个图像进行点阵映射
    for (i = 1:ry)
        for (j = 1:cy)
            level = qim(i, j);  % 去除灰度级
            y((i-1)*3+1:i*3,(j-1)*3+1:j*3)=dot_mat(:,:,level+1);
        end
    end

    y = uint8(y);

    figure(3)
    imshow(y);
    title('halftoning');

    imwrite(y,'halftoning.jpg');

实验结果:
这里写图片描述

这里写图片描述

这里写图片描述

第一张图片是原图像;
第二张图片是调整大小后的图像,由于使用半色调技术会将原图像放大,所以事先将图片大小缩小一些,避免使用半色调技术后生成的图像过大;
第三张图片就是结果, 也可以在当前文件夹目录下查看程序生成的halftoning.jpg图片。

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Revit 是一种建筑信息模型(BIM)软件,不太了解半色调的融合算法在 Revit 中的应用场景,不过我可以给您提供一个 C++ 的半色调融合算法示例。 半色调融合算法是一种将两幅图像融合在一起的方法,它可以使得融合后的图像既能够保留原始图像的细节信息,又能够获得更好的视觉效果。该算法的基本思路是将两幅图像的灰度值进行平均,然后根据平均灰度值将两幅图像进行分割,并对分割后的图像进行半色调处理。 以下是一个简单的 C++ 实现示例: ```c++ #include <opencv2/opencv.hpp> using namespace cv; void halfToning(Mat& src, Mat& dst) { int threshold = 128; int width = src.cols; int height = src.rows; int x, y; uchar grayValue; for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { grayValue = src.at<uchar>(y, x); if (grayValue > threshold) { dst.at<uchar>(y * 2, x * 2) = 255; dst.at<uchar>(y * 2, x * 2 + 1) = 255; dst.at<uchar>(y * 2 + 1, x * 2) = 255; dst.at<uchar>(y * 2 + 1, x * 2 + 1) = 255; } else { dst.at<uchar>(y * 2, x * 2) = 0; dst.at<uchar>(y * 2, x * 2 + 1) = 0; dst.at<uchar>(y * 2 + 1, x * 2) = 0; dst.at<uchar>(y * 2 + 1, x * 2 + 1) = 0; } } } } int main() { Mat src = imread("input.jpg", IMREAD_GRAYSCALE); Mat dst(src.rows * 2, src.cols * 2, CV_8UC1, Scalar(0)); halfToning(src, dst); imshow("Half Toning", dst); waitKey(0); return 0; } ``` 该示例中,我们使用 OpenCV 库来读取图像并进行半色调处理。`halfToning` 函数中,我们将输入的灰度图像 `src` 进行半色调处理,并将结果保存在输出图像 `dst` 中。在半色调处理中,我们使用一个阈值 `threshold` 来将灰度值进行分割,并根据分割后的结果进行半色调处理。最终,我们展示了处理后的图像并等待用户按下任意键退出程序。 需要注意的是,该示例只是一个简单的示例,实际应用中可能需要更加复杂的算法或者处理逻辑。同时,该示例也没有涉及到 Revit 软件的相关内容,如果您有更加详细的应用场景或者要求,可以提供更加详细的信息,以便我能够更好地回答您的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值