CS 231A CV(winter 2014) problem set 3第一题第三小题(给定关键点及边框寻找最优边框)

  时光荏苒转眼大三下了,学习公开课已经快要两年了,断断续续做到这里,感觉效率非常低。...............0.0

回到正题,最近碰到的问题,一开始看不懂,反复看题目描述N遍,思考几天后突然开窍了,但是不太清楚最近做的对不对,希望有大神看到并不吝赐教。

题目描述:

Now given an object in an image, we want to explore how to find the same object in another image by matching the keypoints across these two images.

(i) A keypoint is specified by its coordinates, scale and orientaion (u,v,s,θ). Suppose that you have matched a keypoint in the bounding box of an object in the first image to a keypoint in a second image, as shown in Figure 3. Given the keypoint pair and the red bounding box in Image 1, which is specified by its center coordinates, width and height (x 1 ,y 1 ,w 1 ,h 1 ), find the predicted green bounding box of the same object in Image 2. Define the center position, width, height and relative orientation (x 2 ,y 2 ,w 2 ,h 2 ,o 2 ) of the predicted bounding box. Assume that the relation between a bounding box and a keypoint in it holds across rotation, translation and scale.
(ii) Once you have defined the five features of the new bounding box in terms of the two keypoint features and the original bounding box, briefly describe how you would utilize the Hough transform to determine the best bounding box in Image 2 given n correspondences.
(e) Implement the function getObjectRegion.m to recover the position, scale, and orientation of the objects (via calculating the bounding boxes) in the test images. You can use a coarse Hough transform by setting the number of bins for each dimension equal to 4. Your Hough space should be four dimensional.

解题思路其实挺简单的,就是找到两幅图片里面关键点之间的联系,再把这种联系扩展到两个矩形框上。因为一个第一幅图像里的特征点有可能会对应第二幅图像里面的多个特征点,因此会有多个匹配框,所以我们要用投票法选出第二幅图里面最优的匹配框(我选择特征点最多的框为最优框),这里我用粗略霍夫转换思想设定小块,再投票选出最优矩形框。两个矩形框的具体联系我已经写在代码里了。

function [cx, cy, w, h, orient, count] = getObjectRegion(keypt1, keypt2, matches, objbox, thresh)
% Get the center x-y (cx,cy), width and height (w,h), and orientation
% (orient) for each predicted object bounding box in the image.

% Find parameters for object bounding box
objx = mean(objbox([1 3])); % x-center
objy = mean(objbox([2 4])); % y-center
objw = objbox(3)-objbox(1);
objh = objbox(4)-objbox(2);

% YOUR CODE HERE
% Find parameters for keypoints in image 1
s1 = 0;
o1 = 0;
x1 = 0;
y1 = 0;
matchkeypt1=keypt1(:,matches(1,:));%关键点1(带重复)
x1 = matchkeypt1(1,:);
y1 = matchkeypt1(2,:);
s1 = matchkeypt1(3,:);
o1 = matchkeypt1(4,:);
% Find parameters for keypoints in image 2
s2 = 0;
o2 = 0;
x2 = 0;
y2 = 0;
matchkeypt2=keypt2(:,matches(2,:));%关键点2(与关键点1对应)
x2 = matchkeypt2(1,:);
y2 = matchkeypt2(2,:);
s2 = matchkeypt2(3,:);
o2 = matchkeypt2(4,:);
% Use four uniform bins for each dimension, vote from each keypoint and
% store all the bounding boxes whose votes is larger than thresh.

cx = []; cy = []; w = []; h = []; orient = []; count = [];

s = s2./s1;%关键点缩放倍数
w = objw.*s;%结果矩形框大小等于关键点缩放倍数跟给定矩形框之间的简单乘法
h = objh.*s;%同上
l = sqrt((x1-objx).^2+(y1-objy).^2).*s;%l为关键点到给定矩形框中心的距离乘上缩放倍数,即结果矩形框中心到对应特征点距离
d_theta = atan2(y1-objy,x1-objx)-o1;%直线l跟X轴的夹角
cx = l.*cos(d_theta+o2)+x2;%利用已知夹角求结果矩形框重点位置(横坐标)
cy = l.*sin(d_theta+o2)+y2;%利用已知夹角求结果矩形框重点位置(纵坐标)
orient = o2-o1;%结果矩形框与给定矩形框之间的夹角

nb = 4;%因为有x 2 ,y 2 ,w 2 ,h 2 这四个待确定的参数所以选择迭代次数为4(理论上可以增加o 2 ,但是没有必要)
bmq = ones(1,4);%编码器,我给每个维度设定了一个编码器,方便后续工作。只有编码器全为1的时候这个模型才是一个好的拟合,不然就舍去
p = cell(1,4);%把数据归类成4份
c = zeros(4,nb);%求得每份里面数据的个数
p{1} = assign2bins(cx, nb);%例如输入数据[2 2 3 5 4 1 9 6],结果为[1 1 1 2 2 1 4 3]
p{2} = assign2bins(cy, nb);
p{3} = assign2bins(w, nb);
p{4} = assign2bins(h, nb);
for i =1:nb
    c(i,:) = histc(p{i},1:nb);%计算1:4出现的次数,例如输入[1 1 1 2 2 1 4 3],结果为[4 2 1 1]
end
for i = 1:nb
    for j =1:nb
        if c(i,j)<thresh
            bmq(1,j) = 0;
%历遍1x4空间如果有任何一个小块里面的数据个数少于阈值(这里是5)的话对应编码器就变为0
        end
    end
end
count = 0;%计数器,纪录数据点的最大值
for i =1:nb
    summ = 0;
    if bmq(i)==1%判断编码器是否为1,是则进行以下步骤
        summ = sum(c(:,i));%纪录当下小块的数据点总个数
        if summ>count%如果总个数大于已知最大值那么纪录下当前的边框信息作为最优解
            count = summ;
            o_min = min(c(:,i));
            if i==1
                cx = sum(cx(1:c(1,1)))/c(1,i);%结果边框的中心坐标我用平均值取代
                cy = sum(cy(1:c(2,1)))/c(2,i);%这里大家可以用其他方法来获得结果边框的信息
                w = sum(w(1:c(3,1)))/c(3,i);%比如可以从候选边框里面选出最接近平均值的边框
                h = sum(h(1:c(4,1)))/c(4,i);
                orient = sum(orient(1:o_min))/o_min;
            else
                o_min = min(c(:,i));
                cx = sum(cx(sum(c(1:i-1)):sum(c(1:i))))/c(1,i);
                cy = sum(cx(sum(c(1:i-1)):sum(c(1:i))))/c(2,i);
                w = sum(cx(sum(c(1:i-1)):sum(c(1:i))))/c(3,i);
                h = sum(cx(sum(c(1:i-1)):sum(c(1:i))))/c(4,i);
                orient = sum(orient(sum(c(1:i-1)):sum(c(1:i))))/o_min;
            end
        end
    end
end

% Potentially useful function:
% creates nb uniform bins within range of x and assigns each x to a bin
% example usage:
% myhistogram = assign2bins(data_vector, number_of_bins)
% myhistogram is now an array of the same length as data_vector, in which
% all entries now correspond to their bin index.
function b = assign2bins(x, nb)
b = min(max(ceil((x-min(x))/(max(x)-min(x))*nb), 1), nb);
%nb=4;
一下为测试图片:

图片3(RANSAC拟合)


图片3(SIFT拟合)

此处的SIFT拟合为Lowe文章里面的两个最优拟合之比小于0.8即为好拟合

图片3(边框)


图片2(RANSAC拟合)


图片2(SIFT拟合)

图片2(边框)

图片4(RANSAC拟合


图片4( SIFT拟合


图片4(由于特征点太少所以没有出现结果矩形框)


图片5(RANSAC拟合)


图片5(SIFT拟合)


图片5(边框)

从拟合结果来看该算法待优化的空间还很大,希望大家能够给予改进的意见!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值