2017-05-06 57 views
-1

我想在一個窗口中顯示12個圖像。在MATLAB中聲明和初始化對象

% Read 12 images into workspace. 
input_images = {imread('1.png'),imread('2.png'),imread('3.png'),... 
    imread('4.png'),imread('5.png'),imread('6.png'),... 
    imread('7.png'),imread('8.png'),imread('9.png'),... 
    imread('10.png'),imread('11.png'),imread('12.png')}; 

longest_line = []; 

for n=1:12 
    %Create a binary image. 
    binary_image = edge(input_images{n},'canny'); 

    %Create the Hough transform using the binary image. 
    [H,T,R] = hough(binary_image); 

    %Find peaks in the Hough transform of the image. 
    P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:)))); 

    %Find lines 
    hough_lines = houghlines(binary_image,T,R,P,'FillGap',5,'MinLength',7);  

    % Plot the detected lines 
    figure, imshow(binary_image), hold on 
    max_len = 0; 

    longest_line = FindTheLongestLine(hough_lines, longest_line); 
end 

% Highlight the longest line segment by coloring it cyan. 
plot(longest_line.point1, longest_line.point2,'LineWidth',2,'Color','cyan'); 

第一個問題是,它生成以下錯誤,

>> main2 
Attempt to reference field of non-structure array. 

Error in LenthOfLine (line 4) 
    length = norm(line.point1 - line.point2); 

Error in FindTheLongestLine (line 15) 
    if(LenthOfLine(old_longest_line) > LenthOfLine(longest_line)) 

Error in main2 (line 26) 
     longest_line = FindTheLongestLine(hough_lines, longest_line); 

>> 

這意味着,longest_line對象還沒有被正確初始化。

我該如何解決這些問題?

相關的源碼

function longest_line = FindTheLongestLine(hough_lines , old_longest_line) 
%FINDTHELONGESTLINE Summary of this function goes here 
% Detailed explanation goes here 
    max_len = 0; 
    for i = 1:length(hough_lines) 
     % Determine the endpoints of the longest line segment 
     len = LenthOfLine(hough_lines(i)); 

     if (len > max_len) 
      max_len = len; 
      longest_line = hough_lines(i); 
     end 
    end 

    if(LenthOfLine(old_longest_line) > LenthOfLine(longest_line)) 
     longest_line = old_longest_line; 
    end 
end 

function length = LenthOfLine(line) 
%LENTHOFLINE Summary of this function goes here 
% Detailed explanation goes here 
    length = norm(line.point1 - line.point2); 
end 
+1

請務必先嚐試使用matlab調試器:在包含錯誤的行放置一個斷點,並檢查相關變量。試着瞭解錯誤發生的原因。 – m7913d

回答

0

好。我知道了。

longest_line = struct('point1',[0 0], 'point2',[0 0], 'theta', 0, 'rho', 0); 

該初始化解決了該問題。

% Read 12 images into workspace. 
input_images = {imread('1.png'),imread('2.png'),imread('3.png'),... 
    imread('4.png'),imread('5.png'),imread('6.png'),... 
    imread('7.png'),imread('8.png'),imread('9.png'),... 
    imread('10.png'),imread('11.png'),imread('12.png')}; 

longest_line = struct('point1',[0 0], 'point2',[0 0], 'theta', 0, 'rho', 0); 

for n=1:12 
    %Create a binary image. 
    binary_image = edge(input_images{n},'canny'); 

    %Create the Hough transform using the binary image. 
    [H,T,R] = hough(binary_image); 

    %Find peaks in the Hough transform of the image. 
    P = houghpeaks(H,3,'threshold',ceil(0.3*max(H(:)))); 

    %Find lines 
    hough_lines = houghlines(binary_image,T,R,P,'FillGap',5,'MinLength',7);   
    longest_line = FindTheLongestLine(hough_lines, longest_line); 
end 


% Highlight the longest line segment by coloring it cyan. 
plot(longest_line.point1, longest_line.point2,'LineWidth',2,'Color','cyan'); 
+0

你能總結一下你的解決方案嗎?現在,您必須逐行比較您的代碼才能找到解決方案。 – m7913d