use*_*234 5 matlab image-processing edge-detection
我想了解这段代码:
d=edge(d,'canny',.6);
figure,
imshow(d,[])
ds = bwareaopen(d,40);
figure,
imshow(ds,[])
iout = d1;
BW=ds;
iout(:,:,1) = iout;
iout(:,:,2) = iout(:,:,1);
iout(:,:,3) = iout(:,:,1);
iout(:,:,2) = min(iout(:,:,2) + BW, 1.0);
iout(:,:,3) = min(iout(:,:,3) + BW, 1.0);
Run Code Online (Sandbox Code Playgroud)
据我所知,应用d了图像和精确探测器,忽略了40个像素.图像为灰度,轮廓添加到图像中.
你能解释下一行吗?这里使用了什么原理/算法?我遇到了麻烦,尤其是代码的轮廓检测部分.
gno*_*ice 10
假设变量d1存储了可能是操作的原始灰度强度图像的双精度表示(0到1之间的值),那么最后5行将该灰度图像转换为看起来像的3D灰度图像 iout与原始灰度图像相同,只是轮廓将以青色覆盖在图像上.
这是一个例子,使用'cameraman.tif'MATLAB 图像处理工具箱附带的图像:
d1 = double(imread('cameraman.tif'))./255; % Load the image, scale from 0 to 1
subplot(2, 2, 1); imshow(d1); title('d1'); % Plot the original image
d = edge(d1, 'canny', .6); % Perform Canny edge detection
subplot(2, 2, 2); imshow(d); title('d'); % Plot the edges
ds = bwareaopen(d, 40); % Remove small edge objects
subplot(2, 2, 3); imshow(ds); title('ds'); % Plot the remaining edges
iout = d1;
BW = ds;
iout(:, :, 1) = iout; % Initialize red color plane
iout(:, :, 2) = iout(:, :, 1); % Initialize green color plane
iout(:, :, 3) = iout(:, :, 1); % Initialize blue color plane
iout(:, :, 2) = min(iout(:, :, 2) + BW, 1.0); % Add edges to green color plane
iout(:, :, 3) = min(iout(:, :, 3) + BW, 1.0); % Add edges to blue color plane
subplot(2, 2, 4); imshow(iout); title('iout'); % Plot the resulting image
Run Code Online (Sandbox Code Playgroud)
以下是上面代码创建的图:

图像的创建iout与边缘检测算法无关.这只是显示前面步骤中找到的边缘的简单方法.2-D灰度强度图像无法显示颜色,因此如果要为图像添加彩色轮廓线,则必须先将其转换为可让您显示颜色的格式:要么是索引图像(这是一个小的根据我的经验,或者三维RGB图像(第三维代表每个像素的红色,绿色和蓝色分量)更难处理.
在第三维中复制灰度图像3次为我们提供了3-D RGB图像,该图像最初仍然包含灰色(每个像素等量的红色,绿色和蓝色).但是,通过修改每个颜色平面的某些像素,我们可以为图像添加颜色.通过将逻辑边掩模BW(边缘为零和其他位置为零)添加到绿色和蓝色平面,找到轮廓的那些像素将显示为青色.对函数的调用min确保添加图像的结果不会导致像素颜色值超过该值1.0,该值是元素对双精度3-D RGB图像应具有的最大值.
还应注意,用于创建3-D RGB图像的代码可以简化为以下内容:
iout = d1;
iout(:, :, 2) = min(d1+ds, 1.0);
iout(:, :, 3) = min(d1+ds, 1.0);
Run Code Online (Sandbox Code Playgroud)