Python:获取非矩形区域的GLCM [英] Python: taking the GLCM of a non-rectangular region

查看:109
本文介绍了Python:获取非矩形区域的GLCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用skimage的SLIC实现来分割超像素中的图像.我想使用GLCM从这些超像素中提取其他功能,以解决分类问题.这些超像素不是矩形的.在MATLAB中,您可以将像素设置为NaN,算法将忽略它们(

I have been using the SLIC implementation of skimage to segment images in superpixels. I would like to use GLCMs to extract additional features from these superpixels for a classification problem. These superpixels are not rectangular. In MATLAB you can set pixels to NaN and they will be ignored by the algorithm (link). I could use this to make bounding boxes around the superpixels and then just setting the unused pixels to NaN.

greycomatrix 函数但是,在skimage中,与MATLAB实现并不完全相同.当将像素设置为NaN时,该函数将无法通过断言检查所有值是否均大于0.

The greycomatrix function in skimage does not work entirely the same as the MATLAB implementation however. When setting pixels to NaN the function fails on an assert to check if all values are larger than 0.

有没有可以与非矩形ROI一起使用的Python实现?

Is there a Python implementation available which is able to work with nonrectangular ROIs?

推荐答案

尽管mahotas也是出色的计算机视觉库,但无需停止使用skimage即可.

Although mahotas is also an excellent computer vision library, there's no need to stop using skimage to do this.

的必要之处在于将这些NaN值设置为整数,因为np.nan具有类型float并且greycomatrix函数需要一个数组整数.

What is necessary, as @Tonechas has pointed out, is to set those NaN values to an integer, since np.nan has type float and the greycomatrix function requires an array of integers.

最简单的选择是将这些NaN设置为零,但是,如果像素中已经有零值并且不想混合它们,则可以选择其他任何常数.之后,您要做的就是从GLCM中过滤出所选的值(再一次,通常为零).

The easiest option would be setting those NaN's to zero but, if you already have zero values in your pixels and don't want to mix them, you can choose any other constant. After that, all you have to do is filter that chosen value (once again, generally zero) out of the GLCM.

要了解这意味着什么,让我们看看skimage告诉我们有关

To understand what this means, let's see what skimage tells us about the output of the greycomatrix function:

4-D ndarray

4-D ndarray

[...]值P [i,j,d,theta]是灰度级j在距灰度级i的距离d和角度theta处出现的次数.如果normed为False,则输出为uint32类型,否则为float64.尺寸为:水平x水平x距离数x角度数.

[...] The value P[i,j,d,theta] is the number of times that grey-level j occurs at a distance d and at an angle theta from grey-level i. If normed is False, the output is of type uint32, otherwise it is float64. The dimensions are: levels x levels x number of distances x number of angles.

换句话说,数组的前两个维定义了一个矩阵,该矩阵告诉我们两个不同的值相隔一定距离多少次.请注意,GLCM不会保持输入数组的形状.这些行和列告诉我们这些值之间的关系.

In other words, the first two dimensions of the array define a matrix that tells us how many times two different values are a certain distant apart. Note that the GLCM does not keep the shape of the input array. Those rows and columns are telling us how the values relate.

知道了这一点,很容易过滤掉投资回报率之外的值(假设我们将那些NaN设置为零):

Knowing this, it's easy to filter out the values outside our ROI (imagine we've set those NaN's to zero):

glcm = greycomatrix(img, [1], [0])  # Calculate the GLCM "one pixel to the right"
filt_glcm = glcm[1:, 1:, :, :]           # Filter out the first row and column

现在,您可以轻松计算出已过滤GLCM的Haralick属性.例如:

Now you could easily calculate the Haralick properties of your filtered GLCM. For example:

greycoprops(filt_glcm, prop='contrast')

这篇关于Python:获取非矩形区域的GLCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆