简体   繁体   English

Matlab-使用逻辑索引删除图像边框

[英]Matlab - remove image border using logical indexing

I have a retinal fundus image which has a white border along the corners. 我有一个视网膜眼底图像,角上有一个白色边框。 I am trying to remove the borders on all four sides of the image. 我正在尝试删除图像所有四个侧面上的边框。 This is a pre-processing step and my image looks like this: 这是一个预处理步骤,我的图像如下所示:

fundus http://snag.gy/XLGkC.jpg 眼底http://snag.gy/XLGkC.jpg

It is an RGB image, and I took the green channel, and created a mask using logical indexing. 这是RGB图像,我选择了绿色通道,并使用逻辑索引创建了蒙版。 I searched for pixels which were all black in the image, and eroded the mask to remove the white edge pixels. 我搜索了图像中全都是黑色的像素,然后腐蚀了遮罩以去除白色边缘像素。 However, I am not sure how to retrieve the final image, without the white pixel border using the mask that I have. 但是,我不确定在没有白色边框的情况下如何获取最终图像,而无需使用已有的遮罩。 This is my code, and any help would be appreciated: 这是我的代码,任何帮助将不胜感激:

maskIdx = rgb(:,:,2) == 0;   # rgb is the original image
se = strel('disk',3);        # erode 3-pixel using a disk structuring element 
im2 = imerode(maskIdx, se);
newrgb = rgb(im2);           # gives a vector - not the same size as original im

Solved it myself. 我自己解决了。 This is what I did with some help. 这是我在一些帮助下所做的。

I first computed the mask for all three color channels combined. 我首先计算了所有三个颜色通道组合的遮罩。 This is because the mask for each channel is not the same when applied to all the three channels individually, and residual pixels will be left in the final image if I used only the mask from one of the channels in the original image: 这是因为当分别应用于所有三个通道时,每个通道的蒙版都不相同,如果我仅使用原始图像中一个通道的蒙版,则最终图像中会残留剩余像素:

mask = (rgb(:,:,1) == 0) & (rgb(:,:,2) == 0) & (rgb(:,:,3) == 0);

Next, I used a disk structuring element with a radius of 9 pixels to dilate my mask: 接下来,我使用半径为9像素的磁盘结构元素来扩展蒙版:

se = strel('disk', 9); 
maskIdx = imdilate(mask,se);

EDIT: A structuring element which is arbitrary can also be used. 编辑:也可以使用任意的结构元素。 I used: se = strel(ones(9,9)) 我用过: se = strel(ones(9,9))

Then, with the new mask, I multiplied the original image with the new dilated mask: 然后,使用新蒙版,将原始图像乘以新的膨胀蒙版:

newImg(:,:,1) = rgb(:,:,1) .* uint8(maskIdx);    # image was of double data-type
newImg(:,:,2) = rgb(:,:,2) .* uint8(maskIdx);
newImg(:,:,3) = rgb(:,:,3) .* uint8(maskIdx);

Finally, I subtracted the computed color-mask from the original image to get my desired border-removed image: 最后,我从原始图像中减去了计算出的颜色蒙版,以获得所需的去除边框的图像:

finalImg = rgb - newImg;

Result: 结果:

image http://snag.gy/g2X1v.jpg 图片http://snag.gy/g2X1v.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM