绘制线并切断循环区域 [英] Draw line and Cut off Circuler area
问题描述
运行以下代码后,我得到了以下图片。
文件= 'grayscale.png';
I = imread(file);
bw = im2bw(I);
bw = bwareaopen(bw,870);
imwrite(bw,'noiseReduced.png')
subplot(2,3,1),imshow(bw);
[〜,threshold] = edge(bw,'sobel');
fudgeFactor = .5;
im = edge(bw,'sobel',threshold * fudgeFactor);
subplot(2,3,2),imshow(im),title('binary gradient mask');
se = strel('disk',5);
closedim = imclose(im,se);
subplot(2,3,3),imshow(closedim),title('Connected Cirlces');
cc = bwconncomp(closedim);
S = regionprops(cc,'Centroid'); //返回innercircle的中心S(2)
numPixels = cellfun(@ numel,cc.PixelIdxList);
[最大,idx] = min(numPixels);
im(cc.PixelIdxList {idx})= 0;
subplot(2,3,4),imshow(im),title('Inner Cirlces Only');
c = S(2);
我的目标是在圆形物体周围绘制一个红色圆圈(见图)并剪切圆圈原始图像I的区域(区域)并将裁剪区域保存为图像或执行其他任务。我怎么能这样做?
或者,你可以用 r <来优化/拟合圆圈/ code>包含所有积分:
bw = imread('http://i.stack.imgur的.com / il0Va.png');
[yy xx] = find(bw);
现在,让 p
成为三个向量参数化圆: p(1),p(2)
是中心的xy坐标, p(3)
它的半径。然后我们想要最小化 r
(即 p(3)
):
obj = @(p)p(3);
受圈内所有积分的限制
con = @(p)deal((xx-p(1))。^ 2+(yy-p(2))。^ 2-p(3)。^ 2, []);
使用
您可以在圆圈中生成与 bw
相同大小的二进制掩码,其中 true
false
outside
msk = bsxfun(@ plus,((1: size(bw,2)) - p(1))。^ 2,((1:size(bw,1))。' - p(2))。^ 2)< = p(3)。^ 2 ;
掩码如下:
< a href =https://i.stack.imgur.com/1mKB4.png =nofollow noreferrer>
I have got the below Image after running the below code.
file='grayscale.png';
I=imread(file);
bw = im2bw(I);
bw = bwareaopen(bw,870);
imwrite(bw,'noiseReduced.png')
subplot(2,3,1),imshow(bw);
[~, threshold] = edge(bw, 'sobel');
fudgeFactor = .5;
im = edge(bw,'sobel', threshold * fudgeFactor);
subplot(2,3,2), imshow(im), title('binary gradient mask');
se = strel('disk',5);
closedim = imclose(im,se);
subplot(2,3,3), imshow(closedim), title('Connected Cirlces');
cc = bwconncomp(closedim);
S = regionprops(cc,'Centroid'); //returns the centers S(2) for innercircle
numPixels = cellfun(@numel,cc.PixelIdxList);
[biggest,idx] = min(numPixels);
im(cc.PixelIdxList{idx}) = 0;
subplot(2,3,4), imshow(im), title('Inner Cirlces Only');
c = S(2);
My target is now to draw a red cirle around the circular object(see image) and cut the circle region(area) from the original image 'I' and save the cropped area as image or perform other tasks. How can I do it?
Alternatively, you can optimize/fit the circle with least r
that contains all the points:
bw = imread('http://i.stack.imgur.com/il0Va.png');
[yy xx]=find(bw);
Now, let p
be a three vector parameterizing a circle: p(1), p(2)
are the x-y coordinates of the center and p(3)
its radii. Then we want to minimize r
(i.e., p(3)
):
obj = @(p) p(3);
Subject to all points inside the circle
con = @(p) deal((xx-p(1)).^2+(yy-p(2)).^2-p(3).^2, []);
Optimizing with fmincon
:
[p, fval] = fmincon(obj, [mean(xx), mean(yy), size(bw,1)/4], [],[],[],[],[],[],con);
Yields
p =
471.6397 484.4164 373.2125
Drawing the result
imshow(bw,'border','tight');
colormap gray;hold on;
t=linspace(-pi,pi,1000);
plot(p(3)*cos(t)+p(1),p(3)*sin(t)+p(2),'r', 'LineWidth',1);
You can generate a binary mask of the same size as bw
with true
in the circle and false
outside
msk = bsxfun(@plus, ((1:size(bw,2))-p(1)).^2, ((1:size(bw,1)).'-p(2)).^2 ) <= p(3).^2;
The mask looks like:
这篇关于绘制线并切断循环区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!