1. 程式人生 > >一種importance map的生成方法

一種importance map的生成方法

%這段程式碼利用被試的滑鼠點選資料生成一個importance map,顏色越深表示該區域滑鼠點選的次數越多

%模擬被試的點選資料
%生成滑鼠點選點,分四個區域,每個區域中滑鼠點選的數量不同
point_x_one=480:10:530;
point_y_one=280:10:330;
[one_x,one_y]=meshgrid(point_x_one,point_y_one);
point_one=[one_x(:),one_y(:)];


point_x_two=80:10:120;
point_y_two=80:10:120;
[two_x,two_y]=meshgrid(point_x_two,point_y_two);
point_two=[two_x(:),two_y(:)];

point_x_three=290:10:310;
point_y_three=290:10:310;
[three_x,three_y]=meshgrid(point_x_three,point_y_three);
point_three=[three_x(:),three_y(:)];

point_four=[560,380;570,380;550,380;570,370;560,370;550,370];

point={point_one,point_two,point_three,point_four};


%初始化與原影象大小相等的權重圖
A = imread('test.png'); 
[a,b,c]=size(A);
weight=zeros(a,b);

%指定bubble size大小為 sigema=32
%由滑鼠點選得到權重分佈圖
sigema=32;
for i=1:4;
    [m,n]=size(point{i});
    for j=1:m;
        for y_cordinate=point{i}(j,2)-32:point{i}(j,2)+32
            left_x=point{i}(j,1)-floor(sqrt(sigema^2-(y_cordinate-point{i}(j,2))^2));
            right_x=point{i}(j,1)+floor(sqrt(sigema^2-(y_cordinate-point{i}(j,2))^2));
            for x_cordinate=left_x:right_x
                weight(x_cordinate,y_cordinate)=weight(x_cordinate,y_cordinate)+32-floor(sqrt((x_cordinate-point{i}(j,1))^2+(y_cordinate-point{i}(j,2))^2));
            end
        end
    end       
end


%生成原始熱圖啦
cmap = colormap(jet(256));
rgb = ind2rgb(weight,cmap);
subplot(1,2,1);
imshow(rgb);

%經過高斯濾波進行處理後的熱圖
w = fspecial('gaussian',[8,8],24);
newmap= imfilter(rgb,w,'replicate');
subplot(1,2,2);
imshow(newmap);

imwrite(rgb,'heatMap.png')

結果:

在python中呼叫OpenCV,將熱圖分佈貼在原圖上:

#-*-coding:utf-8-*-
import cv2

img = cv2.imread('test.png', 1)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
heatmap_img=cv2.imread('heatMap.png', 1)
fin = cv2.addWeighted(heatmap_img, 0.5, img, 0.5, 0)

cv2.imshow('image',fin)
cv2.waitKey(0)
cv2.destroyAllWindows()

結果:

過程中用到的資料:

test.png

heatMap.png