我遇到了问题,我应该在下图中分隔圆圈和线条:
问题是我刚刚编写了一些代码,它的工作方式只是将它们分离为不同的对象,最终分离的对象与输入的对象不同!我发布这个问题,因为它可能对某人有帮助,如果有人可以制作更好的代码来更清楚地分离对象,也可能像输入的那样准确。
我提前感谢任何评论......
代码如下:
clear; close all; clc; warning off
im = imread('input\circle_and_lines.jpg');
level = graythresh(im);
imBin = im2bw(im,level);
figure; imshow(imBin);
%% Separating circles
se1 = strel('disk',6);
imCircles = imopen(imBin,se1);
figure; imshow(imCircles);
[~,numCC] = bwlabel(imCircles,8);
imwrite(imCircles,'output\ex04_circle.png');
%% Separating lines
linDeg0 = imopen(imBin,strel('line',60,0)); figure; imshow(linDeg0);
linDeg10 = imopen(imBin,strel('line',60,10)); figure; imshow(linDeg10);
linDeg20 = imopen(imBin,strel('line',60,20)); figure; imshow(linDeg20);
linDeg75 = imopen(imBin,strel('line',60,75)); linDeg75 = imclose(linDeg75,strel('line',60,75)); figure; imshow(linDeg75);
linDeg95 = imopen(imBin,strel('line',40,95)); linDeg95 = imdilate(linDeg95,strel('disk',1)); figure; imshow(linDeg95);
linDeg135 = imopen(imBin,strel('line',60,135)); linDeg135 = imdilate(linDeg135,strel('disk',1)); figure; imshow(linDeg135);
linDeg170 = imopen(imBin,strel('line',60,170)); figure; imshow(linDeg170);
imLines = linDeg0 | linDeg10 | linDeg20 | linDeg75 | linDeg95 | linDeg135 | linDeg170;
figure; imshow(imLines);
imwrite(imLines,'output\ex04_line.png');
答案 0 :(得分:2)
% load image
I = imread('circles_and_lines.jpg');
% Separate the Circles
S1 = strel('disk',6,0);
circles = imopen(I, S1);
figure, imshow(circles);
title('Circles only');
lines = imopen(I,strel('line',60,1));
% Separate the Lines
for c = 1:1:200
lines = lines + imopen(I,strel('line',60,c));
end
figure, imshow(lines);
title('Lines only');
基本上是解决方案的一个更好的版本,因为for循环扫描并更详细地分隔行