2016-06-13 86 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) 

这样你就会收到所有“微红”的角落。你只需要玩这个门槛。

请注意,此解决方案相当简单(使用阈值)