2016-11-02 4 views
0

Ich habe die Aufgabe, einen Screenshot zu segmentieren und den Screenshot wie ein Raster zu durchlaufen, wobei jeder vordefinierte Block als eigenes Bild gespeichert wird, ähnlich wie im Diagramm unten. Die tatsächliche Grid-Größe ist 10X8 Es bleibt Spalte 2 überspringen. Bitte, erziehen Sie mich auf meine logische Fehler, wie ich die If-Block 100 Mal umgeschrieben, 10 verschiedene Möglichkeiten in den letzten 3 Tagen vergebens.Beim Segmentieren eines Bildes überspringt die Schleife eine komplette Spalte.

 Col1 Col2 Col3 
Row1  1  2 3 
Row2  4  5 6 
Row3  7  8 9 
 
"Starting to loop image for each ball" 


    [int]$startCell = 1 
    [int]$startRow = 1 
    [int]$startColumn = 1 
    [int]$totalLoops = 81 


    [int]$startleftCoord = 293 
    [int]$starttopCoord = 90 
    [int]$startrightCoord = 385 
    [int]$startbottomCoord = 160 
    [int]$cellheight = 75 
    [int]$cellwidth = 100 
    [int]$offset = 2 


     Do 
     {   


      #get the current time and build the filename from it 
      $Time = (Get-Date) 

      [string] $FileName += "-cellshot" 
      $FileName = "$($Time.Month)" 

      $FileName += '-' 
      $FileName += "$($Time.Day)" 
      $FileName += '-' 
      $FileName += "$($Time.Year)" 
      $FileName += '-' 
      $FileName += "$($Time.Hour)" 
      $FileName += '-' 
      $FileName += "$($Time.Minute)" 
      $FileName += '-' 
      $FileName += "$($Time.Second)" 
      $FileName += '-' 
      $FileName += "$($Time.Millisecond)" 
      $FileName += '-' 
      $FileName += [string]$currentCell 

      $FileName += '.png' 

      #use join-path to add path to filename 
      [string] $FilePath = (Join-Path $Path $FileName) 



      if (!$currentCell -OR !$currentColumn -OR !$currentRow){ 
       "Initializing Globals" 

       $currentCell = $startCell 
       $currentColumn = $startColumn 
       $currentRow = $startRow 

      } 


      "Designate capture point" 

      if ($currentColumn -gt 1 -AND $currentColumn -lt 11) { 

       "Calculating side coordinates offset" 
       $newleftCoord = $startleftCoord+($currentColumn*$cellwidth) 
       $newrightCoord = $startrightCoord+($currentColumn*$cellwidth) 

      } elseif ($currentColumn -eq 11) { 

       "Resetting column coordinates " 
       $currentColumn = $startColumn 
       $newleftCoord = $startleftCoord 
       $newrightCoord = $startrightCoord 

       "Compensating for multiple rows offest" 
       $newtopCoord = $starttopCoord+($currentRow*$cellheight)+$offset 
       $newbottomCoord = $startbottomCoord+($currentRow*$cellheight)+$offset 
       $currentRow++ 


      }else{ 
       "Getting number one" 
       $newleftCoord = $startleftCoord 
       $newtopCoord = $starttopCoord 
       $newrightCoord = $startrightCoord 
       $newbottomCoord = $startbottomCoord 
      } 



      "Current Column is " + $currentColumn 
      "Current row is " + $currentRow 
      "Current cell is " + $currentCell 


      #save cellshot 
      $cellBounds = [Drawing.Rectangle]::FromLTRB($newleftCoord,$newtopCoord, $newrightCoord, $newbottomCoord) 
      $cellObject = New-Object Drawing.Bitmap $cellBounds.Width, $cellBounds.Height 
      $cellGraphics = [Drawing.Graphics]::FromImage($cellObject) 
      $cellGraphics.CopyFromScreen($cellBounds.Location, [Drawing.Point]::Empty, $cellBounds.Size) 
      $cellGraphics.Dispose()  
      $cellObject.Save($FilePath) 





      $currentColumn++ 
      $currentCell++ 


     }Until ($currentCell -eq $totalLoops) 
    Start-Sleep -Second 20 
} 




      #load required assembly 
      Add-Type -Assembly System.Windows.Forms 

      Start-Sleep -Seconds 5  

      $ballarray = @{} 

      Do { 



       #run screenshot function 
       # If ($ballarray.count -eq 20){ 
       # GenScreenshot 
       #  "Snapped screenshot - $Filename ." 
       # }else{ 
        Do{ 

        GetNewBall 
        }Until($currentCell -eq 80) 
       #} 


       #$bounds = [Drawing.Rectangle]::FromLTRB(307,129, 1060, 668) 
       #screenshot $bounds $Filepath 

       #Start-Sleep -Second 10 
      }Until($ballarray.count -eq 20) 

Antwort

0
1. $currentColumn is $null 

2. "Initializing Globals" 

3. $currentColumn = 1 

4. $newleftCoord = $startleftCoord #(is 293) 

5. $currentColumn++     #(now 2) 

6. if ($currentColumn -gt 1)   #(Yes, it's 2) 

7. $newleftCoord = $startleftCoord+($currentColumn*$cellwidth) 
       #= 293 + (2*100) 
       #= 493 

       #= jump from 293 to 493 = skipped column 

Fix: Zählung von 0 statt 1.

293 + 0*100 
293 + 1*100 
293 + 2*100 
+1

Vielen Dank beginnen sich die Zeit nehmen. Für weitere Ausführungen zu "Wie & Warum" erhielt ich die Ergebnisse, die ich hatte. Ihr "Fix" war genau die Lösung. Ich beneide dich. @TessellatingHeckler Vielen Dank für Ihre Großzügigkeit. Ich trage dazu bei und bleibe aktiv in der Community, um meinen Repräsentanten über 15 zu bekommen und deine Antwort zu bestätigen. Bis dahin geht hoffentlich jemand mit und stimmt ab. – Eric

+0

@Eric Danke :) Wenn Sie noch nicht, schauen Sie sich PowerShell ISE - es kommt mit Windows - öffnen Sie Ihr Skript in es, klicken Sie auf einige Code an der Spitze der Schleife, gehen Sie zu 'Debug -> Toggle Breakpoint (oder F9-Taste), die Linie wird rot - versuchen Sie verschiedene Linien, bis einer tut. Dann 'Debug -> Run/Continue '(F5), wird es ausgeführt und stoppt bei dieser Zeile. Sie können 'Debug -> Step Over '(F10 und F11) Zeile für Zeile ausführen. Wirklich nützlich können Sie die Maus über Variablen bewegen und ihre Werte sehen. Finden Sie genau, wann sich die Spaltenposition falsch ändert und warum. Führen Sie den Code in der Eingabeaufforderung "[DBG]: PS >>" aus und klicken Sie auf den angehaltenen Status. – TessellatingHeckler

+0

(PS, wenn Sie möchten, können Sie das Häkchen unter den Abstimmpfeilen markieren und 'meine Antwort' akzeptieren, was Ihre Frage als 'beantwortet' markiert und mir Punkte gibt). – TessellatingHeckler

Verwandte Themen