Hough transform: Difference between revisions

Scala entry
m (added whitespace before the TOC (table of contents), added a ;Task: (bold) header.)
(Scala entry)
Line 661:
end
out
end</lang>
 
</lang>
=={{header|Scala}}==
{{trans|Kotlin}}
<lang scala>import java.awt.image._
import java.io.File
import javax.imageio._
 
object HoughTransform extends App {
override def main(args: Array[String]) {
val inputData = readDataFromImage(args(0))
val minContrast = if (args.length >= 4) 64 else args(4).toInt
val outputData = inputData(args(2).toInt, args(3).toInt, minContrast)
outputData.writeOutputImage(args(1))
}
 
private def readDataFromImage(filename: String) = {
val image = ImageIO.read(new File(filename))
val width = image.getWidth
val height = image.getHeight
val rgbData = image.getRGB(0, 0, width, height, null, 0, width)
val arrayData = new ArrayData(width, height)
for (y <- 0 until height; x <- 0 until width) {
var rgb = rgbData(y * width + x)
rgb = (((rgb & 0xFF0000) >>> 16) * 0.30 + ((rgb & 0xFF00) >>> 8) * 0.59 +
(rgb & 0xFF) * 0.11).toInt
arrayData(x, height - 1 - y) = rgb
}
arrayData
}
}
 
class ArrayData(val dataArray: Array[Int], 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) {
dataArray(y * width + x) = value
}
 
def apply(thetaAxisSize: Int, rAxisSize: Int, minContrast: Int) = {
val maxRadius = Math.ceil(Math.hypot(width, height)).toInt
val halfRAxisSize = rAxisSize >>> 1
val outputData = new ArrayData(thetaAxisSize, rAxisSize)
val sinTable = Array.ofDim[Double](thetaAxisSize)
val cosTable = sinTable.clone()
for (theta <- thetaAxisSize - 1 until -1 by -1) {
val thetaRadians = theta * Math.PI / thetaAxisSize
sinTable(theta) = Math.sin(thetaRadians)
cosTable(theta) = Math.cos(thetaRadians)
}
for (y <- height - 1 until -1 by -1; x <- width - 1 until -1 by -1)
if (contrast(x, y, minContrast))
for (theta <- thetaAxisSize - 1 until -1 by -1) {
val r = cosTable(theta) * x + sinTable(theta) * y
val rScaled = Math.round(r * halfRAxisSize / maxRadius).toInt + halfRAxisSize
outputData.accumulate(theta, rScaled, 1)
}
 
outputData
}
 
def writeOutputImage(filename: String) {
val max = dataArray.max
val image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
for (y <- 0 until height; x <- 0 until width) {
val n = Math.min(Math.round(get(x, y) * 255.0 / max).toInt, 255)
image.setRGB(x, height - 1 - y, (n << 16) | (n << 8) | 0x90 | -0x01000000)
}
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 = {
val centerValue = get(x, y)
for (i <- 8 until -1 by -1 if i != 4) {
val newx = x + (i % 3) - 1
val newy = y + (i / 3) - 1
if (newx >= 0 && newx < width && newy >= 0 && newy < height &&
Math.abs(get(newx, newy) - centerValue) >= minContrast)
return true
}
 
false
}
}</lang>
 
=={{header|Tcl}}==
Line 689 ⟶ 779:
out
}</lang>
 
<lang zkl>fcn readPNG2PPM(fileName){
p:=System.popen("convert \"%s\" ppm:-".fmt(fileName),"r");
Anonymous user