-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
2,812 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function ret_box = ConvertBox4dToBox8d( box ) | ||
x1 = box(1); | ||
y1 = box(2); | ||
x2 = box(1) + box(3) - 1; | ||
y2 = box(2); | ||
x3 = box(1) + box(3) - 1; | ||
y3 = box(2) + box(4) - 1; | ||
x4 = box(1); | ||
y4 = box(2) + box(4) - 1; | ||
ret_box = [y1, x1; y2, x2; y3, x3; y4, x4]; | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function retBox = ConvertBox8dToBox4d( bbox_eightPixel ) | ||
retBox = [min(bbox_eightPixel(2,:)), ... | ||
min(bbox_eightPixel(1,:)), ... | ||
max(bbox_eightPixel(2,:)) - min(bbox_eightPixel(2,:)) + 1, ... | ||
max(bbox_eightPixel(1,:)) - min(bbox_eightPixel(1,:)) + 1]; | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function nms_idx = box_nms(boxes, scores, area_threashold) | ||
[~, sort_idx] = sort(scores, 'descend'); | ||
boxes = boxes(sort_idx, :); | ||
area = boxes(:,3) .* boxes(:,4); | ||
|
||
nms_flag = ones(size(boxes, 1), 1); | ||
for i = 1 : size(boxes,1) | ||
if(nms_flag(i) == 0) | ||
continue; | ||
end | ||
|
||
int_area = rectint(boxes(i, :), boxes)'; | ||
area_ratio = int_area ./ (area + area(i) - int_area); | ||
area_ratio(1 : i) = 0; | ||
nms_flag(area_ratio > area_threashold) = 0; | ||
end | ||
|
||
nms_idx = sort_idx(logical(nms_flag)); | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function [ rectX, rectY, area, perimeter, sideLength] = computeMergedBox( boxes, append_pixels ) | ||
nBoxes = size(boxes, 1); | ||
compPoints = zeros(nBoxes* 4, 2); | ||
for nComp = 1 : size(boxes,1) | ||
loopBox = boxes(nComp, :); | ||
compPoints((nComp - 1) * 4 + 1, :) = [loopBox(2), loopBox(1)]; | ||
compPoints((nComp - 1) * 4 + 2, :) = [loopBox(2), loopBox(1) + loopBox(3) - 1]; | ||
compPoints((nComp - 1) * 4 + 3, :) = [loopBox(2) + loopBox(4) - 1, loopBox(1) + loopBox(3) - 1]; | ||
compPoints((nComp - 1) * 4 + 4, :) = [loopBox(2) + loopBox(4) - 1, loopBox(1)]; | ||
end | ||
|
||
if(nargin == 2) | ||
compPoints = cat(1, compPoints, append_pixels); | ||
end | ||
|
||
[rectX, rectY, area, perimeter] = minboundrect(compPoints(:, 2), compPoints(:, 1)); | ||
|
||
sideLength = ((rectX(2 : 5) - rectX(1 : 4)) .^ 2 + (rectY(2 : 5) - rectY(1 : 4)).^2).^0.5; | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
function [ rectX, rectY, area, perimeter, sideLength] = computeMergedBoxByOrientation(img, pixels, estimated_orientation) | ||
%% Compute convex hull | ||
DT = delaunayTriangulation(pixels(:, 2), pixels(:, 1)); | ||
convexPointIdx = convexHull(DT); | ||
convexPoints = DT.Points(convexPointIdx, :); | ||
convexPoints = convexPoints(:, [2 1]); | ||
% imshow(img); | ||
% hold on; | ||
% plot(convexPoints(:, 2), convexPoints(:, 1)); | ||
% hold off; | ||
|
||
%% Compute centralized pixels | ||
cx = mean(convexPoints(:, 2)); | ||
cy = mean(convexPoints(:, 1)); | ||
|
||
centralizedPixels = pixels; | ||
centralizedPixels(:, 1) = centralizedPixels(:, 1) - cy; | ||
centralizedPixels(:, 2) = centralizedPixels(:, 2) - cx; | ||
|
||
%% compute rotate pixels | ||
rotate_mat = [cosd(estimated_orientation), -sind(estimated_orientation); sind(estimated_orientation), cosd(estimated_orientation)]; | ||
centralizedPixels = centralizedPixels(:, [2, 1]); | ||
rotatePixels = (rotate_mat * centralizedPixels')'; | ||
|
||
%% swap X and Y | ||
rotatePixels = rotatePixels(:, [2, 1]); | ||
centralizedPixels = centralizedPixels(:, [2, 1]); | ||
|
||
%% compute min_box of rotate pixels | ||
rotate_min_box = [min(rotatePixels(:,2)), ... | ||
min(rotatePixels(:,1)), ... | ||
max(rotatePixels(:,2)) - min(rotatePixels(:,2)), ... | ||
max(rotatePixels(:,1)) - min(rotatePixels(:,1))]; | ||
rotate_min_box8p = ConvertBox4dToBox8d(rotate_min_box); | ||
rotate_min_box8p = getRotateBox8D(rotate_min_box8p, -estimated_orientation, 0, 0); | ||
|
||
|
||
|
||
rectX = [rotate_min_box8p(:, 2); rotate_min_box8p(1, 2)] + cx; | ||
rectY = [rotate_min_box8p(:, 1); rotate_min_box8p(1, 1)] + cy; | ||
|
||
area = polyarea(rectX, rectY); | ||
sideLength = ((rectY(2 : 5) - rectY(1 : 4)) .^ 2 + (rectX(2 : 5) - rectX(1 : 4)) .^ 2) .^ 0.5; | ||
perimeter = sum(sideLength); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function orientationDiff = computeOrientationDiff(pixels, relativePixel, estimated_orientation) | ||
diffVector = zeros(size(pixels,1), 2); | ||
diffVector(:, 1) = pixels(:, 1) - relativePixel(1); | ||
diffVector(:, 2) = pixels(:, 2) - relativePixel(2); | ||
|
||
atanDiff = - atan(diffVector(: ,1) ./ (diffVector(:, 2) + eps)) * 180 / pi; | ||
|
||
orientationDiff = abs(atanDiff - estimated_orientation); | ||
orientationDiff = min(180 - orientationDiff, orientationDiff); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
function orientation = estimateOrientation(img, region, region_comp_infos, orient_param) | ||
global param | ||
|
||
%% return if region_comp_infos is empty | ||
if isempty(region_comp_infos) | ||
orientation = NaN; | ||
return | ||
end | ||
|
||
if length(region_comp_infos) == 1 | ||
orientation = 0; | ||
return; | ||
end | ||
|
||
if(orient_param.minOrientation == orient_param.maxOrientation) | ||
orientation = 0; | ||
return; | ||
end | ||
|
||
box8d = cell(length(region_comp_infos), 1); | ||
for i = 1 : length(region_comp_infos) | ||
box8d{i} = ConvertBox4dToBox8d(region_comp_infos{i}.box); | ||
end | ||
|
||
if(false && param.debug) | ||
debug_box = zeros(length(region_comp_infos), 4); | ||
for d_i = 1 : length(region_comp_infos) | ||
debug_box(d_i,:) = region_comp_infos{d_i}.box; | ||
end | ||
show_bbox(img, debug_box); | ||
end | ||
|
||
region_cx = mean(region(:,2)); | ||
region_cy = mean(region(:,1)); | ||
|
||
orientations = [orient_param.minOrientation : orient_param.orientationInterval : orient_param.maxOrientation]; | ||
hitBoxCount = zeros(length(orientations), 1); | ||
|
||
|
||
y1_arr = zeros(length(region_comp_infos), 1); | ||
y2_arr = zeros(length(region_comp_infos), 1); | ||
for i = 1 : length(orientations) | ||
|
||
for j = 1 : length(region_comp_infos) | ||
rotate_box8d = getRotateBox8D(box8d{j}, orientations(i), region_cx, region_cy); | ||
|
||
y1_arr(j) = min(rotate_box8d(:,1)); | ||
y2_arr(j) = max(rotate_box8d(:,1)); | ||
|
||
y_offest_arr = y2_arr(j) - y1_arr(j) + 1; | ||
y1_arr(j) = y1_arr(j) + floor(0.3 * y_offest_arr); | ||
y2_arr(j) = y2_arr(j) - floor(0.3 * y_offest_arr); | ||
end | ||
|
||
min_y_arr = min(y1_arr); | ||
y_map = zeros(max(y2_arr) - min_y_arr + 1, 1); | ||
for j = 1 : length(region_comp_infos) | ||
y_map(y1_arr(j) - min_y_arr + 1 : y2_arr(j) - min_y_arr + 1) = ... | ||
y_map(y1_arr(j) - min_y_arr + 1 : y2_arr(j) - min_y_arr + 1) + 1; | ||
end | ||
|
||
hitBoxCount(i) = max(y_map); | ||
|
||
if false && param.debug | ||
imshow(img); | ||
hold on; | ||
for k = 1 : length(region_comp_infos) | ||
cy = mean(box8d{k}(:,1)); | ||
cx = mean(box8d{k}(:,2)); | ||
|
||
plot(cx,cy, '*', 'color', 'y'); | ||
|
||
rotate_box8d = getRotateBox8D(box8d{k}, orientations(i), region_cx, region_cy); | ||
cy = mean(rotate_box8d(:,1)); | ||
cx = mean(rotate_box8d(:,2)); | ||
plot(cx,cy, '*', 'color', 'r'); | ||
|
||
plot([rotate_box8d(:, 2); rotate_box8d(1, 2)], [rotate_box8d(:, 1); rotate_box8d(1, 1)]); | ||
end | ||
hold off; | ||
end | ||
end | ||
|
||
max_hit_box = max(hitBoxCount); | ||
|
||
%% calc_continues | ||
continueScore = zeros(length(hitBoxCount), 1); | ||
if(hitBoxCount(length(hitBoxCount)) == max_hit_box) | ||
continueScore(length(hitBoxCount)) = 1; | ||
end | ||
|
||
for i = length(hitBoxCount) - 1 : -1 : 1 | ||
if(hitBoxCount(i) == max_hit_box) | ||
continueScore(i) = continueScore(i+1) + 1; | ||
else | ||
continueScore(i) = 0; | ||
end | ||
end | ||
|
||
left_most = find(continueScore == max(continueScore)); | ||
if(length(left_most) > 1) | ||
[~, middlest] = min(abs(left_most - length(hitBoxCount))); | ||
left_most = left_most(middlest); | ||
end | ||
|
||
right_most = left_most; | ||
len_max_hit_box = 1; | ||
|
||
while true | ||
i = left_most - 1; | ||
if(i == 0) | ||
i = length(hitBoxCount); | ||
end | ||
if(hitBoxCount(i) == max_hit_box && right_most ~= i) | ||
left_most = i; | ||
len_max_hit_box = len_max_hit_box + 1; | ||
else | ||
break; | ||
end | ||
end | ||
|
||
while true | ||
i = right_most + 1; | ||
if(i >= length(hitBoxCount)) | ||
i = 1; | ||
end | ||
if(hitBoxCount(i) == max_hit_box && left_most ~= i) | ||
right_most = i; | ||
len_max_hit_box = len_max_hit_box + 1; | ||
else | ||
break; | ||
end | ||
end | ||
|
||
max_orientation_idx = left_most + round(len_max_hit_box/2); | ||
if max_orientation_idx > length(hitBoxCount) | ||
max_orientation_idx = max_orientation_idx - length(hitBoxCount); | ||
end | ||
orientation = orientations(max_orientation_idx); | ||
|
||
if param.debug | ||
imshow(img); | ||
hold on; | ||
for k = 1 : length(region_comp_infos) | ||
cy = mean(box8d{k}(:,1)); | ||
cx = mean(box8d{k}(:,2)); | ||
|
||
plot(cx,cy, '*', 'color', 'y'); | ||
|
||
rotate_box8d = getRotateBox8D(box8d{k}, orientation, region_cx, region_cy); | ||
|
||
cy = mean(rotate_box8d(:,1)); | ||
cx = mean(rotate_box8d(:,2)); | ||
|
||
plot(cx,cy, '*', 'color', 'r'); | ||
end | ||
hold off; | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function [width, height] = findWidthAndHeight(rectX, rectY, sideLenght, estimated_orientation) | ||
pixels = zeros(4, 2); | ||
for n = 1 : 4 | ||
pixels(n, :) = [(rectY(n) + rectY(n+1))/2, (rectX(n) + rectX(n+1))/2]; | ||
end | ||
orientationDiff1 = computeOrientationDiff(pixels(1, :), pixels(3, :), estimated_orientation); | ||
orientationDiff2 = computeOrientationDiff(pixels(2, :), pixels(4, :), estimated_orientation); | ||
if(orientationDiff1 > orientationDiff2) | ||
width = round(0.5 * (sideLenght(1) + sideLenght(3))); | ||
height = round(0.5 * (sideLenght(2) + sideLenght(4))); | ||
else | ||
width = round(0.5 * (sideLenght(2) + sideLenght(4))); | ||
height = round(0.5 * (sideLenght(1) + sideLenght(3))); | ||
end | ||
end |
Oops, something went wrong.