Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何改进grabcut算法的图像分割?_Java_Android_Opencv_Kotlin_Image Processing - Fatal编程技术网

Java 如何改进grabcut算法的图像分割?

Java 如何改进grabcut算法的图像分割?,java,android,opencv,kotlin,image-processing,Java,Android,Opencv,Kotlin,Image Processing,我正在开发一个kotlin应用程序来删除图像中的背景。为此,我将使用grabcut和​​要削减的地区的利益。用户用矩形标记该区域,坐标被传递给算法,该算法依次执行背景移除。但是,该算法无法在某些图像上很好地工作,并最终删除了不需要删除的区域,如下图所示。如何改进图像分割以获得更好的grabcut性能? 出口: 代码: typealias坐标=成对 private fun extractForegroundFromBackground(坐标:坐标,currentPhotoPath:String

我正在开发一个kotlin应用程序来删除图像中的背景。为此,我将使用grabcut和​​要削减的地区的利益。用户用矩形标记该区域,坐标被传递给算法,该算法依次执行背景移除。但是,该算法无法在某些图像上很好地工作,并最终删除了不需要删除的区域,如下图所示。如何改进图像分割以获得更好的grabcut性能?

出口:

代码:

typealias坐标=成对
private fun extractForegroundFromBackground(坐标:坐标,currentPhotoPath:String):字符串{
//OpenCV将在内部使用的矩阵
val bgModel=Mat()
val fgModel=Mat()
val srcImage=Imgcodecs.imread(当前光路)
val迭代次数=5
//遮罩图像,其中我们指定哪些区域是背景、前景或可能的背景/前景
val firstMask=Mat()
val source=Mat(1,1,CvType.CV_8U,标量(Imgproc.GC_PR_FGD.toDouble())
val rect=rect(coordinates.first,coordinates.second)
//使用矩形运行抓取切割算法(对于后续使用补漆笔划的迭代,
//标志应为Imgproc.GC_INIT_(带_掩码)
grabCut(srcImage、firstMask、rect、bgModel、fgModel、iterations、Imgproc.GC_INIT_WITH_rect)
//创建0和1的矩阵,指示各个像素是否相等
//或者“第一个掩码”和“源”对象之间存在差异
//结果存储回“firstMask”
比较(firstMask,source,firstMask,Core.CMP_EQ)
//创建一个矩阵来表示前景,用白色填充
val前台=Mat(srcImage.size(),CvType.CV_8UC3,标量(255.0,255.0,255.0))
//将前景矩阵复制到第一个遮罩
srcImage.copyTo(前台,第一个掩码)
//创建红色
val颜色=标量(255.0,0.0,0.0,255.0)
//使用包围前景的边界框的坐标绘制矩形
矩形(srcImage,coordinates.first,coordinates.second,color)
//创建一个新的矩阵来表示背景,填充白色
val background=Mat(srcImage.size(),CvType.CV_8UC3,标量(0.0,0.0,0.0))
val mask=Mat(前台.size(),CvType.CV_8UC1,标量(255.0,255.0,255.0))
//将前景的颜色空间从BGR转换为灰度
Imgproc.cvt颜色(前景、遮罩、Imgproc.COLOR\u bgr2灰色)
//通过将像素强度与阈值进行比较,分离出遮罩区域
Imgproc.threshold(掩码,掩码,254.0,255.0,Imgproc.THRESH\u二进制\u INV)
//创建一个矩阵以保存最终图像
val dst=Mat()
//将背景矩阵复制到表示最终结果的矩阵上
背景。复制到(dst)
val val=Mat(1,1,CvType.CV_8UC3,标量(0.0))
//在给定前景遮罩的情况下,替换背景矩阵中的所有0值
背景。设置为(VAL、掩码)
//通过应用遮罩,添加背景矩阵和前景矩阵之和
添加(背景、前景、dst、掩码)
//将最终图像保存到存储器
Imgcodecs.imwrite(currentPhotoPath+“_tmp.png”,dst)
//清理使用过的资源
firstMask.release()
来源:release()
bgModel.release()
fgModel.release()
vals.release()
dst.release()
回流光路
}
typealias Coordinates = Pair<Point, Point>
private fun extractForegroundFromBackground(coordinates: Coordinates, currentPhotoPath: String): String {

    // Matrices that OpenCV will be using internally
    val bgModel = Mat()
    val fgModel = Mat()

    val srcImage = Imgcodecs.imread(currentPhotoPath)
    val iterations = 5

    // Mask image where we specify which areas are background, foreground or probable background/foreground
    val firstMask = Mat()

    val source = Mat(1, 1, CvType.CV_8U, Scalar(Imgproc.GC_PR_FGD.toDouble()))
    val rect = Rect(coordinates.first, coordinates.second)

    // Run the grab cut algorithm with a rectangle (for subsequent iterations with touch-up strokes,
    // flag should be Imgproc.GC_INIT_WITH_MASK)
    Imgproc.grabCut(srcImage, firstMask, rect, bgModel, fgModel, iterations, Imgproc.GC_INIT_WITH_RECT)

    // Create a matrix of 0s and 1s, indicating whether individual pixels are equal
    // or different between "firstMask" and "source" objects
    // Result is stored back to "firstMask"
    Core.compare(firstMask, source, firstMask, Core.CMP_EQ)

    // Create a matrix to represent the foreground, filled with white color
    val foreground = Mat(srcImage.size(), CvType.CV_8UC3, Scalar(255.0, 255.0, 255.0))

    // Copy the foreground matrix to the first mask
    srcImage.copyTo(foreground, firstMask)

    // Create a red color
    val color = Scalar(255.0, 0.0, 0.0, 255.0)
    // Draw a rectangle using the coordinates of the bounding box that surrounds the foreground
    Imgproc.rectangle(srcImage, coordinates.first, coordinates.second, color)

    // Create a new matrix to represent the background, filled with white color
    val background = Mat(srcImage.size(), CvType.CV_8UC3, Scalar(0.0, 0.0, 0.0))

    val mask = Mat(foreground.size(), CvType.CV_8UC1, Scalar(255.0, 255.0, 255.0))
    // Convert the foreground's color space from BGR to gray scale
    Imgproc.cvtColor(foreground, mask, Imgproc.COLOR_BGR2GRAY)

    // Separate out regions of the mask by comparing the pixel intensity with respect to a threshold value
    Imgproc.threshold(mask, mask, 254.0, 255.0, Imgproc.THRESH_BINARY_INV)

    // Create a matrix to hold the final image
    val dst = Mat()
    // copy the background matrix onto the matrix that represents the final result
    background.copyTo(dst)

    val vals = Mat(1, 1, CvType.CV_8UC3, Scalar(0.0))
    // Replace all 0 values in the background matrix given the foreground mask
    background.setTo(vals, mask)

    // Add the sum of the background and foreground matrices by applying the mask
    Core.add(background, foreground, dst, mask)

    // Save the final image to storage
    Imgcodecs.imwrite(currentPhotoPath + "_tmp.png", dst)

    // Clean up used resources
    firstMask.release()
    source.release()
    bgModel.release()
    fgModel.release()
    vals.release()
    dst.release()

    return currentPhotoPath
}