静态图像中的颜色检测-OpenCV Android [英] Color detection in a static image - OpenCV Android
问题描述
我有一个红色,绿色,蓝色和黄色四个正方形的图像.我需要获取每个正方形的rgb值.我可以获取整个图像的rgb,但我希望将其用于特定的部分.
I have an image with four squares red, green, blue and yellow. I need to get the rgb values of each squares. I'm able to get the rgb of the whole image, but i want it for a specific section.
- 我要获取的图像将来自相机并存储到SD卡上
推荐答案
我不知道我是否完全了解你,但是来了.
I don't know if I understand you exactly, but here it comes.
您需要创建BufferedImage对象以获得RGB值:
You need to create BufferedImage object to get RGB value:
File f = new File(yourFilePath);
BufferedImage img = ImageIO.read(f);
从那时起,您可以从图像中获取RGB颜色值.您有4个正方形;要检查其RGB值,可以检查角点像素的RGB值:
You can get RGB Color values from the image from then. You have 4 squares; to check their RGB values, you can check the corner pixels' RGB values:
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));
此后,很容易分别获得红色,绿色和蓝色值:
After that it's easy to get red, green and blue values individually:
int red = leftTop.getRed();
int green = leftTop.getGreen();
int blue = leftTop.getBlue();
真的很抱歉,我没有看到它适用于Android.如您所说,Android没有ImageIO类.要在Android中完成任务,请首先初始化图片:
I'm really sorry, I didn't see it's for Android. As you said, Android doesn't have ImageIO class. To accomplish the task in Android, first initialize the image:
Bitmap img = BitmapFactory.decodeFile(yourFilePath);
从那时起,操作几乎是相同的:
From then the operation is pretty much the same:
int leftTop = img.getPixel(0, 0);
...
int red = Color.red(pixel);
int blue = Color.blue(pixel);
int green = Color.green(pixel);
这篇关于静态图像中的颜色检测-OpenCV Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!