MATLAB 图像处理(1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %测量交角(线性拟合的思想) %Step 1: Load image %Step 2: Extract the region of interest %Step 3: Threshold the image %Step 4: Find initial point on each boundary %Step 5: Trace the boundaries %Step 6: Fit lines to the boundaries %Step 7: Find the angle of intersection %Step 8: Find the point of intersection %Step 9: Plot the results. clc;clear;close all; %Step 1: Load image RGB = imread('gantrycrane.png'); imshow(RGB); %Step 2: Extract the region of interest % pixel information displayed by imview start_row = 34; start_col = 208; cropRGB = RGB(start_row:163, start_col:400, :); figure, imshow(cropRGB) % Store (X,Y) offsets for later use; subtract 1 so that each offset will % correspond to the last pixel before the region of interest offsetX = start_col-1; offsetY = start_row-1; %Step 3: Threshold the image I = rgb2gray(cropRGB); threshold = graythresh(I); BW = im2bw(I,threshold); BW = ~BW; % complement the image (objects of interest must be white) figure, imshow(BW) %Step 4: Find initial point on each boundary dim = size(BW); % horizontal beam col1 = 4; row1 = min(find(BW(:,col1))); % angled beam row2 = 12; col2 = min(find(BW(row2,:))); %Step 5: Trace the boundaries boundary1 = bwtraceboundary(BW, [row1, col1], 'N', 8, 70); % set the search direction to counterclockwise, in order to trace downward. boundary2 = bwtraceboundary(BW, [row2, col2], 'E', 8, 90,'counter'); figure, imshow(RGB); hold on; % apply offsets in order to draw in the original image plot(offsetX+boundary1(:,2),offsetY+boundary1(:,1),'g','LineWidth',2); plot(offsetX+boundary2(:,2),offsetY+boundary2(:,1),'g','LineWidth',2); %Step 6: Fit lines to the boundaries ab1 = polyfit(boundary1(:...