数字图像处理实验(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);endy=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');endfigure(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);endendy = uint8(y);figure(3)imshow(y);title('halftoning');imwrite(y,'halftoning.jpg');實驗結果:
第一張圖片是原圖像;
第二張圖片是調整大小后的圖像,由于使用半色調技術會將原圖像放大,所以事先將圖片大小縮小一些,避免使用半色調技術后生成的圖像過大;
第三張圖片就是結果, 也可以在當前文件夾目錄下查看程序生成的halftoning.jpg圖片。
總結
以上是生活随笔為你收集整理的数字图像处理实验(1):PROJECT 02-01, Image Printing Program Based on Halftoning的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习入门学习笔记:(1)BP神经网络
- 下一篇: 数字图像处理实验(2):PROJECT