在MATLAB中声明和初始化对象

时间:2017-05-06 08:37:12

标签: matlab

我想在一个窗口中显示12张图片。

%   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 个答案:

答案 0 :(得分: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');