2017-11-09 8 views
0

빨간색이있는 경우 픽셀 단위로 확인하면서 이미지를 가져 오려고합니다.이미지 조작, openCV 및 numpy 사용. 빨간색이 아닌 이미지를 반환하려고 시도합니다.

흰색으로 교체하십시오. 모든 픽셀을 통과하면 빨간색 대신 흰색으로 새 이미지가 반환됩니다. ,

import cv2 
import numpy as np 

def take_out_red(): 
''' 
Takes an image, and returns the same image with red replaced 
by white 
------------------------------------------------------------ 
Postconditions: 
returns 
new_img (no red) 
''' 
img = cv2.imread('red.png',1) 
#Reads the image# 

new_img = img 
#Makes a copy of the image 
for i in range(499): 
    for y in range(499): 
     if np.all(img[i,y]) == np.all([0,0,253]): 
      #[0,0,253] is a red pixel 
      #Supposed to check if the particular pixel is red 
      new_img[i,y] == [255,255,255] 
      #if it is red, it'll replace that pixel in the new_image 
      #with a white pixel 
return cv2.imshow('image',new_img) 
#returns the new_image with no red 

어떤 도움은 매우 극명하게 될 것이다 사전에 너무 감사합니다 :

다음

내 시도이다.

+0

어떻게 빨간색을 정의합니까? 당신은 단지 BGR 색 공간, 구글 "hsv"에서 빨간 채널을 대체 할 수 없으며 H 채널을 수정 한 다음 다시 변환 할 수 없습니다. – Silencer

답변

0
If img[i,y] == [0,0,255]: 

이 문제입니다. 두 가치를 가진 것을 무언가와 3과 비교하려고 시도하고 있으며이를 확인할 방법이 없습니다.

+0

특정 위치 (예 : 빨간색 픽셀 인 img [13,230])의 픽셀을 확인하면 [0,0, 253] 목록이 표시됩니다. 나는 모든 픽셀을 그 픽셀과 비교하고 싶었습니다. @Asher Mancineli –

1

서비스에서 OpenCV 또는 numpy이있는 경우 깨끗하고 비효율적이지 않은 반복적 인 반복 루프 for을 작성할 필요가 없습니다. 두 라이브러리 모두 nD 배열을 반복하고 등호 검사와 같은 기본 연산을 적용하는 매우 효율적인 루틴을 가지고 있습니다.

메서드를 사용하면 입력 이미지의 빨간색을 세그먼트로 나누고 강력한 numpy을 사용하여 에서 얻은 마스크로서 cv2.inRange() :

import cv2 
import numpy as np 

img = cv2.imread("./sample_img.png") 

red_range_lower = np.array([0, 0, 240]) 
red_range_upper = np.array([0, 0, 255]) 
replacement_color = np.array([255, 0, 0]) 
red_mask = cv2.inRange(img, red_range_lower, red_range_upper) 
img[red_mask == 255] = replacement_color 

cv2.imwrite("./output.png", img) 

입력 :

enter image description here

출력 :

enter image description here

관련 문제