2
A
回答
1
你覺得這個解決方案的?
我在代碼中包含了解釋。總體思路是做一個閾值提取黑色區域,然後查找輪廓,並選出最長的一個。
Dilation確實已經完成了指針的所有工作,但是我留下了一些替代代碼,以便在需要時查找最長的輪廓。
#!/usr/bin/env python
import sys
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('image.jpg',0)
print img.shape
h, w = img.shape[:2]
# Drop top and bottom area of image with black parts.
img= img[100:h-100, :]
h, w = img.shape[:2]
# Threshold image
ret,th1 = cv2.threshold(img,50,255,cv2.THRESH_BINARY)
# get rid of thinner lines
kernel = np.ones((5,5),np.uint8)
th1 = cv2.dilate(th1,kernel,iterations = 3)
# Determine contour of all blobs found
_, contours0, hierarchy = cv2.findContours(th1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours = [cv2.approxPolyDP(cnt, 3, True) for cnt in contours0]
# Draw all contours
vis = np.zeros((h, w, 3), np.uint8)
cv2.drawContours(vis, contours, -1, (128,255,255), 3, cv2.LINE_AA)
# Draw the contour with maximum perimeter (omitting the first contour which is outer boundary of image
# Not necessary in this case
vis2 = np.zeros((h, w, 3), np.uint8)
perimeter=[]
for cnt in contours[1:]:
perimeter.append(cv2.arcLength(cnt,True))
print perimeter
print max(perimeter)
maxindex= perimeter.index(max(perimeter))
print maxindex
cv2.drawContours(vis2, contours, maxindex +1, (255,0,0), -1)
# Show all images
titles = ['Original Image','Threshold','Contours', 'Result']
images=[img, th1, vis, vis2]
for i in xrange(4):
plt.subplot(2,2,i+1)
plt.imshow(images[i],'gray')
plt.title(titles[i]), plt.xticks([]), plt.yticks([])
plt.show()
[編輯]
您可以廣告多一些代碼來確定輪廓的主軸線爲如下一行:
# Determine and draw main axis
length = 300
(x,y),(MA,ma),angle = cv2.fitEllipse(cnt)
print np.pi , angle
print angle * np.pi/180.0
print np.cos(angle * np.pi/180.0)
x2 = int(round(x + length * np.cos((angle-90) * np.pi/180.0)))
y2 = int(round(y + length * np.sin((angle-90) * np.pi/180.0)))
cv2.line(vis2, (int(x), int(y)), (x2,y2), (0,255,0),5)
print x,y,x2,y2
相關問題
- 1. 橢圓檢測opencv蟒蛇
- 2. 如何檢測使用的OpenCV +蟒蛇
- 3. 蟒蛇opencv全景黑線
- 4. OpenCV的蟒蛇OSX
- 5. opencv蟒蛇文檔
- 6. 線檢測的OpenCV
- 7. OpenCV的蟒蛇顏色檢測與布爾輸出
- 8. OpenCV的 - 蟒蛇 - 在cv2.inRange
- 9. OpenCV蟒蛇線以下代碼低FPS
- 10. cv2.waitkey(1)OpenCV的蟒蛇
- 11. OpenCV的由蟒蛇erorr
- 12. 蟒蛇找不到OpenCV的
- 13. Opencv蟒蛇程序
- 14. 檢測從bash或蟒蛇
- 15. Trackbar回調蟒蛇openCV
- 16. 蟒蛇延長
- 17. 檢測停車場線路和ROI openCV
- 18. OpenCV的線/尺檢測
- 19. 蟒蛇線程
- 20. 問題與opencv蟒蛇VideoWriter
- 21. 蟒蛇opencv:沒有圖像
- 22. 閱讀OpenCV的(蟒蛇)的視頻
- 23. 蟒蛇win32api阻止瓶路線
- 24. 拆包INT +長長的蟒蛇
- 25. 蟒蛇的crontab和路徑
- 26. 蟒蛇環路檢查變量
- 27. 3.2 OpenCV的支持CUDA蟒蛇
- 28. 使用opencv蟒蛇的Ubuntu 15.10
- 29. 使用OpenCV的拼接蟒蛇
- 30. 如何使用OpenCV的蟒蛇
感謝隊友!圖像的膨脹使我的一天! :) –