Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
ws1617:bilder_zu_bildern:protokolle:170209 [2017/02/09 17:35] isabelschwermer |
ws1617:bilder_zu_bildern:protokolle:170209 [2017/03/24 18:27] (aktuell) Chai_Tee.95-Cara |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
=== Protokoll vom 9. Februar 2017 === | === Protokoll vom 9. Februar 2017 === | ||
+ | Erstes funktionierendes Programm (Kantenerkennung/Niveaulinien + Turtle): \\ | ||
+ | Niveaulinien, bzw. Contours | ||
+ | <code python> | ||
+ | #!/usr/bin/env python | ||
+ | # -*- coding: utf-8 -*- | ||
+ | |||
+ | 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 | ||
+ | import turtle | ||
+ | import pickle | ||
+ | |||
+ | bild=misc.imread('hund_mittel_kl.jpg') # bild einlesen | ||
+ | |||
+ | #cap = cv2.VideoCapture(0) #bei kamera bild wird zu bild2 | ||
+ | #res, bild2 = 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! | ||
+ | |||
+ | |||
+ | 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]>15: #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 | ||
+ | </code> | ||
+ | |||
+ | Kantenprogramm | ||
+ | <code python> | ||
+ | bild=misc.imread('hund_mittel_kl.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(bild, axis=-1) #Verschiebungsbild, x | ||
+ | #cv2.namedWindow("Fenster 1") | ||
+ | #cv2.imshow("Fenster 1",b) | ||
+ | |||
+ | # y | ||
+ | b2=ndimage.filters.sobel(bild, 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,80) | ||
+ | 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 | ||
+ | </code> | ||
+ | |||
+ | Niveaulinien + Kantenprogramm verbunden | ||
+ | <code python> | ||
+ | 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 | ||
+ | </code> | ||
+ | |||
+ | Turtle.mod von Stefan Born | ||
+ | <code python> | ||
+ | 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") | ||
+ | self.t=MyTurtle() | ||
+ | self.t.pensize(1) | ||
+ | self.t.pencolor("blue") | ||
+ | self.t.shape("turtle") | ||
+ | self.t.speed(0) | ||
+ | self.t.tracer(1,delay=0) | ||
+ | |||
+ | 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: | ||
+ | tw.t.penup() | ||
+ | for p in kontour: | ||
+ | tw.t.turn_and_move(tw.image_to_turtle(p)) | ||
+ | tw.t.pendown() | ||
+ | |||
+ | def main(): | ||
+ | #l=misc.ascent() | ||
+ | bild_farbbild=cv2.cvtColor(np.uint8(bild),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() | ||
+ | turtle.exitonclick() | ||
+ | |||
+ | </code> | ||
+ | Das Ergebnis:\\ | ||
{{eimer.png}} | {{eimer.png}} |