试图检测边缘曲线拟合的火焰的图像分析并不总是起作用MatLab

时间:2017-09-15 16:18:44

标签: matlab image-processing curve-fitting edge-detection

我试图找出如何在Matlab中制作一个程序来检测火焰边缘,以便我可以比较它们的火焰形状。我有一半时间能够让这个程序工作。程序读取文件,将其转换为黑白,然后我遇到bwboundaries函数问题(我认为)。

根据图片,它会工作一段时间而不是其他工作,它们可以在下面看到。

任何人都可以给我的任何帮助将不胜感激。我在这里努力弄清楚我做错了什么。

clear, clc , close all
%% Specify the folder where the files live.
%myFolder = 'D:\Try';
myFolder = 'D:\Try\PicNamed\SpTestTry\hope';

%% Check to make sure that folder actually exists.  
if ~isdir(myFolder)
    errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
    uiwait(warndlg(errorMessage));
    return;
end
%% Get a list of all files in the folder with the pattern
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever need.
theFiles = dir(filePattern);

line = cell(length(theFiles), 1) ;
rValue = cell(length(theFiles),1);

baseFileName = theFiles(1).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);  
imageArray = imread(fullFileName); 

% getting the rectanglur cords to crop the images
[I2, rect] = imcrop(imageArray);

%% Run Loop
for k = 1 : length(theFiles)

    %Pulling in multiple Images from folder 
    baseFileName = theFiles(k).name;
    fullFileName = fullfile(myFolder, baseFileName);
    fprintf(1, 'Now reading %s\n', fullFileName);  
    imageArray = imread(fullFileName); 


    %%  Cropping Image with Rect Cords
    Icrop = imcrop(imageArray,rect);  

    %%  convert to bw
    BW = im2bw(Icrop, graythresh(Icrop));

    %%   Get Boundries 
    boundaries = bwboundaries(BW);
    % putting in x and y cord
    x = boundaries{1}(:, 2);
    y = boundaries{1}(:, 1);  

    %%  Curve Fit
    %pulling up picture of x,y plot to determing where base of flame is
    figure
    plot(x, y)

    % cutting off bottom of flame edge so we can do curve fit
    prompt = '(******) cut off line = ';
    cutnum = input(prompt);

    indices = find(abs(y)>cutnum);
    y(indices) = NaN;
    y(indices) = [];
    x(indices) = [];

    %% Looking for repate x values, averaging them together, and replacing with 1 x value

    uc1 = unique( x ) ;
    mc2 = accumarray( x, y, [], @mean ) ;
    newData = [uc1, mc2(uc1)];

    %% splitting AA into two sperate matricies, i.e. x/y cordinates
    y = newData(:,2);
    x = newData(:,1);

    %% Creating a line Based off of the averaged x and y cords
    [p,S] = polyfit(x,y,4); % 4th degree polynomail
    line{k} = p;
    rValue{k} = S.normr;
    Rsquared{k}=1-S.normr^2/norm(y-mean(y))^2;

    close all

    clear BW
    clear x
    clear y
end 

Pic1

Pic2

0 个答案:

没有答案