NumPy - 使用强度值矩阵进行图像(矩阵)阈值处理。 [英] NumPy - Image (matrix) thresholding using an intensity value matrix.
问题描述
我需要在灰度图像中分割出异常。在算法的某个地方,我计算一个矩阵,其中包含我需要设置为零的已知像素强度。我该怎么做?
I need to segment out anomalies in a greyscale image. In a certain place in my algorithm, I compute a matrix that contains the known pixel intensities that I need to set to zero. How would I do this?
例如:
计算出的像素强度:
(数组([94,95,96, 97,98,99,100,101,102,103,104,105,106,
107,108,109,110,111,112,113,114,115,116,117,118,119,
120,121,122,123,124,125,126,127,128,129,130,131,132,
133,134,135,136,137,138,139,140, 141,142,143,144,145,
146,147,148,149,150,151]),)
图片大小(480,640):
印刷它给出:
[[86 90 97 ...,142 152 157]
[85 89 97 ...,145 154 158]
[83 87 95 ...,154 158 159]
...,
[130 134 139 ...,156 154 154]
[130 134 140 ...,154 153 152]
[130 134 141 ...,154 153 152]]
For example:
The computed pixel intensities:
(array([ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145,
146, 147, 148, 149, 150, 151]),)
The picture is of size (480,640) :
Printed it gives:
[[ 86 90 97 ..., 142 152 157]
[ 85 89 97 ..., 145 154 158]
[ 83 87 95 ..., 154 158 159]
...,
[130 134 139 ..., 156 154 154]
[130 134 140 ..., 154 153 152]
[130 134 141 ..., 154 153 152]]
我意识到,对于每个像素,我都可以通过强度矩阵。然而,这将太昂贵。 NumPy专家我需要你的帮助!
I realize that for each pixel I could go through the intensity matrix. This would, however, be too expensive. NumPy experts I need your help!
推荐答案
要将图像数组中所有像素设置为零,其值为91到151,包容性,使用:
To set to zero all pixels in an image array which have values from 91 to 151, inclusive, use:
import numpy as np
newimage = np.where(np.logical_and(91<=oldimage, oldimage<=151), 0, oldimage)
将图像数组中的所有像素设置为零其值属于某个数组 vct
,请使用:
To set to zero all pixels in an image array whose values belong to some array vct
, use:
newimage = np.where(np.in1d(oldimage, vct).reshape(oldimage.shape), 0, oldimage)
< h3>示例
假设我们有一个 oldimage
,就像这样:
In [12]: oldimage
Out[12]:
array([[0, 1, 2],
[3, 4, 5]])
我们有一个名为的数字列表vct
:
In [13]: vct
Out[13]: array([3, 5])
让我们将所有像素设为零n oldimage
也在 vct
:
Let's set to zero all pixels in oldimage
that are also in vct
:
In [14]: newimage = np.where(np.in1d(oldimage, vct).reshape(oldimage.shape), 0, oldimage)
In [15]: newimage
Out[15]:
array([[0, 1, 2],
[0, 4, 0]])
这篇关于NumPy - 使用强度值矩阵进行图像(矩阵)阈值处理。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!