Dies ist eine alte Version des Dokuments!
Unser aktuelles Programm:
#!/usr/bin/env python # -*- coding: utf-8 -*- #Niveaulinien, bzw. Contours-Programm ; Kamera einlesen import numpy as np import matplotlib.pyplot as plt from scipy import misc, ndimage, signal import cv2 import scipy.misc as misc import skimage.color as color laenge=int(raw_input("Geben Sie die Laenge der Niveaulinien an: ")) percentile=int(raw_input("Geben Sie die Staerke der Kanten an(max bis 100): ")) #bild=misc.imread('Heuchera.jpg') # bild einlesen cap = cv2.VideoCapture(0) #bei kamera bild wird zu bild2 res, bild = cap.read() #bei kamera imgray =color.rgb2gray(bild) from skimage.measure import find_contours res = [] for value in np.linspace(np.min(imgray), np.max(imgray),10): res.extend(find_contours(imgray,value)) #for cont in res: # print cont.shape # alte Liste aller Contours! cap.release() #wenn kamera def Isolux(): #a_als_farbbild = cv2.cvtColor(a, cv2.COLOR_GRAY2BGR) #a ist ein s/w-bild, um farbe überhaupt anzeigen zu können, wird es in ein bgr konvertiert res2 = [] for cont in res: if cont.shape[0]>laenge: #and ... Fkt.Check for i,j in cont: cv2.circle(bild,(int(round(j)),int(round(i))),1,(1,0,0),-1) res2.append(cont) #cv2.imshow("Bild:",bild) #cv2.waitKey(0) #cv2.destroyAllWindows() return res2 res2 = Isolux() # unsere Liste der gewünschten Contours res2 (ausgefiltert) #print res2 # Contouren! #for cont in res2: # print cont.shape # Contourenanzahl #Kantenprogramm import numpy as np import matplotlib.pyplot as plt from scipy import misc, ndimage import cv2 #bild=misc.imread('Heuchera.jpg',mode='F',) bild=ndimage.filters.gaussian_filter(bild,1.5) #plt.imshow(bild,cmap=plt.get_cmap('gray')) #Original #plt.show() # x b=ndimage.filters.sobel(imgray, axis=-1) #Verschiebungsbild, x #cv2.namedWindow("Fenster 1") #cv2.imshow("Fenster 1",b) # y b2=ndimage.filters.sobel(imgray, axis=0) #Verschiebungsbild, y #cv2.namedWindow("Fenster 2") #cv2.imshow("Fenster 2",b2) a=np.sqrt(b[1:,:]**2+b2[:-1,:]**2) # b und b2 werden zum Kantenbild a verrechnet #Kantenbild #plt.imshow(a,cmap=plt.get_cmap('gray')) #plt.show() #cv2.waitKey(0) #cv2.destroyAllWindows() median2 = np.percentile(a,percentile) z = a.shape[0] #y z2 = range(0,z) s = a.shape[1] #x s2 = range(0,s) alle_koordinaten = [] for i in z2: for j in s2: alle_koordinaten.append((i,j)) kandidaten = [ p for p in alle_koordinaten if a[p[0],p[1]]>median2 ] #Rumspielbar...mit Schwellenwert def kurvenversuch(): #Nur zum Veranschaulichen! a_als_farbbild = cv2.cvtColor(a, cv2.COLOR_GRAY2BGR) #a ist ein s/w-bild, um farbe überhaupt anzeigen zu können, wird es in ein bgr konvertiert for i,j in kandidaten: cv2.circle(a_als_farbbild,(j,i),1,(1,0,0),-1) cv2.imshow("1:",a_als_farbbild) cv2.waitKey(0) cv2.destroyAllWindows() #print kandidaten #print len(kandidaten) #Koordinaten x und y #print len(a) #Helligkeitswerte #Allg. Aufbau: contouren = res2 #Liste in Listen kant = kandidaten #hat Schwellenwert #Kantenbild = a Schwellw = np.median(a) beste_Werte = [] def check(cont, Kant): hell = 0. #auf 0 gesetzt for pkt in cont: try: # try aufgrund der Rundungsfehler, über 319 hell += a[int(round(pkt[0])),int(round(pkt[1]))] #werden aufgerechnet except: pass if hell > Schwellw*len(cont): return True else: return False for cont in contouren: if check(cont, a): #print cont beste_Werte.append(cont) else: pass #print beste_Werte #turtle mod #!/usr/bin/env python # -*- coding: utf-8 -*- # # bewegeturtle.py # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # # import turtle import pickle import cv2 import numpy as np from scipy import misc def smooth(kont): #? for i in range(1,len(kont)-1): kont[i]=1/3.*(kont[i-1]+kont[i]+kont[i+1]) class MyTurtle(turtle.Turtle): #? def turn_and_move(self,p): self.setheading(self.towards(p)) self.goto(p) class turtle_world(object): def __init__(self,bild): #pencv speichert die Bilder als BGR! self.screen=turtle.Screen() self.xsize=bild.shape[1] self.ysize=bild.shape[0] turtle.screensize(self.xsize,self.ysize) misc.imsave("zwischenbild_.gif",bild) #turtle.bgpic("zwischenbild_.gif") #Setzt das Hintergrudbild weg self.t=MyTurtle() self.t.pensize(1) self.t.pencolor("blue") self.t.shape("turtle") self.t.speed(0) self.t.tracer(0,delay=0) #nur 0 für Mac turtle, 1 bei normal def image_to_turtle(self,point): '''Rechnet die Koordinaten des Bild-Arrays auf die Turtle-Koordinaten um. Returns: Koordinatentupel.''' return (point[1]-(self.xsize//2),(self.ysize//2)-point[0]) def turtle_to_image(self,point): '''Rechnet die Turtle-Koordinaten auf Bild-Array-Koordinaten um. Returns: numpy-Vektor mit Bildkoordinaten''' return np.array([self.ysize//2-point[1], point[0]+self.xsize//2]) #______________________ def bewege_turtle_auf_bild(bild,kontouren): '''Lässt Turtle auf Hintergrundbild laufen. Dabei ist bild ein BGR-Bild (opencv-Farbbild) und kontouren eine Liste von Listen von Punkten.''' tw=turtle_world(bild) ### Hier müsste die Bewegung der Turtle hin for kontour in kontouren: #mac turtle:Hier Zähler einbauen, der alle 20. Konturen einmal zeichnet, damit es nicht zu schnell geht tw.t.penup() for p in kontour: tw.t.turn_and_move(tw.image_to_turtle(p)) tw.t.pendown() turtle.update() #nur bei Mac turtle def main(): #l=misc.ascent() bild_farbbild=cv2.cvtColor(np.uint8(imgray),cv2.cv.CV_GRAY2BGR) #kontour=[[50,50],[70,400],[200,100],[300,300]] #hier Konturen übergeben? kontouren=beste_Werte bewege_turtle_auf_bild(bild_farbbild,kontouren) return 0 if __name__ == '__main__': main()