2017-12-28 9 views
0

데이터가 약간 있으며 크기는 249X250입니다. 나는 데이터 플롯에 다음 코드를 사용했습니다 :연결된 픽셀 수가 지정된 숫자 임계 값보다 작음

import numpy as np 

import pandas as pd 

import matplotlib.pyplot as pl 

data = pd.read_excel("sample_data.xlsx") 

x = np.arange(data.shape[0]) 

y = np.arange(data.shape[1]) 

mask_data = np.ma.masked_outside(data,0,233) 


pl.contourf(y,x,mask_data) 

pl.colorbar() 

및 줄거리는 다음과 같이 온 :

Plot

가 지금은 플롯의 오른쪽에있는 작은 패치를 제거 할 및 가장 큰 패치 만 유지하려고합니다. 이 목적을 위해 내 논리는 연결된 픽셀 수가 특정 임계 값보다 작은 연결된 픽셀을 제거하는 것입니다 (이 목적으로 200이 될 수 있음). 어떻게해야합니까?

+1

https://www.learnopencv.com/blob-detection- using-opencv-python-c/ – f5r5e5d

+0

하지만 엑셀 파일에 데이터가 있습니다. 그것은 이미지가 아닙니다. – sam

+0

당신은'data','masked_data'를 가지고 있습니다.'masked_data'에서 작업하고 싶다면'masked_data.fill (255)'처럼 그것을 정규 np.array에 던져 넣을 것입니다. 어쩌면 uint8에도 캐스트해야합니다 : 'np.array (data, dtype = np.uint8)'- 이미지 처리 함수로 그레이 스케일 이미지로 인식되어야합니다. – f5r5e5d

답변

2

본질적으로 당신이하려는 것은 이미지의 모든 개체를 식별하는 것입니다. 이것은 ndimage.measurements.label에서 scipy.으로 수행 할 수 있습니다. 이미지의 연속적인 픽셀 그룹을 검색하여 레이블을 할당합니다. 그런 다음 레이블이 지정된 섹터를 반복하고 개체의 크기 (픽셀 단위)를 계산하여 그 기준으로 필터링 할 수 있습니다.

Excel에서 데이터를 가져 오는 경우에도 실제로 플로팅 할 249x250 픽셀 "이미지"만 있으면됩니다. Excel의 각 셀은 실제로 값을 포함하는 "픽셀"입니다. 당신은 말 그대로하기 matplotlib의 이미지 보여주는 기능을 사용할 수있는 홈이 점을 드라이브에 (예를 들어 plt.imshow)

import matplotlib.pyplot as plt 
import numpy as np 
from scipy import ndimage 

xn = 250 
yn = 249 

# fake data to illustrate that images are just matrices of values 
X = np.stack([np.arange(xn)] * yn) 
Y = np.stack([np.arange(yn)] * xn).transpose() 
Z = np.sin(3*np.pi * X/xn) * np.cos(4*np.pi * Y/yn) * np.sin(np.pi * X/xn) 
Z[Z <.5] = 0 

fig,axes = plt.subplots(1,2) 
axes[0].contourf(Z) 
axes[0].set_title("Before Removing Features") 

# now identify the objects and remove those above a threshold 
Zlabeled,Nlabels = ndimage.measurements.label(Z) 
label_size = [(Zlabeled == label).sum() for label in range(Nlabels + 1)] 
for label,size in enumerate(label_size): print("label %s is %s pixels in size" % (label,size)) 

# now remove the labels 
for label,size in enumerate(label_size): 
    if size < 1800: 
     Z[Zlabeled == label] = 0 

axes[1].contourf(Z) 
axes[1].set_title("After Removing Features") 

일러스트 결과 : enter image description here

+0

작동 중 ... 고맙습니다. – sam

관련 문제