数字图像处理实验(1):PROJECT 02-01, Image Printing Program Based on Halftoning
實(shí)驗(yàn)要求:
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).
實(shí)驗(yàn)要求是英文的,不懂的詞查查字典就行了,這里就不翻譯了。
這里簡(jiǎn)單介紹一下:
Halftoning, 意思是半色調(diào)(技術(shù)),而在前面的英文實(shí)驗(yàn)要求中詳細(xì)介紹了其原理。
我們主要看下面這幅圖:
假設(shè)一幅灰度圖像,那么我們知道它的每個(gè)像素都有一個(gè)對(duì)應(yīng)的灰度值,通常這個(gè)灰度的取值范圍是 0 ~ 255,即 8 bit 灰度級(jí)。而半色調(diào)技術(shù)的意思就是,如圖中所示,將灰度級(jí)壓縮,劃分成 10 個(gè)灰度級(jí),取值范圍是 0 ~ 9。我們想將以前的圖像的一個(gè)像素,用現(xiàn)在的一個(gè) 3 * 3的矩陣表示,它在原圖像中的像素值是一個(gè) 0 ~ 255 的數(shù), 在 0 ~ 255 內(nèi)10等分,即像素灰度值除以25.6,則其像素值就會(huì)對(duì)應(yīng)圖中的某一種情況,那么就用那個(gè)矩陣來(lái)表示這個(gè)像素。比如灰度是 0, 那么那個(gè)像素就用圖中 0 的情況的矩陣表示。
程序?qū)崿F(xiàn)不難,不多說(shuō)直接上代碼吧:
%%clear all;clc;close all;%% 生成一個(gè)大小為 256 *256 的簡(jiǎn)便灰度圖像s = 256;y = zeros(s,s);% 每行對(duì)應(yīng)一個(gè)灰度級(jí)for (i=1:s)y(i,:)=(255-i-1)*ones(1,s);endy=uint8(y);imwrite(y,'general_img.jpg');%% 基于半色調(diào)計(jì)數(shù)的圖像打印程序 % x = imread('general_img.jpg'); %讀取圖片x = imread('gray_image.jpg'); %讀取圖片figure(1)imshow(x);title('original_image');[r, c] = size(x); % 獲取圖片大小% 判斷圖片大小是否超過(guò) 272 * 352, 若超過(guò)則進(jìn)行調(diào)整r_scale = double(r) / 272; c_scale = double(c) / 352;scale = max(r_scale, c_scale); % 找出較大的,后面進(jìn)行壓縮% 若圖像過(guò)大,進(jìn)行壓縮if(scale > 1)x = imresize(x, 1.0 / scale ); imwrite(x, 'adjusted_img.jpg');endfigure(2)imshow(x);title('adjusted_image');%% 將256灰度級(jí)量化成10灰度級(jí)別[ry, cy] = size(x); % 獲取圖片大小qim = fix( double(x) / 25.6 ); % 四舍五入?;叶燃?jí)減小到10灰度級(jí),取整數(shù)形式。y = zeros(ry*3, cy*3); % 創(chuàng)建新圖片% 構(gòu)造點(diǎn)模式表示10個(gè)灰度級(jí)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];% 對(duì)每個(gè)圖像進(jìn)行點(diǎn)陣映射for (i = 1:ry)for (j = 1:cy)level = qim(i, j); % 去除灰度級(jí)y((i-1)*3+1:i*3,(j-1)*3+1:j*3)=dot_mat(:,:,level+1);endendy = uint8(y);figure(3)imshow(y);title('halftoning');imwrite(y,'halftoning.jpg');實(shí)驗(yàn)結(jié)果:
第一張圖片是原圖像;
第二張圖片是調(diào)整大小后的圖像,由于使用半色調(diào)技術(shù)會(huì)將原圖像放大,所以事先將圖片大小縮小一些,避免使用半色調(diào)技術(shù)后生成的圖像過(guò)大;
第三張圖片就是結(jié)果, 也可以在當(dāng)前文件夾目錄下查看程序生成的halftoning.jpg圖片。
總結(jié)
以上是生活随笔為你收集整理的数字图像处理实验(1):PROJECT 02-01, Image Printing Program Based on Halftoning的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 机器学习入门学习笔记:(1)BP神经网络
- 下一篇: 数字图像处理实验(2):PROJECT