簡體   English   中英

在MATLAB中給定約束條件的情況下隨機選擇子矩陣

[英]Selecting submatrices at random given a set of constraints in MATLAB

這里是編程的新手,我真的不知道該如何處理。

我的問題:我有一堆用矩形邊界框注釋的圖像,想要隨機選擇其他與我已有的邊界框不重疊的矩形邊界框。 因此,基本上,我有一個矩陣M和一個M子矩陣的預定義子集X,我想生成具有不與X重疊但可以相互重疊的隨機位置的新子矩陣。 生成的子矩陣的大小應與X中的子矩陣大小相同,並包含在矩陣M中。

在此處輸入圖片說明

http://i.imgur.com/74AEI2x.png

在上面的示例中,該圖像中的方框表示X,即足球的正序; 我想生成相等數量的相同大小的框,它們不包含足球,以表示足球的負面示例。

任何方向都值得贊賞,我正在MATLAB中進行此操作。

讓我們仔細閱讀代碼和注釋,並嘗試了解如何實現問題中設定的目標。

%// Bounding box array, where the first and second columns denote the X-Y 
%// location of the uppper-left corner pixel. The third and fourth columns
%// denote the extent of the repctive boxes along X and Y directions 
%// respectively. Some random values are used here for demo purposes.
bb = [
    12 10 10 5
    15 20 14 12
    135 60 11 4
    20 30 10 7
    20 30 13 13
    20 30 13 14]

%// Tolerance in terms of the size difference betwen similar boxes that
%// is admissible as a less than or equal to value
tol = 2

%// Get X and Y direction limits for each box
xlims = [bb(:,1) bb(:,1) + bb(:,3)]
ylims = [bb(:,2) bb(:,2) + bb(:,4)];

%// Create a binary matrix that decides whether each box is in or out with
%// respect to all other boxes along both X and Y directions. Ones mean "in"
%// and zeros denote "out".
x1 = bsxfun(@ge,xlims(:,1),xlims(:,1)') & bsxfun(@le,xlims(:,1),xlims(:,2)')
x2 = bsxfun(@ge,xlims(:,2),xlims(:,1)') & bsxfun(@le,xlims(:,2),xlims(:,2)')
x12 = x1 | x2;

y1 = bsxfun(@ge,ylims(:,1),ylims(:,1)') & bsxfun(@le,ylims(:,1),ylims(:,2)')
y2 = bsxfun(@ge,ylims(:,2),ylims(:,1)') & bsxfun(@le,ylims(:,2),ylims(:,2)')
y12 = y1 | y2;

d1 = x12 & y12

%// Create another binary matrix based on sizes to decide for each box
%// what other boxes are "similar"
szmat = bb(:,[3 4])
v1 = abs(bsxfun(@minus,szmat,permute(szmat,[3 2 1])));
szmat_d = squeeze(all(v1<=tol,2));

%// Get a binary matrix based on combined decisions from X-Y incompatibility
%// and sizes. Please note for incompatibility, negation of d1 is needed.
out1 = ~d1 & szmat_d
out1(1:size(out1,1)+1:end)=0
out2 = mat2cell(out1,ones(1,size(out1,1)),size(out1,2))
out3 = cellfun(@find,out2,'uni',0)

如何使用代碼-

out3是容納每個框的最終決策的最終輸出,還有哪些其他框相似且不重疊。 為了進行驗證,讓我們通過執行out3{1}查看其他哪些框符合框1的這些條件。 它打印出34 ,這意味着框3和4就是框1的框。這可以通過查看邊界框bb中的值來手動驗證。

感謝您的幫助Divakar。

我還提供了自己的解決方案,以供將來參考:

% Loop through each image in the dataset
for i=1:numel(anno.image_names)

% Skip images with no positive examples
inds = anno.bboxes(:,1) == i;
if sum(inds) == 0
    continue
end

% Read image
img = imread(fullfile(folder,anno.image_names{i}));

% Query image size
w_img = size(img,2);
h_img = size(img,1);

% Define the size of new negative bboxes to be the mean of the size of
% all positive bboxes in the image
w_new = floor(mean(anno.bboxes(inds,5)));
h_new = floor(mean(anno.bboxes(inds,6)));

% Define top-left and bottom-right corner of each existing bbox
tmp_bboxes = anno.bboxes(inds,:);
x = floor(tmp_bboxes(:,3));
y = floor(tmp_bboxes(:,4));
x_max = ceil(tmp_bboxes(:,3)+tmp_bboxes(:,5));
y_max = ceil(tmp_bboxes(:,4)+tmp_bboxes(:,6));

% Choose a random origin (a,b) that represents the upper-left
% corner pixel location and is properly constrained by the image size
a = randi([1,w_img - w_new],[sum(inds), 1]);
b = randi([1,h_img - h_new],[sum(inds),1]);

% Check if for the origin (a,b), the bbox created by the origin and
% w_new, h_new overlaps with the positive bboxes
inds2 = (a<x & (a+w_new)<x) | (a>x_max & (a+w_new)>x_max) | ...
    (b<y & (b+h_new)<y) | (b>y_max & (b+h_new)>y_max);
while sum(inds2) < sum(inds)
    a(~inds2) = randi([1,w_img - w_new],[sum(~inds2), 1]);
    b(~inds2) = randi([1,h_img - h_new],[sum(~inds2),1]);
    inds2 = (a<x & (a+w_new)<x) | (a>x_max & (a+w_new)>x_max) | ...
    (b<y & (b+h_new)<y) | (b>y_max & (b+h_new)>y_max);
end

end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM