2016-06-13 3 views
1

다음 이미지에서 감지 된 붉은 색 선에 좌표를 가져 오려고합니다. 하지만 아래의 코드를 실행하면, 나는 모든 좌표 (붉은 색 선 및 기타 특정 된 오브젝트)를 얻을 :붉은 색 선의 좌표를 얻으려면

import cv2 
import numpy as np 
import matplotlib.pyplot as plt 

filename = 'detectedRoof.jpg' 
img = cv2.imread(filename) 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

gray = np.float32(gray) 
dst = cv2.cornerHarris(gray, 2, 3, 0.04) 

# Threshold for an optimal value, it may vary depending on the image. 
img[dst > 0.01*dst.max()] = [255, 0, 0] 
cv2.imwrite('outputsimple.jpg', img) 

coord = np.where(np.all(img == (255, 0, 0), axis=-1)) 

lol = zip(coord[0], coord[1]) 
print(lol) 
print ("") 

x = np.array(lol, dtype="int") 
print (x) 

filename1 = open("coordinates_simple.txt", "w") 
filename1.write(str(lol)) 
filename1.close() 

plt.scatter(coord[0], coord[1]) 
plt.show() 

입력 이미지 누구도 날은 좌표를 얻을 수 있도록 할 수

Original image 붉은 색 라인의.

답변

1

첫째, "빨간색"은 무엇을 의미합니까? 자연스러운 빨간색 물체 (예 : 모퉁이)는 항상 녹색 및 파란색 채널에도 구성 요소가 있기 때문에 묻습니다.

둘째, 모든 색상의 모서리를 의미하는 회색조 이미지의 모서리를 감지합니다. 그런 다음이 객체를 파란색 픽셀로 설정합니다.

(셋째, 당신은 img[dst > 0.01*dst.max()] = [255, 0, 0]를 설정하여 당신이 파란색 이러한 픽셀을 설정 알고 있습니다?) (대로 나머지를 떠나) 나는이 유사하여 각각의 코드를 수정하는 것이 좋습니다 모두 문제를 관찰

:

img = cv2.imread(filename, 1) 
b,g,r = cv2.split(img) 
gray = np.float32(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)) 

# detect corners in gray scale image 
dst = cv2.cornerHarris(gray, 2, 3, 0.04) 

# threshold (red > X, blue and green < Y) such that only reddish corners arec extracted 
dst = np.where((dst > 0.01*dst.max()) & (r > 130) & (g < 100) & (b < 100), dst, 0) 

이렇게하면 모든 "붉은 색"모서리가 나타납니다. 한계점을 가지고 놀아야 할 것입니다.

이 솔루션은 다소 간단합니다 (임계 값 사용)

관련 문제