---
# System prepended metadata

title: Untitled

---

```kotlin=
    private fun cutLineB(data: ByteArray, kernelSize: ArrayList<Double>): ByteArray? {

        try {
            var byteArray = ByteArray(0)
            var dst = Mat()
            // Decode image from input byte array
            var src = Imgcodecs.imdecode(MatOfByte(*data), Imgcodecs.IMREAD_UNCHANGED)
            // 設置結構元素，用於膨脹操作
            var kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, Size(kernelSize[0], kernelSize[1]))
            //提取圖片的 alpha 通道
            val alpha = Mat()
            Core.extractChannel(src, alpha, 3)
            //對 alpha 通道進行膨脹操作，增強圖片中的邊緣
            Imgproc.dilate(alpha, alpha, kernel, Point(-1.0, -1.0), 5)
            //將 alpha 通道轉為二值圖像
            var thresh = Mat()
            Imgproc.threshold(alpha, thresh, 200.0, 255.0, Imgproc.THRESH_BINARY)
            // 提取輪廓
            var contours = ArrayList<MatOfPoint>()
            var hierarchy = Mat()
            Imgproc.findContours(thresh, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE)
            // 在原圖上畫出輪廓
            Imgproc.drawContours(src, contours, -1, Scalar(0.0, 0.0, 0.0, 255.0), 2)

            // instantiating an empty MatOfByte class
            val matOfByte = MatOfByte()
            // Converting the Mat object to MatOfByte
            Imgcodecs.imencode(".png", src, matOfByte)
            byteArray = matOfByte.toArray()
            return byteArray
        } catch (e: java.lang.Exception) {
            println("OpenCV Error: $e")
            return data
        }
    }
```

$\sum_{i=1}^n a_i$ <- 這是Latex語法，用$$包起來就可以打