[81c116]: / detect_wall_boundary_cropped_image.m

Download this file

46 lines (36 with data), 1.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
% This function detects the boundary of the image.
% The boundary is assumes to be a straight line.
% Region = "Proximal" or "Distal"
% Output wall location on the image is the format [x_start x_end y_start y_end]
function [ Wall_detect, valid_cols, valid_rows] = detect_wall_boundary_cropped_image(Crop_Image, region)
Wall_detect = nan;
% Perform edge detection
BW = edge(Crop_Image);
% Find the region of the figure with valid rows.
valid_cols = find(~all(BW == 0))';
% If the length of the valid_rows is less than 20 pixels, ignore the frame
if(length(valid_cols) < 20)
valid_cols = [];
valid_rows = [];
end
% Find the row index of the boundary
% If the input is proximal wall, wall is assumed to be in the bottom of the
% croped image. If the input is distal wall, wall is assumed to be in the top of the
% cropped image. This assumption helps to speed up the code via vectorization.
switch (region)
case 'Proximal'
[~, valid_rows] = max(flipud(BW(:, valid_cols)));
valid_rows = (size(Crop_Image, 1) - valid_rows + 1)';
case 'Distal'
[~, valid_rows] = max(BW(:, valid_cols));
valid_rows = valid_rows';
end
% Fit a straight line
% p_fit = polyfit(valid_cols, valid_rows, 1);
% Estimated_valid_row = floor(polyval(p_fit, valid_cols));
% Wall_detect = [valid_cols(1) valid_cols(end) Estimated_valid_row(1) Estimated_valid_row(end)];
% % Plot the data
% figure(); imshow(Crop_Image); hold on;
% plot(valid_cols, valid_rows, 'og');
% line(Wall_detect(1:2), Wall_detect(3:4), 'Color', 'r', 'LineStyle', '-', 'LineWidth', 3);
end