静态图像中的颜色检测-OpenCV Android
我有一个四个正方形的图像,红色、绿色、蓝色和黄色。我需要得到每个方块的rgb值。我可以得到整个图像的rgb,但我想要一个特定的部分静态图像中的颜色检测-OpenCV Android,android,opencv,Android,Opencv,我有一个四个正方形的图像,红色、绿色、蓝色和黄色。我需要得到每个方块的rgb值。我可以得到整个图像的rgb,但我想要一个特定的部分 我将要获得的图像将来自相机并存储在SD卡上 我不知道我是否完全理解你,但它来了 您需要创建BuffereImage对象以获取RGB值: File f = new File(yourFilePath); BufferedImage img = ImageIO.read(f); 从那时起,您可以从图像中获取RGB颜色值。你有4个正方形;要检查其RGB值,可以检查角点
- 我将要获得的图像将来自相机并存储在SD卡上
- 我不知道我是否完全理解你,但它来了
您需要创建BuffereImage对象以获取RGB值:
File f = new File(yourFilePath);
BufferedImage img = ImageIO.read(f);
从那时起,您可以从图像中获取RGB颜色值。你有4个正方形;要检查其RGB值,可以检查角点像素的RGB值:
Color leftTop = new Color(img.getRGB(0, 0));
Color rightTop = new Color(img.getRGB(img.getWidth - 1, 0));
Color leftBottom = new Color(img.getRGB(0, img.getHeight - 1));
Color rightBottom = new Color(img.getRGB(img.getWidth - 1, img.getHeight - 1));
之后,很容易分别获得红色、绿色和蓝色值:
int red = leftTop.getRed();
int green = leftTop.getGreen();
int blue = leftTop.getBlue();
编辑:
我真的很抱歉,我没有看到它是Android的。正如你所说,Android没有ImageIO类。要在Android中完成任务,请首先初始化图像:
Bitmap img = BitmapFactory.decodeFile(yourFilePath);
从那时起,操作基本相同:
int leftTop = img.getPixel(0, 0);
...
int red = Color.red(pixel);
int blue = Color.blue(pixel);
int green = Color.green(pixel);
用于裁剪图像
现在,要检测图像的颜色,请从正方形中提取一个像素,然后使用检测工具检测其颜色
在找到RGB值后,使用一个简单的条件语句查看正方形是红蓝还是绿。我是这样得到的
int topLeftIndex = squareImage.getPixel(0, 0);
int R1 = (topLeftIndex >> 16) & 0xff;
int G1 = (topLeftIndex >> 8) & 0xff;
int B1 = topLeftIndex & 0xff;
和我一样
int bottomLeftIndex=squareImage.getPixel(0, picHeight - 1);
int topRightIndex=squareImage.getPixel(picWidth -1 , 0);
int bottomRightIndex=squareImage.getPixel(picWidth -1, picHieght - 1);
你试过裁剪特定区域并检测颜色吗?你需要在图像中显示区域并显示它们的rgb值,对吗?@IsmetAlkan我只想获得所有四个区域的rgb,而不是*显示它anywhere@Torcellite没有裁剪,但我尝试提供只包含R、G和B的图像。。。我为他们找到了合适的RGB。。基本上我得到的是图像的平均rgb颜色。@aditya正方形是否总是在相同的位置?我的意思是方块的颜色可以改变,但它们各自的位置不会改变,如果是这样,你最好的选择就是裁剪和检测。谢谢你的支持。。。我正在尝试这个。。我会尽快回复你的..Android SDK不支持BuffereImage和ImageIO。。。现在我正在寻找位图类是否可以为我做到这一点。你完全改变了我的方法。。。我得到的顶部和底部角落像素rgb。。。让我们看看用这种方法是否能解决问题。。I’’我现在不接受答案,但你肯定会得到+1。。谢谢你的帮助伙计…谢谢你的帮助。。。但是我不需要做这些。。再次感谢+谢谢你的帮助。看看我的答案。。。其中,我得到了四个示例角点像素及其RGB:)