【问题标题】:While Segmenting an image, the loop skips a complete column分割图像时,循环跳过完整的列
【发布时间】:2016-11-02 05:20:39
【问题描述】:

我的任务是分割屏幕截图并像网格一样循环浏览屏幕截图,将每个预定义块保存为自己的图像,就像下图一样。实际网格大小是 10X8 它一直跳过第 2 列。请告诉我我的逻辑缺陷,因为我在过去 3 天里用 10 种不同的方式重写了 if 块 100 次,但无济于事。

        Col1 Col2 Col3
Row1     1     2    3
Row2     4     5    6 
Row3     7     8    9
“开始为每个球循环图像” [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]$单元格宽度 = 100 [int]$偏移量 = 2 做 { #获取当前时间并从中构建文件名 $Time =(获取日期) [字符串] $FileName += "-cellshot" $FileName = "$($Time.Month)" $文件名 += '-' $FileName += "$($Time.Day)" $文件名 += '-' $FileName += "$($Time.Year)" $文件名 += '-' $FileName += "$($Time.Hour)" $文件名 += '-' $FileName += "$($Time.Minute)" $文件名 += '-' $FileName += "$($Time.Second)" $文件名 += '-' $FileName += "$($Time.Millisecond)" $文件名 += '-' $FileName += [字符串]$currentCell $文件名 += '.png' #use join-path 添加路径到文件名 [字符串] $FilePath = (加入路径 $Path $FileName) if (!$currentCell -OR !$currentColumn -OR !$currentRow){ “初始化全局” $currentCell = $startCell $currentColumn = $startColumn $currentRow = $startRow } “指定捕获点” if ($currentColumn -gt 1 -AND $currentColumn -lt 11) { “计算侧坐标偏移” $newleftCoord = $startleftCoord+($currentColumn*$cellwidth) $newrightCoord = $startrightCoord+($currentColumn*$cellwidth) } elseif ($currentColumn -eq 11) { “重置列坐标” $currentColumn = $startColumn $newleftCoord = $startleftCoord $newrightCoord = $startrightCoord “补偿多行错误” $newtopCoord = $starttopCoord+($currentRow*$cellheight)+$offset $newbottomCoord = $startbottomCoord+($currentRow*$cellheight)+$offset $currentRow++ }别的{ “获得第一” $newleftCoord = $startleftCoord $newtopCoord = $starttopCoord $newrightCoord = $startrightCoord $newbottomCoord = $startbottomCoord } “当前列是” + $currentColumn “当前行是” + $currentRow “当前单元格是” + $currentCell #保存细胞截图 $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) $当前列++ $currentCell++ }直到 ($currentCell -eq $totalLoops) 开始-睡眠-第二 20 } #加载所需的程序集 Add-Type -Assembly System.Windows.Forms 开始-睡眠-秒 5 $ballarray = @{} 做 { #运行截图功能 # if ($ballarray.count -eq 20){ # GenScreenshot # "截图 - $Filename ." # }别的{ 做{ 获取新球 }直到($currentCell -eq 80) #} #$bounds = [Drawing.Rectangle]::FromLTRB(307,129, 1060, 668) #screenshot $bounds $文件路径 #Start-Sleep -Second 10 }直到($ballarray.count -eq 20)

【问题讨论】:

    标签: powershell nested-loops do-while image-segmentation


    【解决方案1】:
    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
    

    修复:从 0 开始计数,而不是 1。

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

    【讨论】:

    • 非常感谢您抽出宝贵时间。为了进一步详细说明“如何以及为什么”,我得到了我的结果。您的“修复”正是解决方案。我羡慕你。 @TessellatingHeckler 感谢您的慷慨。我为社区做出贡献并保持活跃,以使我的代表超过 15 岁,然后相信您的回答。在那之前,希望有人能一路走来投票。
    • @Eric 谢谢 :) 如果您还没有,请查看 PowerShell ISE - 它随 Windows 提供 - 在其中打开您的脚本,单击循环顶部的一些代码,转到 @ 987654323@(或 F9 键),该行变为红色 - 尝试不同的行,直到出现。然后Debug -> Run/Continue (F5),它将在该行运行并停止。您可以Debug -> Step Over(F10 和 F11)一次一行。非常有用的是,您可以将鼠标悬停在变量上并查看它们的值。准确查找列位置何时错误更改以及原因。在[DBG]: PS >>提示符下运行代码,戳到暂停状态。
    • (PS,如果您愿意,可以在投票箭头下方打勾并“接受”我的回答,这会将您的问题标记为“已回答”并给我积分)。
    猜你喜欢
    • 2017-02-11
    • 1970-01-01
    • 2023-02-05
    • 2016-10-24
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多