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()
}

結果

源碼

github.com/onlyloveyd/…

以上就是Android基於opencv實現多通道分離與合並的詳細內容,更多關於Android opencv實現多通道分離與合並的資料請關註WalkonNet其它相關文章!

推薦閱讀: