Hough transform: Difference between revisions

Content added Content deleted
m (separated comment from a task directive.)
(Scala improved)
Line 675: Line 675:
val inputData = readDataFromImage(args(0))
val inputData = readDataFromImage(args(0))
val minContrast = if (args.length >= 4) 64 else args(4).toInt
val minContrast = if (args.length >= 4) 64 else args(4).toInt
val outputData = inputData(args(2).toInt, args(3).toInt, minContrast)
inputData(args(2).toInt, args(3).toInt, minContrast).writeOutputImage(args(1))
outputData.writeOutputImage(args(1))
}
}


Line 695: Line 694:
}
}


class ArrayData(val dataArray: Array[Int], val width: Int, val height: Int) {
class ArrayData(val width: Int, val height: Int) {
def this(width: Int, height: Int) {
this(Array.ofDim[Int](width * height), width, height)
}

def get(x: Int, y: Int) = dataArray(y * width + x)

def update(x: Int, y: Int, value: Int) {
def update(x: Int, y: Int, value: Int) {
dataArray(y * width + x) = value
dataArray(x)(y) = value
}
}


Line 722: Line 715:
val r = cosTable(theta) * x + sinTable(theta) * y
val r = cosTable(theta) * x + sinTable(theta) * y
val rScaled = Math.round(r * halfRAxisSize / maxRadius).toInt + halfRAxisSize
val rScaled = Math.round(r * halfRAxisSize / maxRadius).toInt + halfRAxisSize
outputData.accumulate(theta, rScaled, 1)
outputData.dataArray(theta)(rScaled) += 1
}
}


Line 729: Line 722:


def writeOutputImage(filename: String) {
def writeOutputImage(filename: String) {
val max = dataArray.max
var max = Int.MinValue
for (y <- 0 until height; x <- 0 until width) {
val v = dataArray(x)(y)
if (v > max) max = v
}
val image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
val image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
for (y <- 0 until height; x <- 0 until width) {
for (y <- 0 until height; x <- 0 until width) {
val n = Math.min(Math.round(get(x, y) * 255.0 / max).toInt, 255)
val n = Math.min(Math.round(dataArray(x)(y) * 255.0 / max).toInt, 255)
image.setRGB(x, height - 1 - y, (n << 16) | (n << 8) | 0x90 | -0x01000000)
image.setRGB(x, height - 1 - y, (n << 16) | (n << 8) | 0x90 | -0x01000000)
}
}
ImageIO.write(image, "PNG", new File(filename))
ImageIO.write(image, "PNG", new File(filename))
}

private def accumulate(x: Int, y: Int, delta: Int) {
update(x, y, get(x, y) + delta)
}
}


private def contrast(x: Int, y: Int, minContrast: Int): Boolean = {
private def contrast(x: Int, y: Int, minContrast: Int): Boolean = {
val centerValue = get(x, y)
val centerValue = dataArray(x)(y)
for (i <- 8 until -1 by -1 if i != 4) {
for (i <- 8 until -1 by -1 if i != 4) {
val newx = x + (i % 3) - 1
val newx = x + (i % 3) - 1
val newy = y + (i / 3) - 1
val newy = y + (i / 3) - 1
if (newx >= 0 && newx < width && newy >= 0 && newy < height &&
if (newx >= 0 && newx < width && newy >= 0 && newy < height &&
Math.abs(get(newx, newy) - centerValue) >= minContrast)
Math.abs(dataArray(newx)(newy) - centerValue) >= minContrast)
return true
return true
}
}
Line 754: Line 747:
false
false
}
}

private val dataArray = Array.ofDim[Int](width, height)
}</lang>
}</lang>