2017-05-12 2 views
0

나는 cv2.THRESH_BINARY과 기능을 내 이미지를 구축파이썬으로 이진 이미지를 만드는 방법은 무엇입니까?

RET, im_thresh = cv2.threshold (gray_image, 40, 255, cv2.THRESH_BINARY)

하지만 화이트해야합니다 1 내 프로그램 사용.

은 내가 만든 돕기 위해 :

height = int(np.size(im_thresh, 0)) 
width = int(np.size(im_thresh, 1)) 

for x in range(height): 
    for y in range(width): 
     if im_thresh[x,y]==255: 
      im_thresh[x,y] = 1 

내 질문

: 파이썬에서 빠르게이 작업을 수행 할 수있는 방법이 있나요?

답변

1

배열의 값을 가져오고 설정하려면 부울 인덱스를 사용해보십시오. 그러면 중첩 된 for 루프가 발생하지 않습니다.

import numpy as np 
from numpy import random 

# Generating an image of values between 1 and 255. 
im_thresh = random.randint(1,256, (64,64)) 

# Set anything less than 255 to 0. Unnecessary if cv2 does this during threshold. 
# Must go before the operation below in order not to set all values to 0. 
im_thresh[im_thresh<255] = 0 

# Set all values at indices where the array equals 255 to 1. 
im_thresh[im_thresh==255] = 1 
관련 문제