在matlab中将图像分成两个非矩形非正方形子图像

Dividing an image into two non-rectangular non-square sub-image in matlab

假设我有这样一张图片: http://www.mathworks.com/help/releases/R2015a/examples/images/BasicImageImportProcessingAndExportExample_03.png

(是matlab中的原图pout.tif)

像这样帮助图像(二进制掩码): http://upload7.ir/uploads//df17be22203e3099ba0d86e7cb203477bc909244.jpg

(与原图大小相同)

我写了这个简单的代码来将原始图像分成两个图像:

I=imread('pout.tif');
figure,imshow(I),title('original image')

Ih=imread('helpPic.jpg');
Ih=rgb2gray(Ih);
Ih=im2bw(Ih);
figure,
imshow(Ih),title('help image')

im1=I.*uint8(Ih);
figure,
imshow(im1),title('im1')

im2=I.*uint8(1-Ih);
figure,
imshow(im2),title('im2')

结果:

http://upload7.ir/uploads//12f09dde101cd4c98b53fcc0d400be87029ee07a.png

并且知道我想实现 Otsu 的方法来二值化这个子图像(im1 和 im2),我如何实现 otsu 的方法 only 有信息的部分(非零部分子图像)?

Image Processing Toolbox中的graythresh函数实现了Otsu的方法。此外,由于 Otsu 的方法只关心像素的 而不是它们的位置,您可以在应用 graythresh 之前在一维数组中重新排列感兴趣的像素。

在实践中,这给出了 -compact- 以下代码:

level1 = graythresh(I(Ih(:)));
level2 = graythresh(I(~Ih(:)));

最佳,

由于 Matlab 的 graythresh(它实现了 Otsu 的方法来计算阈值)使用 I(:) 并忽略了 I 的维度(你可以看到,如果你 edit gryythresh),你可以使用

t = graythresh(im(im_mask))

计算掩码为 1 的区域的阈值。