我正在使用OpenCV 2.4.3 c ++接口来查找两个图像之间的匹配点.第一次尝试是使用SURF.唯一的问题是消耗时间,所以我尝试了新的FREAK提取器.使用SURF进行检测和FREAK进行描述,我意识到FREAK将关键点的数量减少到几乎检测到的一半,并且得到的匹配不够.这就是原因,我尝试了FAST以获得更多关键点.结果:
之后,我使用了ORBFeatureDetector,它获得了与FAST相同数量的关键点,但在FREAK提取器之后,每个图像的结果关键点为0.难道我做错了什么?ORB关键点是否与从FAST获得的关键点不同?也许我可以为此开另一个问题,但我有最后一个问题.检测器/提取器的最佳组合是什么才能获得与使用SURF的第一次实验相同的结果,但缩短处理时间?因为虽然我使用了FREAK,但是当我获得更多关键点时,提取器部分也更耗时.
我不能在unapply提取器的方法上使用泛型以及隐式"转换器"来支持特定于参数化类型的模式匹配吗?
我想这样做(注意使用[T]就unapply行),
trait StringDecoder[A] {
def fromString(string: String): Option[A]
}
object ExampleExtractor {
def unapply[T](a: String)(implicit evidence: StringDecoder[T]): Option[T] = {
evidence.fromString(a)
}
}
object Example extends App {
implicit val stringDecoder = new StringDecoder[String] {
def fromString(string: String): Option[String] = Some(string)
}
implicit val intDecoder = new StringDecoder[Int] {
def fromString(string: String): Option[Int] = Some(string.charAt(0).toInt)
}
val result = "hello" match {
case ExampleExtractor[String](x) => x // <- type hint barfs …Run Code Online (Sandbox Code Playgroud) 基本上,我希望能够构建一个自定义提取器,而无需在使用它之前将其存储在变量中.
这不是我将如何使用它的一个真实示例,它更可能用于正则表达式或其他字符串模式(如构造),但希望它能解释我正在寻找的内容:
def someExtractorBuilder(arg:Boolean) = new {
def unapply(s:String):Option[String] = if(arg) Some(s) else None
}
//I would like to be able to use something like this
val {someExtractorBuilder(true)}(result) = "test"
"test" match {case {someExtractorBuilder(true)}(result) => result }
//instead I would have to do this:
val customExtractor = someExtractorBuilder(true)
val customExtractor(result) = "test"
"test" match {case customExtractor(result) => result}
Run Code Online (Sandbox Code Playgroud)
当只做一个自定义提取器时,它没有太大的区别,但如果你为case语句构建一个大的提取器列表,它可能会通过将所有提取器与它们的使用分开来使事情变得更难.
我希望答案是不,你不能这样做,但我想我先问一下:D
给出以下代码:
abstract class MyTuple
...
case class MySeptet(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int, g: Int) extends MyTuple
case class MyOctet(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int, g: Int, h: Int) extends MyTuple
...
Run Code Online (Sandbox Code Playgroud)
使用生成的提取器时,是否可以跳过剩余的参数,假设它们未被使用?
例如,我不想在下面的代码片段中写下大量的下划线:
case MyOctet(a, b, _, _, _, _, _, _) => ... // uses only a and b
Run Code Online (Sandbox Code Playgroud) I'm often writing code that compares two objects and produces a value based on whether they are the same, or different, based on how they are different.
So I might write:
val result = (v1,v2) match {
case (Some(value1), Some(value2)) => "a"
case (Some(value), None)) => "b"
case (None, Some(value)) => "b"
case _ = > "c"
}
Run Code Online (Sandbox Code Playgroud)
Those 2nd and 3rd cases are the same really, so I tried writing:
val result = (v1,v2) match {
case (Some(value1), Some(value2)) …Run Code Online (Sandbox Code Playgroud) 我最近一直在玩提取器,并且想知道List提取器如何工作尤其如此:
List(1, 2, 3) match {
case x :: y :: z :: Nil => x + y + z // case ::(x, ::(y, ::(z , Nil)))
}
Run Code Online (Sandbox Code Playgroud)
Ok ::用在模式中,所以我猜编译器现在在:: - Object中查找unapply方法.试过这个:
scala> (::).unapply(::(1, ::(2, Nil)))
res3: Option[(Int, List[Int])] = Some((1,List(2)))
Run Code Online (Sandbox Code Playgroud)
很好,有效.但是,这不是:
scala> (::).unapply(List(1,2,3))
<console>:6: error: type mismatch;
found : List[Int]
required: scala.collection.immutable.::[?]
(::).unapply(List(1,2,3))
Run Code Online (Sandbox Code Playgroud)
这样做:
scala> List.unapplySeq(List(1,2,3))
res5: Some[List[Int]] = Some(List(1, 2, 3))
Run Code Online (Sandbox Code Playgroud)
其实我此刻有点困惑.编译器如何在此处选择正确的unapply实现.
我有一个简单的类层次结构,它表示一个类似于图形的结构,其中有几个不同类型的顶点使用case类实现:
sealed trait Node
sealed abstract case class Vertex extends Node
case class Arc extends Node
case class VertexType1 (val a:Int) extends Vertex
case class VertexType2 (val b:Int) extends Vertex
Run Code Online (Sandbox Code Playgroud)
这允许我写这样的匹配块:
def test (x: Node) = x match {
case _ : Arc => "got arc"
case _ : Vertex => "got vertex"
}
Run Code Online (Sandbox Code Playgroud)
或者像这样:
def test (x: Node) = x match {
case _ : Arc => "got arc"
case c : Vertex => c match {
case _ …Run Code Online (Sandbox Code Playgroud) Scala中是否存在允许提取器采用自定义参数的语法?这个例子有点人为.假设我在整数上有一个二叉搜索树,如果它的值可以被某个自定义值整除,我想匹配当前节点.
使用F#活动模式,我可以执行以下操作:
type Tree =
| Node of int * Tree * Tree
| Empty
let (|NodeDivisibleBy|_|) x t =
match t with
| Empty -> None
| Node(y, l, r) -> if y % x = 0 then Some((l, r)) else None
let doit = function
| NodeDivisibleBy(2)(l, r) -> printfn "Matched two: %A %A" l r
| NodeDivisibleBy(3)(l, r) -> printfn "Matched three: %A %A" l r
| _ -> printfn "Nada"
[<EntryPoint>]
let main args =
let …Run Code Online (Sandbox Code Playgroud) 有没有办法PartialFunction通过case声明创建一个除外?
我很好奇,因为我想表达以下内容(scala pseudo ahead!)...
val bi = BigInt(_)
if (bi.isValidInt) bi.intValue
Run Code Online (Sandbox Code Playgroud)
......作为一个部分功能,并做
val toInt : PartialFunction[String, Int] = {
case s if BigInt(s).isValidInt => BigInt(s).intValue
}
Run Code Online (Sandbox Code Playgroud)
因为我创造了BigInt两次似乎是多余的.
给出以下代码snippelt:
import scala.util.Try
def foo(x:Int) : (Int, String) = {
(x+1, x.toString)
}
def main(args: Array[String]) : Unit = {
val r1: Try[(Int, String)] = for {
v <- Try { foo(3) }
} yield v
val r2: Try[(Int, String)] = for {
(i, s) <- Try { foo(3) } // compile warning refers to this line
} yield (i, s)
}
Run Code Online (Sandbox Code Playgroud)
1.为什么编译上面的代码会抛出以下警告?
`withFilter' method does not yet exist on scala.util.Try[(Int, String)], using `filter' method instead
[warn] (i, s) <- …Run Code Online (Sandbox Code Playgroud)