An image contains certain image java - java

I have two buffered images and I want to check whether there is a part in Image 1 that is exactly the same as Image 2.
BufferedImage image1 = ......;
BufferedImage image2 = ......;
if(image1.contains(image2)) //I'm looking for method like contains
{
//do some work.
}
Thanks in advance.

JavaCV is a good choice for image manipuation in Java. JavaCV is an OpenCV wrapper that contains tons of methods for image processing.

Related

How can I know if the image is in RGB or BGR format?

Is there any way to know in advance if an image used as an input to a system is in RGB or BGR format?
I am using OpenCV with java API and I would like to convert an input image into grayscale or L*a*b* color space, but in OpenCV you have to specify first whether the image you want to convert is in RGB or BGR.
The type of the image I am using is either .jpg or .png.
If you are reading in the image file, or you have access to the code that reads in the file, know it is:
BGR order if you used cv2.imread()
RGB order if you used mpimg.imread() (assuming import matplotlib.image as mpimg)
If you don't know how the file was opened, the accepted answer BufferedImage is great for Java.
I would like to find a way to do the same in Python!
When you use opencv (imread, VideoCapture), the images are loaded in the BGR color space.
If your image is a BufferedImage then you can ask for his type with getType(), and test against the several constants (see: BufferedImage).

Various types of images in Java

When attemting to make a simple application which uses an image I was amazed at the large number of image types available to me. I did a search on them and whilst finding a little about them cannot work out what image is ment to be used in what circumstances.
The types of image I am confused about are Image, ImageIcon, and ImageIO.
The only one of those that is an image is Image. A more powerful sub-class of Image is BufferedImage.
An ImageIcon is an image based Icon.
ImageIO simply helps us to read and write images, and do other things with images.
Be sure to check the JavaDocs for each. It adds more details.
Image | BufferedImage | ImageIcon | ImageIO

Steps for Instantiating a BufferedImage

I've been researching the BufferedImage class to use for game programming purposes (so that I can make image maps for levels and use getRGB() to identify the tile in each pixel), but I'm very confused about how to actually make a BufferedImage. What's got me particularly confused is how you can't make a BufferedImage using an Image as a parameter. Step by step, how does one instantiate a BufferedImage for a simple image file (e.g. a .jpg)?
Simple steps - ImageIO.read(File file) as shown in the documentation
You can load an image with ImageIO.read(new File("example.jpg"));

How to work with BufferedImage and YCbCr colorspace?

I need to translate colors in bitmap loaded to BufferedImage from RGB to YCbCr (luminance and 2 channels chrominance) and back after process.
I made it with functions used like rgb2ycbcr() in main method for each pixel, but it isn't so smart solution. I should use ColorSpace and ColorModel classes to get BufferedImage with correct color space. It would be more flexible method, but I don't know how to do that.
I'm lost and I need some tips. Can somebody help me?
As I understood your question, you want to do the following:
Load RGB image -> process YCbCr image -> Use RGB image again
And you want us to help you, to make this process as seamless as possible. First and foremost you want us to give you a simple way to avoid the -> (converting) parts.
Well I looked into the BufferedImage documentation. It seems, as if there doesn't exist a way to change the ColorSpace of an once created BufferedImage.
You could create a new BufferedImage with an YCbCr color space for that you can use the predefined ICC_ColorSpace. Then you copy the data from your old image possibly via ColorSpace.fromRGB to the YCbCr color space, do the image processing and then convert again via ColorSpace.toRGB. This method requires you to fully convert the image before and after processing via existing methods. Furthermore you have to know, how ICC_ColorSpace converts your image to YCbCr color space. Otherwise you can't know, which array indices corresponds to the same pixel.
If you just want to create a wrapper around the RGB-BufferedImage that lets you manipulate this image, as if it was an YCbCr image, that isn't possible with BufferedImage.
EDIT:
To convert the color space of a BufferedImage use ColorConvertOp. The code would look something like this:
ColorConvertOp cco = new ColorConvertOp(new YCbCrColorSpace(), null);
BufferedImage ycbcrImage = cco.filter( oldRGBImage, null );
This requires you to either write your own ColorSpace class or you could download and use the classes mentioned here. If you just want to load a JPEG image you should use the predefined classes.

java pgm 2 jpeg/png

I have an int array containing gray scale values from 0-254, i also have the x and y size of the image. It is an easy thing to create an pgm image, but i want to display it in a jsp, so i need somehow to convert it to a jpeg or png image.
If you suggest jai, than please tell me at which classes to look, or how to actually do it in jai.
Thanks a lot, in advance.
Maybe skip the PGM entirely?
int[] myImage = getGreyscaleIntArray();
BufferedImage im = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = im.getRaster();
for(int h=0;h<height;h++)
{
for(int w=0;w<width;w++)
{
raster.setSample(w,h,0, myImage[h * width + w]);
}
}
ByteArrayOutputStream myJpg = new ByteArrayOutputStream();
javax.imageio.ImageIO.write(im, "jpg", myJpg);
uses the JAI ImageIO api, specifically the ImageIO utility class
WriteableRaster sample from the Java Image Processing cookbook
ImageMagick works well for converting images and Jmagick provides an interface to call directly from java programs.

Categories