【问题标题】:Color values have the same value all over the image颜色值在整个图像中具有相同的值
【发布时间】:2019-11-01 03:52:57
【问题描述】:

我有一个对象的轮廓和中心坐标。现在我正在尝试检测位于轮廓边界上的点,但仍然与中心处于相同的高度(y)上。背景设置为黑色。因此,我正在计算阈值图像并遍历像素并检查它们是否设置为白色。问题是根据我的代码,所有值都是 0。

也尝试处理原始图像。问题仍然存在,我总是得到 0/255/255。无论我是在物体内部还是在背景中。

private fun calcPointOnContour(point: Point, image: Mat): Point {
    var pointOnContour = Point()
    val ycrcb = getCbComponent(image)
    val imageThresh = getThresholdImage(ycrcb)

    for (i in point.x.toInt() until image.cols()) {
        val pixel = imageThresh.get(i, point.y.toInt())

        if (pixel[0] < 255) {
            pointOnContour = Point(i.toDouble(), point.y)
            break
        }
    }

    return pointOnContour
}

private fun getCbComponent(mat: Mat): Mat {
    val ycrcb = Mat(mat.rows(), mat.cols(), CvType.CV_8UC3)
    val lYCrCb = ArrayList<Mat>(3)

    Imgproc.cvtColor(mat, ycrcb, Imgproc.COLOR_RGB2YCrCb)
    Core.split(mat, lYCrCb)

    return lYCrCb[2]
}

private fun getThresholdImage(mat: Mat): Mat {
    val imageThresh = Mat.zeros(mat.rows(), mat.cols(), CvType.CV_8UC1)
    Imgproc.threshold(mat, imageThresh, 100.0, 255.0, Imgproc.THRESH_BINARY)

    return imageThresh
}

【问题讨论】:

    标签: android opencv image-processing mobile kotlin


    【解决方案1】:

    Imgproc.threshold 是一种根据您提供的阈值将图像转换为二进制的方法。 https://docs.opencv.org/2.4/doc/tutorials/imgproc/threshold/threshold.html

    您提供的 maxVal 为 255,因此只有 0 或 255。

    ====

    Imgproc.cvtColor(mat, ycrcb, Imgproc.COLOR_RGB2YCrCb)
    Core.split(mat, lYCrCb)
    

    我觉得应该是Core.split(ycrcb, lYCrCb)

    另外,您可以在从图像创建原始 Mat 的位置添加代码吗?也许,那里有一些问题。

    【讨论】:

    • 你对 Core.split(ycrcb, lYCrCb) 是完全正确的。这是我尝试检查原始图像中的颜色值是否不同的遗留问题。在这些特定方法之前使用的整个代码在此处发布有点太多了,但基本上我从外部存储加载 jpeg 图像,过滤背景并将图像裁剪到感兴趣的区域。跨度>
    • 可以关闭...翻转 image.get() 中的值...感谢您也查看此内容:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    相关资源
    最近更新 更多