我正在处理代码以找到最长的分支,这是一个在我的二进制图像中找到下一个未访问点的函数: 注意:我在vectVisitedPoint中保存找到像素的数组缓冲区。
var vectVisitedPoint= new scala.collection.mutable.ArrayBuffer[Point]()
var pTemp=new Point (0,0)
var res = new Array[Byte](1)
img.get(pTemp.x.toInt,pTemp.y.toInt,res) //img is a binary image
var value1: Int=0
var value2: Int=0
scala.util.control.Breaks.breakable {
while((value1 < img.rows ) ){
while ( (value2 < img.cols )){
if (res(0) == -1 && vectVisitedPoint.exists(_ == (value1, value2))) {
pTemp.x=(pTemp.x.toInt)+value1
pTemp.y=(pTemp.y.toInt)+value2
vectVisitedPoint.append(new Point(pTemp.x,pTemp.y)
scala.util.control.Breaks.break()
}
value2=value2+1
img.get(value1,value2,res)
}
value2=0
value1=value1+1
}
}
}
答案 0 :(得分:2)
def longestBranch(binImage: TypeOfBinImage): Vector[Point] = {
var res = new Array[Byte](1)
def getRes(x: Int, y: Int) = {
binImage.get(x, y, res)
res
}
(0 until img.rows).foldLeft(Vector.empty[Point])((visited, x) =>
(0 until img.cols).find(y =>
getRes(x, y)(0) == -1 && visited.exists(_ == (x, y)))
.map(y => visited :+ new Point(visited.last + x, visited.last + y))
.getOrElse(visited)
)
}
注意:我认为这不是更高/更低效,但肯定更多是Scalaery