【数字图像处理】浮雕效果和倒影效果

版权声明:本文为博主原创文章,欢迎转载。转载请注明出处http://blog.csdn.net/jsgaobiao https://blog.csdn.net/jsgaobiao/article/details/50913065


Ø  【作业要求】

Perform either of the following two tasks:

 

1. Write your own imresize() function codeto simulate the matab function imresize(). You should implement at least the‘nearest’ and the ‘bilinear’ methods. Compare you result with the matlabfunction imresize().

 

2.Design interpolation or tranformation algorithm to build some cool images.

 

Submit your code, result, and report. Youcan use the two attached images or other images for testing.


Ø  【作业思路】

一、          浮雕效果

每个像素的RGB值都设置为该位置的初始值img(I,j)减去其右下方第二的像素img(i+5,j+5)的差,最后统一加上128用于控制灰度,显示出类似浮雕的灰色。

这样处理的思路是,将图像上的每个点与它的对角线的像素点形成差值,这样淡化相似的颜色,突出不同的颜色、边缘,从而使图像产生纵深感,产生类似于浮雕的效果。

开始我采用了img(I,j)-img(i+1,j+1),后来与img(I,j)-img(i+5,j+5)实现的效果比较后发现,后者变换后的图像更“凸出”,效果更好,于是就采用了后者。

效果如下:

 

 

二、          水中倒影效果

倒影效果分两部分实现:

第一部分是对原图像的模糊化,用来展现水中倒影的样子。

这里的模糊是采用让每个像素点在原来的位置周围随机振荡的方式实现的。具体来看,我是生成了两个随机变量deltax,deltay作为像素”跳跃”的座标变化——即将img(i+deltax,j+deltay)处的值复制到img(I,j)处。这个像素”跳跃“的范围在半径为20的圆内。

 

第二部分是图像的拼接。首先需要将第一部分中获得的模糊图像镜面反转。

然后生成一个原图像两倍大的图像——宽度不变,高度变为原来的两倍。其中,新图像的上半部分为原图像,下半部分为镜面反射后的模糊图像。拼在一起后,就形成了水中倒影的效果。

 

 

上期谜语:不施粉黛(二字数字图像术语)—— 像素


Ø  【文件说明】

main.m:

主程序,包含了对图像进行浮雕效果和倒影效果的处理程序。

 

%% relievo the image
img1 = imread('ha.jpg');
ret1 = relievo(img1);
subplot(1,2,1);
imshow(img1);
subplot(1,2,2);
imshow(ret1);

%% dim the image & create the inverted reflection in water
img2 = imread('building.jpg');
% dim the image
ret2 = dim(img2);   
% direct reflection of the dim image
ret2 = flip(ret2);
m = size(ret2, 1);
n = size(ret2, 2);
% combine the source image & the dim image
for i = 1:m
    for j = 1:n
        for k = 1:3
            output(i,j,k) = img2(i,j,k);
        end
    end
end
% combine the source image & the dim image
for i = 1:m
    for j = 1:n
        for k = 1:3
            output(i+m,j,k) = ret2(i,j,k);
        end
    end
end
subplot(1,2,1);
imshow(img2);
subplot(1,2,2);
imshow(output);

relievo.m:

对传进来的图像矩阵进行浮雕效果变换的函数。

 

function [ ret ] = relievo( img )
%RELIEVO transform the pictures to relievo style
    R = img(:,:,1); 
    G = img(:,:,2); 
    B = img(:,:,3); 
    m = size(img, 1);
    n = size(img, 2);
    
    for i = 1:m-5
        for j = 1:n-5
            RR(i,j) = R(i,j) - R(i+5,j+5) + 128;
            GG(i,j) = G(i,j) - G(i+5,j+5) + 128;
            BB(i,j) = B(i,j) - B(i+5,j+5) + 128;
        end
    end
    
    ret(:,:,1) = RR;
    ret(:,:,2) = GG;
    ret(:,:,3) = BB;
end

dim.m:

对传进来的图像矩阵进行模糊的函数,作为倒影。

 

function [ ret ] = dim( img )
%DIM transform the pictures to be dimed
    R = img(:,:,1); 
    G = img(:,:,2); 
    B = img(:,:,3); 
    m = size(img, 1);
    n = size(img, 2);
    
    for i = 1:m-20
        for j = 1:n-20
            deltax = fix(rand(1)*20);
            deltay = fix(rand(1)*20);
            ii = i + deltax;
            jj = j + deltay;
            RR(i,j) = R(ii,jj);
            GG(i,j) = G(ii,jj);
            BB(i,j) = B(ii,jj);
        end
    end
    
    ret(:,:,1) = RR;
    ret(:,:,2) = GG;
    ret(:,:,3) = BB;
end


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章