在 MATLAB 中批处理图像文件

发布于 2024-08-15 13:41:16 字数 1965 浏览 10 评论 0 原文

我是 MATLAB 和图像处理的初学者。

我在尝试使用批处理时遇到了问题,希望有人能够启发我。谢谢。

按照 MATLAB 中的示例,我执行了以下操作:

p = which('Picture1.tif');
filelist = dir([fileparts(p) filesep 'Picture*.tif']);
fileNames = {filelist.name}'

I = imread(fileNames{1});
imshow(I)

因为我想选择感兴趣的区域,所以

BW = roipoly(I);
BW1 = not(BW);
N = roifill(I,BW1);

在选择 ROI 后,我在编辑器中创建了一个函数:

function Segout = DetectLines(N)
    [junk threshold] = edge(N, 'sobel');
    fudgeFactor = .5;
    BWs = edge(N, 'sobel', threshold*fudgeFactor);
    se90 = strel('line', 3, 90);
    se0 = strel('line', 3, 0);
    BWsdil = imdilate(BWs, [se90 se0]);
    BWdfill = imfill(BWsdil, 'holes');
    BWnobord = imclearborder(BWdfill, 4);
    seD = strel('diamond', 1);
    BWfinal = imerode(BWnobord, seD);
    BWfinal = imerode(BWfinal, seD);
    BWoutline = bwperim(BWfinal);
    Segout = N;
    Segout(BWoutline) = 255;
end

返回到命令窗口,我键入;

Segout = DetectLines(N);
figure, imshow(Segout)

出来的数字正是我所期望的。

当我尝试循环图像时,问题就出现了。我不确定我是否做到了 正确。

按照这个例子,我在编辑器中创建了另一个函数;

function SegoutSequence = BatchProcessFiles(fileNames, fcn)
    N = imread(fileNames{1});
    [mrows, ncols] = size(N);
    nImages = length(fileNames);
    SegoutSequence = zeros(mrows, ncols, nImages, class(N));
    parfor (k = 1:nImages)
        N = imread(fileNames{k});
        SegoutSequence(:,:,k) = fcn(N);
    end
end

在命令窗口中,我输入:

SegoutSequence = BatchProcessFiles(fileNames, @DetectLines);
implay(SegoutSequence)

但是,结果并不是我想要的。这不是我想要的投资回报率。谁能帮我解决这个问题吗?非常感谢。

图片1:

选择ROI后的图片1:

替代文本

I'm a beginner in MATLAB and image processing.

I encountered a problem while trying to use the batch processing and hope someone would be able to enlighten me. Thanks.

Following the example from MATLAB, I did these:

p = which('Picture1.tif');
filelist = dir([fileparts(p) filesep 'Picture*.tif']);
fileNames = {filelist.name}'

I = imread(fileNames{1});
imshow(I)

Because I wanted to select the region of interest,

BW = roipoly(I);
BW1 = not(BW);
N = roifill(I,BW1);

After I selected the ROI, I created a function in the editor:

function Segout = DetectLines(N)
    [junk threshold] = edge(N, 'sobel');
    fudgeFactor = .5;
    BWs = edge(N, 'sobel', threshold*fudgeFactor);
    se90 = strel('line', 3, 90);
    se0 = strel('line', 3, 0);
    BWsdil = imdilate(BWs, [se90 se0]);
    BWdfill = imfill(BWsdil, 'holes');
    BWnobord = imclearborder(BWdfill, 4);
    seD = strel('diamond', 1);
    BWfinal = imerode(BWnobord, seD);
    BWfinal = imerode(BWfinal, seD);
    BWoutline = bwperim(BWfinal);
    Segout = N;
    Segout(BWoutline) = 255;
end

Back to the command window, I typed;

Segout = DetectLines(N);
figure, imshow(Segout)

The figure that came out was what I expected.

The problem comes now when I try to loop over images. I'm not sure if I've done it
correctly.

Following the example, I created another function in the editor;

function SegoutSequence = BatchProcessFiles(fileNames, fcn)
    N = imread(fileNames{1});
    [mrows, ncols] = size(N);
    nImages = length(fileNames);
    SegoutSequence = zeros(mrows, ncols, nImages, class(N));
    parfor (k = 1:nImages)
        N = imread(fileNames{k});
        SegoutSequence(:,:,k) = fcn(N);
    end
end

At the command window, I typed:

SegoutSequence = BatchProcessFiles(fileNames, @DetectLines);
implay(SegoutSequence)

However, the result was not what I wanted it to be. It was not the ROI that I wanted. Can anyone help me with this? Thank you very much.

Picture1:

alt text

Picture1 after selecting ROI:

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-08-22 13:41:16

查看您的代码,您只为单独测试的图像选择了一次 ROI。

但是,当您调用BatchProcessFiles函数时,您没有选择感兴趣的区域,并且DetectLines函数应用于原始图像......因此您需要传递使用 roipoly 创建的掩码到您的 BatchProcessFiles 函数,以对所有图像执行相同的操作。

附带说明一下,如果您尝试霍夫变换来检测线条,您可能会获得更好的结果。
如果图像不是灰度图像,您的代码也会中断(为了安全起见,您可以添加对 rgb2gray() 的调用)

示例解决方案:

MATLAB

I = imread(fileNames{1});
BW = not( roipoly(I) );

SegoutSequence = BatchProcessFiles(fileNames, BW, @DetectLines);
implay(SegoutSequence)

BatchProcessFiles.m

function SegoutSequence = BatchProcessFiles(fileNames, Mask, fcn)
    % ...
    parfor (k = 1:nImages)
        N = imread(fileNames{k});
        if ndims(N)==3, N=rgb2gray(N); end
        if ~isempty(Mask), N = roifill(N, Mask); end
        SegoutSequence(:,:,k) = fcn(N);
    end
end

或将其调用为:BatchProcessFiles (fileNames, [], @DetectLines) 如果您不需要应用掩码

Lookin at your code, you only selected the ROI once for the image you tested individually.

However, when you call the BatchProcessFiles function, you dont select a region of interest, and the DetectLines function is applied to the raw images... Therefore you need to pass the mask you created with roipoly to your BatchProcessFiles function to do the same on all the images.

As a side note, you might get better results if you try Hough transformation to detect the lines.
Also your code breaks if the images are not grayscale (you can add a call to rgb2gray() to be on the safe side)

Sample solution:

MATLAB

I = imread(fileNames{1});
BW = not( roipoly(I) );

SegoutSequence = BatchProcessFiles(fileNames, BW, @DetectLines);
implay(SegoutSequence)

BatchProcessFiles.m

function SegoutSequence = BatchProcessFiles(fileNames, Mask, fcn)
    % ...
    parfor (k = 1:nImages)
        N = imread(fileNames{k});
        if ndims(N)==3, N=rgb2gray(N); end
        if ~isempty(Mask), N = roifill(N, Mask); end
        SegoutSequence(:,:,k) = fcn(N);
    end
end

or call it as: BatchProcessFiles(fileNames, [], @DetectLines) if you don't need to apply the mask

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文