如何在OpenCV C ++中使图像黑白化?

时间:2018-11-06 15:59:25

标签: android c++ opencv java-native-interface

实际上,我是opencvnative C++概念的完整入门者,因此,如果我的问题如此简单,请原谅。基本上,我想使用Black&White将图像转换为native C++,因为我试图查找教程,但找不到任何内容。 因此有人可以帮助我学习这一概念吗?

2 个答案:

答案 0 :(得分:2)

cv::Mat colorMat, grayscaleMat, binaryMat;

首先转换为灰度:

cv::cvtColor(colorMat, grayscaleMat, CV_BGR2GRAY);

然后将二进制阈值应用于灰度图像:

cv::threshold(grayscaleMat, binaryMat, 100, 255, cv::THRESH_BINARY);

此外,您可能需要考虑使用OpenCV4Android SDK

答案 1 :(得分:1)

您应该使用cvtColor函数。 假设“图像”是您的原始照片,“灰色图像”是用于存储新的灰色照片的变量:

package com.iraqairoirt.iraqairports

import android.os.AsyncTask
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import kotlinx.android.synthetic.*
import kotlinx.android.synthetic.main.fragment_dep.*
import org.json.JSONArray
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL

class fragment_Dep :Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_dep,container,false)

        val url = "xxxxxxxxxxxxxxxxxxx"
        Download().execute(url)


    }

    //    full class for json api
    inner class Download : AsyncTask<String, String, String>(){

        //        for build connection
        override fun doInBackground(vararg url: String?): String{

            var text : String
            val connection = URL(url[0]).openConnection() as HttpURLConnection

            try {
                connection.connect()
                text = connection.inputStream.use { it.reader().use{reader -> reader.readText()} }


            } finally{

                connection.disconnect()

            }
            return text
        }

        override fun onPostExecute(result: String?) {

            super.onPostExecute(result)
            handleJson(result)

        }

        private fun handleJson (jsonString: String?){

            val jsonObj = JSONObject(jsonString)
            val result = jsonObj.getJSONObject("result")
            val response = result.getJSONObject("response")
            val airport = response.getJSONObject("airport")
            val pluginData = airport.getJSONObject("pluginData")
            val schedule = pluginData.getJSONObject("schedule")
            val arrivals = schedule.getJSONObject("arrivals")
//        val data = arrivals.getJSONObject("data")
            val jsonArray = JSONArray(arrivals.get("data").toString())

            val list =  ArrayList<FlightShdu>()
            var x = 0
            while (x < jsonArray.length()){

                val jsonObject = jsonArray.getJSONObject(x)



                list.add(FlightShdu(

                    jsonObject.getJSONObject("flight").getJSONObject("identification").getString("callsign"),
                    jsonObject.getJSONObject("flight").getJSONObject("identification").getJSONObject("number").getString("default")

                ))


                x++
            }

            val adapter = ListAdapte(this@fragment_Dep,list)
            flightShdu_list.adapter = adapter

        }

        //    for get items from json api
        override fun onProgressUpdate(vararg values: String?) {

        }


    }

}

(“图像”和“灰色图像”是“垫子”类型