I am trying to get the different amount of colors inside an image in java, but I don't know if there is a library for this propose of not. the project is about finding out the different colors from one image, and then print out the name of the colors. any idea??? please help me if you have any answer.
You can turn an image into a BufferedImage and call getRGB(int x, int y) to get the rgb for each pixel. Then you can use one of the many color websites such as this one or this one to map the rgb to a color name. Just find the named color which is the closest in distance to each rgb in the image.
Java has the Java Advanced Imaging (JAI) library which is built to allow stuff like this.
Related
I wanna make the same(https://play.google.com/store/apps/details?id=appinventor.ai_yuanryan_chen.BT_LED) app. I want to get a color of some pixel like here(https://i.stack.imgur.com/ixz84.png). How can I get the color(rgb)?
You don't need to do it from scratch since there is a set of readily-made libraries for colour picking, for example, this library:
https://github.com/jbruchanov/AndroidColorPicker
for more libraries you can check this:
https://android-arsenal.com/tag/18?sort=created
Right now I have a composite code that produces and image from recorded data. I am trying to figure out a way that I can fill the spots in the image where no data was recorded (aka where it reads 0.0) with a new color. I have been experimenting a little with Graphics, but am not finding a way that I can just will these empty spots. I would post an image if I had enough points...
But I hope you understand what I am trying to say.
Like the comment suggests we really need more info.
If you use a BufferedImage then you can simply set a single pixel color using this method setRGB(int x, int y, int rgb)
I am trying to identify and differentiate the colours of LEDs against a black background using OpenCV on an Android phone, but am currently struggling. My investigations to date point to two separate issues:
Using OpenCV, the camera appears to default to automatic white balance which makes it difficult to differentiate between certain colours. Using the native Android camera the best images appear to be produced with a white balance set to "Cloudy".
OpenCV provides images in the RGB colour space, yet RGB does not match the human-perceived distance between colours meaning a Euclidean RGB distance metric is not an optimum solution (see How to compare two colors).
Consequently I have three questions:
Is there a way in Android Java or OpenCV to set the camera's white balance so that it influences the resultant image returned by OpenCV?
If not, is there an algorithm available (preferably in Java) to modify the white balance of the OpenCV image?
Is there an algorithm available (again, preferably in Java) to convert RGB colours to an alternative colour space that would better match the human-perceived distance between colours?
Thanks
My answer to question 1 (meaning I didn't require an answer to question 2) was to use Using the OpenCV Tutorial 3 code (Camera Control) (see OpenCV4Android samples) and modify the colour effects methods to allow the setting of white balance:
public List<String> getWhiteBalanceList() {
return mCamera.getParameters().getSupportedWhiteBalance();
}
public boolean isWhiteBalanceSupported() {
return (mCamera.getParameters().getWhiteBalance() != null);
}
public String getWhiteBalance() {
return mCamera.getParameters().getWhiteBalance();
}
public void setWhiteBalance(String whiteBalance) {
Camera.Parameters params = mCamera.getParameters();
params.setWhiteBalance(whiteBalance);
mCamera.setParameters(params);
}
Once I knew that worked I was able to add a call to my main thread to set the correct white balance:
mOpenCvCameraView.setWhiteBalance(Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT);
In answer to question 3, I used the Java answer in Color Logic Algorithm.
I think that's related to the device driver of the camera whether you can control it or not through Android's Camera API.
This is useful for you: Detecting colors
I like electronics, so a simple Light dependant resistor would do that without any code :).
To identify different LED colors from black background, I propose the following:
First, Using OpenCV's cvtColor function convert your RGB into HSV color space. Here HSV stands for Hue(such as red,green,yellow, blue etc.), Saturation(how saturated the color is), Value (lightness). After conversion for each RGB value you will have HSV value. Like RGB HSV has three channels H, S and V.
Second, since you are interested in the LED's color, please analyze only the hue component to decide LED's Color. For example, you can threshold the Hue channel and decide which part of the image contains hue from lets say 10 to 25 and so on ... Plot/print the thresholded image to check your code.
So I am doing a group project for my programming class, we are makeing a photo editing program and one of my parts of the program is taking the image and turning it into black and white using rgb. I was wondering what would be the best value or way in RGB to achieve black and white?
I would recommend letting the Java 2D library worry about the conversion:
create a greyscale BufferedImage (BufferedImage.TYPE_BYTE_GRAY);
get a graphics context by createGraphics()
ensure that colour rendering is accurate on that graphics context: call setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY)
draw the colour image you want to "convert" to the graphics context
If you do the conversion "manually" and you want to do it as accurately as possible, then you need to take into account that the eye is more sensitive to certain colour components than others. (If you want a "rough and ready" conversion, you can average the colour components, but this isn't strictly speaking the most accurate conversion.)
For each pixel, you can convert the RGB to HSB (using Color.RGBtoHSB), set the saturation to 0, and convert back to a Color instance using Color.getHSBColor.
Wikipedia actually has a good piece about how to perform the transformation once you have the RGB colors.
Hey , this page has many Java Image filters which are freely available for download.All filters are standard Java BufferedImageOps and can be plugged directly into existing programs.The GrayscaleFilter can convert the image to black and white
Using java.awt.image.ColorConvertOp with a gray destination ColorSpace is very efficient. There's an excellent example here.
I have a folder with many images with different backgrounds. I have got requirement to sort these images on the basis of background color.
Can I make a java program to read the folder and each image file in there, and decide the image of each file? please share options.
Yes, it is possible. You can load images with ImageIO.
BufferedImage img = ImageIO.read(imageFile);
int rgb = img.getRGB(x,y);
Color color = new Color(rgb);
But you have to create an algorithm that finds out which color is the backround color. It depends on the kind of images.
So, not knowing what your images really look like, you may want to average as much of the background as you can to come up with a good representation of the background color.
I would consider a couple things:
* Read in the pixels of each of the four edges. If there's little variance in the pixel color, then you may be done, just take the average.
* Do the same, but also read in lines from the edge to the middle until you hit a pixel that has a rather different color than your running average. Do this for all edges.
Those would be the cheapest things that I can think of to cover variances in background color. Depending on the images you're working with, you may have to get fancier.
A BufferedImage should get you your image data.
Mark