openCV中棕色的HSV值范围是多少? [英] What is the range of HSV values for BROWN color in openCV?

查看:2225
本文介绍了openCV中棕色的HSV值范围是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是openCV的新手.我已经能够计算带有遮罩的图像的绿色和黄色像素.我想知道openCV中Brown的HSV范围是什么.

    private void testBrownPixelCount(Mat originalMat) {
    if(isMatEmpty(originalMat)){
        Log.i(TAG, "Empty Original Mat at testBrownPixCoutn()");
    }else{
        //Perform:
        Mat maskMat = Imgcodecs.imread(testFilePath);
        Mat bgr = new Mat();
        Imgproc.cvtColor(maskMat, bgr, Imgproc.COLOR_BGR2RGB);
        Mat maskMatHsv = new Mat();
        Imgproc.cvtColor(bgr, maskMatHsv , Imgproc.COLOR_RGB2HSV);

        //iF NOt; use RGB2HSV:
        Mat brownMat = new Mat();
        Scalar min_brown = new Scalar(20,100,100);
        Scalar max_brown = new Scalar(30,255,255);
        Core.inRange(maskMatHsv, min_brown, max_brown, brownMat);
        Log.i(TAG, "Brown Mat Non-zeros:" + Core.countNonZero(brownMat));

        Bitmap bitmapMat = Bitmap.createBitmap(brownMat.cols(), brownMat.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(brownMat, bitmapMat);
        imgView_testView.setImageBitmap(bitmapMat);

    }

导致imageView为黑色的结果,甚至无法检测到黄色.

解决方案

您可以查看我的答案在python和OpenCV中构建了一个工具,类似于photoshop中的魔术棒选择器,您定义一个阈值,单击一个像素,它将选择与您单击的位置相关的相似颜色的像素.该工具将打印出最高和最低的颜色值,还将打印平均颜色和标准偏差,以便更好地控制与您选择的颜色相似的阈值.您可以在此处看到它的一些gif.

另一种使用值的方法是使用OpenCV创建轨迹栏,提供所有最小和最大阈值,并在每次更改值时使用阈值图像更新屏幕.我也为此在python和OpenCV中构建了工具.您可以在此处看到它的一部分.

如果仔细阅读这两个项目的代码,您将了解如何构建类似的程序来使用值作为玩具,以便在不同的色彩空间中更舒适地工作.

I am new to openCV. I am already able to count green and yellow pixels of an image with a mask. I would like to know what is the HSV range of Brown in openCV.

    private void testBrownPixelCount(Mat originalMat) {
    if(isMatEmpty(originalMat)){
        Log.i(TAG, "Empty Original Mat at testBrownPixCoutn()");
    }else{
        //Perform:
        Mat maskMat = Imgcodecs.imread(testFilePath);
        Mat bgr = new Mat();
        Imgproc.cvtColor(maskMat, bgr, Imgproc.COLOR_BGR2RGB);
        Mat maskMatHsv = new Mat();
        Imgproc.cvtColor(bgr, maskMatHsv , Imgproc.COLOR_RGB2HSV);

        //iF NOt; use RGB2HSV:
        Mat brownMat = new Mat();
        Scalar min_brown = new Scalar(20,100,100);
        Scalar max_brown = new Scalar(30,255,255);
        Core.inRange(maskMatHsv, min_brown, max_brown, brownMat);
        Log.i(TAG, "Brown Mat Non-zeros:" + Core.countNonZero(brownMat));

        Bitmap bitmapMat = Bitmap.createBitmap(brownMat.cols(), brownMat.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(brownMat, bitmapMat);
        imgView_testView.setImageBitmap(bitmapMat);

    }

Which results to black imageView, it can't even detect the yellow ones.

解决方案

You can check out my answer here which goes in-depth on how to plot/view a range of different HSV values.

You can also check out external sites which let you play around with values to get an idea of the colorspace. In HSV, browns correspond to higher saturation level (lower values become more gray) and middle value levels (low is black, high is white), and the hue is between (roughly) 20 degrees for a reddish brick color to 40 degrees for a sand color. But in OpenCV, the hue degrees are divided by two to get them to fit under 255, so thats more like 10 to 20 for the hue values.

Using the code in the linked answer, I generated this gif of a range of brown values from [10, 100, 20] to [20, 255, 200] in HSV:

This seems to encompass most ideas of brown, but you can play with the values if you want some darker, lighter, grayer, etc ones to appear.

Another way to play around with finding good thresholding values is to create a program that lets you try it out. For example, I built a tool in python and OpenCV similar to the magic wand selector in photoshop, which lets you define a threshold, click a pixel, and it will select pixels of a similar color connected to where you clicked. This tool will print out the highest and lowest color values, and also will print out the mean color and standard deviation for better control of thresholding values similar to the color you selected. You can see a little gif of it being used here.

One more way to play around with values is to create trackbars with OpenCV giving all the minimum and maximum thresholding values, and having the screen update with the thresholded image every time you change a value. I built a tool for this as well in python and OpenCV. You can see a little gif of it being used here.

If you peruse the code for both those projects, you can get an idea of how to build similar programs to toy around with values to get more comfortable working in different colorspaces.

这篇关于openCV中棕色的HSV值范围是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆