Android OpenCV(七):多通道分离与合并

多通道分离

API

public static void split(Mat m, List<Mat> mv)
  • 参数一:m,待分离的多通道图像
  • 参数二:mv,分离后的单通道图像

操作

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mBinding = DataBindingUtil.setContentView(this, R.layout.activity_channel_split_merge)
    mBgr = Utils.loadResource(this, R.drawable.test)
    val imgList = mutableListOf<Mat>()
    Core.split(mBgr, imgList)
    mChannelB = imgList[0]
    mChannelG = imgList[1]
    mChannelR = imgList[2]
}

通道合并

API

public static void merge(List<Mat> mv, Mat dst)
  • 参数一:mv,需要合并的图像数组,其中每个图像必须拥有相同的尺寸和数据类型
  • 参数二:dst,合并后输出的图像,与mv[0]具有相同的尺寸和数据类型,通道数等于所有输入图像的通道数总和

操作

private fun showBGZero() {
    val zero = Mat.zeros(mBgr.rows(), mBgr.cols(), CvType.CV_8UC1)
    val list = mutableListOf<Mat>()
    list.add(zero)
    list.add(zero)
    list.add(mChannelR)
    val result = Mat()
    Core.merge(list, result)
    showMat(result)
    result.release()
}

private fun showBRZero() {
    val zero = Mat.zeros(mBgr.rows(), mBgr.cols(), CvType.CV_8UC1)
    val list = mutableListOf<Mat>()
    list.add(zero)
    list.add(mChannelG)
    list.add(zero)
    val result = Mat()
    Core.merge(list, result)
    showMat(result)
    result.release()
}

private fun showGRZero() {
    val zero = Mat.zeros(mBgr.rows(), mBgr.cols(), CvType.CV_8UC1)
    val list = mutableListOf<Mat>()
    list.add(mChannelB)
    list.add(zero)
    list.add(zero)
    val result = Mat()
    Core.merge(list, result)
    showMat(result)
    result.release()
}

private fun showSwitchBR() {
    val zero = Mat.zeros(mBgr.rows(), mBgr.cols(), CvType.CV_8UC1)
    val list = mutableListOf<Mat>()
    list.add(mChannelR)
    list.add(mChannelG)
    list.add(mChannelB)
    val result = Mat()
    Core.merge(list, result)
    showMat(result)
    result.release()
}

结果

源码

https://github.com/onlyloveyd/LearningAndroidOpenCV

onlyloveyd CSDN认证博客专家 Android Kotlin OpenCV
个人公众号【OpenCV or Android】,热爱Android、Kotlin、Flutter和OpenCV。毕业于华中科技大学计算机专业,曾就职于华为武汉研究所。目前在三线小城市生活,专注Android、OpenCV、Kotlin、Flutter等有趣的技术。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__ 返回首页