Chcę używać LineIterator w openCV 3.0 przy użyciu Pythona, czy nadal jest dostępny z openCV 3.0 zbudowany dla Pythona? Wygląda na to, że wszystkie odpowiedzi w Internecie wskazują na cv.InitLineIterator, który jest częścią modułu cv. Próbowałem zaimportować ten moduł, ale wygląda na to, że nie jest on dołączony do bieżącej wersji. Czy została przemianowana lub została właśnie usunięta?python openCV 3.0 LineIterator
9
A
Odpowiedz
20
Rozwiązałem swój własny problem. Line iterator wydaje się być niedostępny w bibliotece cv2. Dlatego stworzyłem własny iterator linii. Żadne pętle nie są używane, więc powinno być dość szybko. Oto kod, jeśli ktoś go potrzebuje:
def createLineIterator(P1, P2, img):
"""
Produces and array that consists of the coordinates and intensities of each pixel in a line between two points
Parameters:
-P1: a numpy array that consists of the coordinate of the first point (x,y)
-P2: a numpy array that consists of the coordinate of the second point (x,y)
-img: the image being processed
Returns:
-it: a numpy array that consists of the coordinates and intensities of each pixel in the radii (shape: [numPixels, 3], row = [x,y,intensity])
"""
#define local variables for readability
imageH = img.shape[0]
imageW = img.shape[1]
P1X = P1[0]
P1Y = P1[1]
P2X = P2[0]
P2Y = P2[1]
#difference and absolute difference between points
#used to calculate slope and relative location between points
dX = P2X - P1X
dY = P2Y - P1Y
dXa = np.abs(dX)
dYa = np.abs(dY)
#predefine numpy array for output based on distance between points
itbuffer = np.empty(shape=(np.maximum(dYa,dXa),3),dtype=np.float32)
itbuffer.fill(np.nan)
#Obtain coordinates along the line using a form of Bresenham's algorithm
negY = P1Y > P2Y
negX = P1X > P2X
if P1X == P2X: #vertical line segment
itbuffer[:,0] = P1X
if negY:
itbuffer[:,1] = np.arange(P1Y - 1,P1Y - dYa - 1,-1)
else:
itbuffer[:,1] = np.arange(P1Y+1,P1Y+dYa+1)
elif P1Y == P2Y: #horizontal line segment
itbuffer[:,1] = P1Y
if negX:
itbuffer[:,0] = np.arange(P1X-1,P1X-dXa-1,-1)
else:
itbuffer[:,0] = np.arange(P1X+1,P1X+dXa+1)
else: #diagonal line segment
steepSlope = dYa > dXa
if steepSlope:
slope = dX.astype(np.float32)/dY.astype(np.float32)
if negY:
itbuffer[:,1] = np.arange(P1Y-1,P1Y-dYa-1,-1)
else:
itbuffer[:,1] = np.arange(P1Y+1,P1Y+dYa+1)
itbuffer[:,0] = (slope*(itbuffer[:,1]-P1Y)).astype(np.int) + P1X
else:
slope = dY.astype(np.float32)/dX.astype(np.float32)
if negX:
itbuffer[:,0] = np.arange(P1X-1,P1X-dXa-1,-1)
else:
itbuffer[:,0] = np.arange(P1X+1,P1X+dXa+1)
itbuffer[:,1] = (slope*(itbuffer[:,0]-P1X)).astype(np.int) + P1Y
#Remove points outside of image
colX = itbuffer[:,0]
colY = itbuffer[:,1]
itbuffer = itbuffer[(colX >= 0) & (colY >=0) & (colX<imageW) & (colY<imageH)]
#Get intensities from img ndarray
itbuffer[:,2] = img[itbuffer[:,1].astype(np.uint),itbuffer[:,0].astype(np.uint)]
return itbuffer
Powiązane problemy
- 1. Zainstaluj OpenCV 3.0 z dodatkowymi modułami (przesiać, surfować ...) dla python
- 2. SimpleBlobDetector nie znaleziono w opencv 3.0 dla python
- 3. Python OpenCV błąd imshow
- 4. PyObjC + Python 3.0 Pytania
- 5. Obliczyć Gęste funkcje SIFT w OpenCV 3.0
- 6. Jaki jest cel ACCESS_FAST w OpenCV 3.0?
- 7. OpenCV 2.0 i Python
- 8. OpenCV HOGDescrypter Python
- 9. Python OpenCV Box2D
- 10. Region zainteresowania opencv python
- 11. Rozmiar obrazu (Python, OpenCV)
- 12. Błąd Python OpenCV Error
- 13. OpenCV Python calcOpticalFlowFarneback
- 14. Python OpenCV Template Matching error
- 15. Histogram rysunkowy w OpenCV-Python
- 16. Python i gpu Funkcje OpenCV
- 17. OpenCV Python i przesiać cechy
- 18. OpenCV Python: Brak funkcji drawMatchesknn
- 19. Python OpenCv. WaitKey nie odpowiada?
- 20. Python OpenCV Drzewo hierarchii konturu
- 21. wykrywanie ucznia w OpenCV & Python
- 22. Python openCV wykryj linie równoległe
- 23. Wykrywanie cech w powiązaniach Python w OpenCV
- 24. OpenCV + python - grab klatek z pliku wideo
- 25. Ustawianie parametrów kamery w OpenCV/Python
- 26. Uzyskanie powierzchni w obrębie konturów Opencv Python?
- 27. Jak połączyć dwie macierze w Python OpenCV?
- 28. Błąd przy użyciu knnMatch z OpenCV + Python
- 29. Pisanie wideo za pomocą OpenCV + Python + Mac
- 30. OpenCV Python: Draw minAreaRect (RotatedRect nie zaimplementowano)
Niesamowite! po prostu znacznie ułatwiłeś pracę –
Negatywne głosowanie było przez pomyłkę, nie mogę tego cofnąć, chyba że zmienisz odpowiedź. Przepraszam! :/ – eudoxos
Dzięki za udostępnienie @mohikhsan. Chciałem tylko zauważyć, że linia różni się nieco od tej podanej przez 'cv2.drawLine()': twoja linia nie zawiera pierwszego punktu 'P1', podczas gdy' cv2.drawLine() 'zawiera go. – krookedking