【數字圖像處理】浮雕效果和倒影效果

版權聲明:本文爲博主原創文章,歡迎轉載。轉載請註明出處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


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