我有一个图像如下
圈包装
我创建了一个matlab代码,可以检测圆周上的像素并创建一个整型掩码,以便我可以将它导入到C ++代码中。 这是主要算法的一小部分。 (我使用imread并将图像存储在NXN阵列中
for i=1:nx for j=1:ny if (wholeGeom(j,i) == 255) B(j,i) = 1; elseif ((wholeGeom(j,i) == 0)&& ... ((i==1||i==nx) && j>1 && j<nx)&& ... (wholeGeom(j+1,i)==255 || ... wholeGeom(j-1,i)==255) ) B(j,i) = 2; elseif ((wholeGeom(j,i) == 0)&& ... ((j==1||j==nx) && i>1 && i<nx)&& ... (wholeGeom(j,i+1)==255 || ... wholeGeom(j,i-1)==255) ) B(j,i) = 2; elseif ((wholeGeom(j,i) == 0)&& ... (i>1 && j>1 && i<nx && j<nx)&& ... (wholeGeom(j+1,i)==255 || ... wholeGeom(j-1,i)==255 ||... wholeGeom(j,i+1)==255 ||... wholeGeom(j,i-1)==255) ) else B(j,i) = 0;这对我很好。 我的输出如下
BounceBack节点
我只是想知道是否有更有效的方法来做到这一点,尤其是在matlab上....任何建议,将不胜感激
I have an image as following
Circle packing
I have created a matlab code that would detect the pixels on the circumference and create a integer mask so I can import it in a C++ code. Here is a snippet of main algo. (I use imread and store the image in a N X N array
for i=1:nx for j=1:ny if (wholeGeom(j,i) == 255) B(j,i) = 1; elseif ((wholeGeom(j,i) == 0)&& ... ((i==1||i==nx) && j>1 && j<nx)&& ... (wholeGeom(j+1,i)==255 || ... wholeGeom(j-1,i)==255) ) B(j,i) = 2; elseif ((wholeGeom(j,i) == 0)&& ... ((j==1||j==nx) && i>1 && i<nx)&& ... (wholeGeom(j,i+1)==255 || ... wholeGeom(j,i-1)==255) ) B(j,i) = 2; elseif ((wholeGeom(j,i) == 0)&& ... (i>1 && j>1 && i<nx && j<nx)&& ... (wholeGeom(j+1,i)==255 || ... wholeGeom(j-1,i)==255 ||... wholeGeom(j,i+1)==255 ||... wholeGeom(j,i-1)==255) ) else B(j,i) = 0;This works well for me. My output is as follows
BounceBack Nodes
I was just wondering if there is a more efficient way to do this, especially on matlab.... Any recommendations would be appreciated
最满意答案
事实上,形态操作是你的朋友。 形态学操作是处理二进制图像的非常快速的工具。 您可以使用imdilate来“扩大”图像,即“加厚”图像中的所有对象。
% Load image wholeGeom = rgb2gray(imread('https://i.stack.imgur.com/Jt4TC.png')); % Threshold to make image binary wholeGeom = wholeGeom > 128; % Dilate image, i.e. add 1 pixel to each side of an object borders = imdilate(wholeGeom, [0,1,0;1,1,1;0,1,0]); % Set borders to 2 and everything inside object to 1 result = 2*borders - wholeGeom;结果(放大以使边框更清晰可见):
Indeed, morphological operations are your friend. Morphological operations are a very fast tool to work on binary images. You can use imdilate to "dilate" an image, i.e. "thicken" all objects in the image.
% Load image wholeGeom = rgb2gray(imread('https://i.stack.imgur.com/Jt4TC.png')); % Threshold to make image binary wholeGeom = wholeGeom > 128; % Dilate image, i.e. add 1 pixel to each side of an object borders = imdilate(wholeGeom, [0,1,0;1,1,1;0,1,0]); % Set borders to 2 and everything inside object to 1 result = 2*borders - wholeGeom;Result (zoomed in to make border more visible):
更多推荐
发布评论