2017-05-11 3 views
0

이미지가 마스크 처리 된 배열로 나타납니다. 이미지는 전경과 배경으로 구성되어 있습니다. 배경에 관심이 없으므로 가려졌습니다. 대비가 좋지 않은 이미지이고 전경에서 대비를 높이고 싶습니다. skimage.exposure.equalize_hist마스크 된 배열을 통해 skimage.equalize_hist를 수행하는 동안 오류가 발생했습니다.

equalize_hist 함수가 마스크 된 데이터를 무시하기 위해 명명 된 매개 변수 mask을 사용하는 것으로 나타났습니다. 이

import numpy as np 
import skimage.exposure as ske 

import matplotlib.pyplot as plt 

# doesn't really exist 
from proprietary import openImage, findForeground 

imagePath = "...." # path to the image file 
# image format is proprietary, so we have a custom function open it for us 
# it returns a regular numpy uint16 2d array 
# print(type(img), img.dtype, img.shape) shows 
# ` 
# <class 'numpy.ndarray'> float64 (2688, 1151) 
# ` 
img = openImage(imagePath) 
foreground = findForeground(img) # this function sets all background pixels to white 

# 65535 == white for a uint16 array 
masked_img = np.ma.masked_where(foreground==65535, foreground) 

# plotting this `masked_img` using plt.imshow works perfectly, the background is completely white 
# and the foreground is shown as it is supposed to 

# this goes wrong 
mask = np.ma.getmask(masked_img) 
equalized = ske.equalize_hist(masked_img, mask=mask) 

ske.equalize_hist 호출이 오류가 발생합니다 같은

내 코드를보고, 그 이유를 확실 해요.

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-4-e2b4f8e60ef9> in <module>() 
    37   print(type(mask)) 
    38   print(mask) 
---> 39   equalized = ske.equalize_hist(fg, mask=mask) 
    40   plt.imshow(equalized, cmap=cmap) 
    41   plt.set_title("Equalized histogram with colormap {cmap}".format(cmap=cmap)) 

C:\Users\myuser\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\exposure\exposure.py in equalize_hist(image, nbins, mask) 
    165   cdf, bin_centers = cumulative_distribution(image[mask], nbins) 
    166  else: 
--> 167   cdf, bin_centers = cumulative_distribution(image, nbins) 
    168  out = np.interp(image.flat, bin_centers, cdf) 
    169  return out.reshape(image.shape) 

C:\Users\myuser\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\exposure\exposure.py in cumulative_distribution(image, nbins) 
    125  True 
    126  """ 
--> 127  hist, bin_centers = histogram(image, nbins) 
    128  img_cdf = hist.cumsum() 
    129  img_cdf = img_cdf/float(img_cdf[-1]) 

C:\Users\myuser\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\exposure\exposure.py in histogram(image, nbins) 
    86   return hist[idx:], bin_centers[idx:] 
    87  else: 
---> 88   hist, bin_edges = np.histogram(image.flat, bins=nbins) 
    89   bin_centers = (bin_edges[:-1] + bin_edges[1:])/2. 
    90   return hist, bin_centers 

C:\Users\myuser\AppData\Local\Continuum\Anaconda3\lib\site-packages\numpy\lib\function_base.py in histogram(a, bins, range, normed, weights, density) 
    495    mn, mx = 0.0, 1.0 
    496   else: 
--> 497    mn, mx = a.min() + 0.0, a.max() + 0.0 
    498  else: 
    499   mn, mx = [mi + 0.0 for mi in range] 

TypeError: unsupported operand type(s) for +: 'MaskedIterator' and 'float' 

누구에게 이런 일이 발생했는지 알 수 있습니까? 나는 잃어 버렸다.

+1

일반적으로 'numpy'및 제 3 자 코드는 마스크 된 배열을 특별한 방법으로 처리하지 않습니다. 그들은 단지'data' 속성을 사용합니다. 'ma' 메소드를 위임하는'np.ma ...'함수 나 함수를 사용해야합니다. 하지만'equalize_hist (masked_img.data, mask = mask)'를 시도해 보셨습니까? – hpaulj

+1

또한,'foreground = 65535'는 아마도'foreground == 65535'이어야합니다. –

+0

@MadPhysicist True, 저는 원래 코드에서 double equals를 가졌지 만 여기서는 그렇지 않습니다. 고쳤다. – Azeirah

답변

2

@hpaulij가 암시 하듯이, 데이터를 전달할 때 가능한 많은 마스크 된 배열을 유지하십시오.

import numpy as np 
import skimage.exposure as ske 

import matplotlib.pyplot as plt 

# doesn't really exist 
from proprietary import openImage, findForeground 

imagePath = "...." # path to the image file 
# image format is proprietary, so we have a custom function open it for us 
# it returns a regular numpy uint16 2d array 
# print(type(img), img.dtype, img.shape) shows 
# ` 
# <class 'numpy.ndarray'> float64 (2688, 1151) 
# ` 
img = openImage(imagePath) 
foreground = findForeground(img) # this function sets all background pixels to white 

# 65535 == white for a uint16 array 
mask = (foreground != 65536) 

# plotting this `masked_img` using plt.imshow works perfectly, the background is completely white 
# and the foreground is shown as it is supposed to 

# foreground should work as well as the original img here 
equalized = ske.equalize_hist(img, mask=mask) 

은 마스크 배열 마스크 equalize_hist 기대되는 것과 반대 감각이 또한 유의 사항 : 당신이 여기 표시 사용을 감안할 때, 단지 별도의 마스크를 유지하지 않는 특별한 이유가 없다. numpy.ma.MaskedArrayinvalid elementsTrue으로 설정하고, equalize_hist는 의 유효 요소를 True으로 예상합니다.

proprietary.findForeground은 원본 이미지를 엉망으로 만드는 대신 마스크를 반환하는 것이 유용 할 수 있습니다. 이렇게하면 마스킹 된 값을 이미지의 dtype에 묶지 않고 포화 된 전경 픽셀에 문제가 발생하지 않는 이점이 있습니다.

mask = findForeground(img) # Now returns a boolean array of the correct size 
... 
equalized = ske.equalize_hist(img, mask=mask) 

당신이 볼 수 있듯이,이 프로세스에서 단계를 제거합니다 :이 작업을 수행 할 수있는 능력이있는 경우, 코드는 같을 것이다.

관련 문제