MATLAB 中的轮廓线边缘检测
contour line edge detection in MATLAB
我有一个检测 图像中的轮廓线的项目,但是当我 运行 我的代码使用精明的边缘检测算法时,图像中的一条线变成了两条线,因为前后线条灰度值变化的两倍.
i= imread('path');
imgG= rgb2gray(i);
PSF = fspecial('gaussian',7,7);
Blurred = imfilter(imgG,PSF,'symmetric','conv');
figure ,imshow(Blurred)
edgeimg = edge(Blurred , 'canny');
figure ,imshow(edgeimg)
我不知道如何解决这个问题,请帮助我。
最佳答案取决于您在检测到边缘后要对它们做什么,但我们假设您只想生成一个图像,其中线条是纯黑色的,其他一切都是纯白色的...
最简单的方法是阈值 图像,使浅灰色像素变为白色,深灰色像素变为黑色。您还可以 侵蚀 图像以尝试减少线条的粗细 - 虽然您会发现这会消除示例图像中的精细轮廓。
这是执行此操作的代码(假设您的工作文件夹中有图像 G4.jpg)。
% load image and convert to double (0 to 1) as well as flip black and white
imG4 = imread('G4.jpg');
imG4_gs = 1 - mean(double(imG4)/255,3);
figure
image(64*(1 - imG4_gs))
colormap('gray');
axis equal
% image grayscale threshold
img_thr = 0.25;
% apply threshold to image
imG4_thr = imG4_gs >= img_thr;
figure
image(64*(1 - imG4_thr))
colormap('gray');
axis equal
% erode image (try "help imerode" in the MATLAB console)
imG4_ero = imerode(imG4_thr,strel('disk',1));
figure
image(64*(1 - imG4_ero))
colormap('gray');
axis equal;
我有一个检测
i= imread('path');
imgG= rgb2gray(i);
PSF = fspecial('gaussian',7,7);
Blurred = imfilter(imgG,PSF,'symmetric','conv');
figure ,imshow(Blurred)
edgeimg = edge(Blurred , 'canny');
figure ,imshow(edgeimg)
我不知道如何解决这个问题,请帮助我。
最佳答案取决于您在检测到边缘后要对它们做什么,但我们假设您只想生成一个图像,其中线条是纯黑色的,其他一切都是纯白色的...
最简单的方法是阈值 图像,使浅灰色像素变为白色,深灰色像素变为黑色。您还可以 侵蚀 图像以尝试减少线条的粗细 - 虽然您会发现这会消除示例图像中的精细轮廓。
这是执行此操作的代码(假设您的工作文件夹中有图像 G4.jpg)。
% load image and convert to double (0 to 1) as well as flip black and white
imG4 = imread('G4.jpg');
imG4_gs = 1 - mean(double(imG4)/255,3);
figure
image(64*(1 - imG4_gs))
colormap('gray');
axis equal
% image grayscale threshold
img_thr = 0.25;
% apply threshold to image
imG4_thr = imG4_gs >= img_thr;
figure
image(64*(1 - imG4_thr))
colormap('gray');
axis equal
% erode image (try "help imerode" in the MATLAB console)
imG4_ero = imerode(imG4_thr,strel('disk',1));
figure
image(64*(1 - imG4_ero))
colormap('gray');
axis equal;