[英]Python; Determining and adding noise to image segments
為了掌握編碼技巧,我決定使用Python / Numpy / SKimage確定X射線圖片特定區域的標准差。 首先,我決定使用閾值獲取圖像的一部分,這並不難。
但是,此時,閾值之上/之下的所有內容都為零,因此我希望將其包括在內。 因此,我需要排除低於/高於閾值的數據。
我想它將能夠創建地圖或排除某些值的或可能更奇特的解決方案。 但是,在這一點上,我認為我可能正在朝錯誤的方向前進。
我的基礎-
import numpy as np
import matplotlib.pyplot as plt
import imageio
image = imageio.imread('thx.jpg', as_gray=True)
image = image.astype('int32')
test1 = np.array(image, copy=True)
test1[image >= 100] = 0
我正在尋找一種排除閾值以上/以下數據的方法。 有人可以為我提供一些正確方向的幫助嗎?
編輯:偶爾有輕松的一天工作真是太好了; 我的問題的一個子解決方案是將所有大於或小於該值的值都添加到列表中,並確定與列表的標准偏差。 然而,這給我留下了實施噪聲對圖像段的問題。
im_row = image.shape[0]
im_col = image.shape[1]
grthen = []
smlthen = []
for i in range(0,im_col-1):
for j in range(0,im_row-1):
if (j > 100):
grthen.append(j)
else:
smlthen.append(j)
print(np.std(smlthen))
print(np.std(grthen))
我認為問題在於將所有這些像素設置為零,然后嘗試從中獲取統計信息。 相反,要意識到test1[image < 100]
僅指閾值以下的那些像素...因此,我認為您可以從中獲取統計信息,例如使用np.std(test1[image < 100])
。
您可能想看看scikit-image
,它包含許多用於閾值處理,處理二進制圖像,將其用作遮罩的工具(本質上是您正在做的事情)等。
...使用Python / Numpy / SKimage確定X射線照片特定區域的標准偏差。
讓我們首先生成一個模擬圖像:
In [18]: import numpy as np
In [19]: rows, cols = 4, 4
In [20]: image = np.linspace(0, 1, rows*cols).reshape(rows, cols)
In [21]: np.set_printoptions(precision=2)
In [22]: image
Out[22]:
array([[0. , 0.07, 0.13, 0.2 ],
[0.27, 0.33, 0.4 , 0.47],
[0.53, 0.6 , 0.67, 0.73],
[0.8 , 0.87, 0.93, 1. ]])
讓我們通過雙重閾值定義關注區域:
In [25]: low, high = .3, .6
In [26]: mask = np.logical_and(image > low, image < high)
In [27]: mask
Out[27]:
array([[False, False, False, False],
[False, True, True, True],
[ True, False, False, False],
[False, False, False, False]])
布爾索引是一種在感興趣區域上計算標准偏差的可能方法:
In [29]: image[mask]
Out[29]: array([0.33, 0.4 , 0.47, 0.53])
In [30]: np.std(image[mask])
Out[30]: 0.07453559924999299
將不需要的像素設置為np.nan
並使用NumPy的nanstd
計算標准偏差將是另一種方法:
In [32]: test1 = np.where(mask, image, np.nan)
In [33]: test1
Out[33]:
array([[ nan, nan, nan, nan],
[ nan, 0.33, 0.4 , 0.47],
[0.53, nan, nan, nan],
[ nan, nan, nan, nan]])
In [34]: np.nanstd(test1)
Out[34]: 0.07453559924999299
...對圖像片段實施噪聲的問題。
你可以使用scikit圖像的random_noise
產生嘈雜的圖像,然后過濾掉那些像素的感興趣區域之外,通過與NumPy的where
:
In [36]: from skimage.util import random_noise
In [37]: noisy = random_noise(image)
In [38]: noisy
Out[38]:
array([[0.14, 0.07, 0.17, 0.29],
[0.34, 0.39, 0.38, 0.53],
[0.66, 0.73, 0.66, 0.67],
[0.73, 0.98, 1. , 0.88]])
In [39]: np.where(mask, noisy, image)
Out[39]:
array([[0. , 0.07, 0.13, 0.2 ],
[0.27, 0.39, 0.38, 0.53],
[0.66, 0.6 , 0.67, 0.73],
[0.8 , 0.87, 0.93, 1. ]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.