Convert Mat to byte[] - .net core - arrays

I'm trying to use EmgCV with .netCore and I want to post my image to an server.
How can I convert my Image (Mat or Image<Bgr, byte>) to byte[].
I cant convert it to Bitmap first, since im using .netcore and not framework. That's the way how I convert my byte[] to a Mat:
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
Mat image2 = new Mat(4000, 6000, DepthType.Cv8U, 3);
CvInvoke.Imdecode(bytes, ImreadModes.AnyColor, image2);
Can someone help me to do it the other way round? I tried using this, but this doesn't work:
Image<Bgr, byte> afterImage = diff.ToImage<Bgr, byte>();
byte[] afterbytes = afterImage.Bytes;
Thanks in advance!

I think that the bytes you receive are not raw bitmap bytes. Instead you receive a jpeg or png encoded image. Thats why you have to use Imdecode.
EmguCV also offers method Imencode to encode images into various formats.
Example below encodes image2 Mat as jpeg bytes. Can also be png or something else when supported by EmguCV.
var buffer = new VectorOfByte();
CvInvoke.Imencode(".jpg", image2, buffer); //Must use .jpg not jpg
byte[] jpgBytes = buffer.ToArray();
Instead of Mat you can also pass Image<TColor,TDepth>

I found a solution by myself:
Image<Bgr, byte> afterImage = afterImageMat.ToImage<Bgr, byte>();
byte[] afterbytes = afterImage.ToJpegData();

Related

SolrJ how to get data from binary field

I have data (small picture) in binary format in Solr 6, when using SolrJ library I got in the response reference like this [B#157dac6d but not the base64 string from Solr. Any idea how to extract the data back?
Example png:
"preview_db":"iVBORw0KGgoAAAA...ABJRU5ErkJggg"
SolrJ returns the solr.BinaryField as byte[] array (class [B).
So to get the binary as a base64 String again we need to cast the response value, encode the byte array and then create String.
byte[] bytes = (byte[]) filedValue;
byte[] encoded = Base64.encodeBase64(bytes);
String s = new String(endcoded);
System.out.println(s);

Convert bitmap to byte[]

I am developing a LightSwitch application that generates barcodes (QR images) for tickets. I am calling an encode function that converts text to bitmap.
I just need to save this in an LightSwitch Image field.
I have this:
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
EditableImage image = qrCodeEncoder.Encode(data);
I want this:
ticket.QRImage = .....???
I am using this library for the QR
http://www.jeff.wilcox.name/2009/09/quick-read-silverlight-barcodes/
http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library
You can get the bytes by calling image.GetStream(), and then using one of the standard methods to get the bytes out of the stream (see How to convert an Stream into a byte[] in C#?)

How to convert a byte[] to a BufferedImage in Java?

I'm posting this thread because I have some difficulties to deal with pictures in Java. I would like to be able to convert a picture into a byte[] array, and then to be able to do the reverse operation, so I can change the RGB of each pixel, then make a new picture. I want to use this solution because setRGB() and getRGB() of BufferedImage may be too slow for huge pictures (correct me if I'm wrong).
I read some posts here to obtain a byte[] array (such as here) so that each pixel is represented by 3 or 4 cells of the array containing the red, the green and the blue values (with the additional alpha value, when there are 4 cells), which is quite useful and easy to use for me. Here's the code I use to obtain this array (stored in a PixelArray class I've created) :
public PixelArray(BufferedImage image)
{
width = image.getWidth();
height = image.getHeight();
DataBuffer toArray = image.getRaster().getDataBuffer();
array = ((DataBufferByte) toArray).getData();
hasAlphaChannel = image.getAlphaRaster() != null;
}
My big trouble is that I haven't found any efficient method to convert this byte[] array to a new image, if I wanted to transform the picture (for example, remove the blue/green values and only keeping the red one). I tried those solutions :
1) Making a DataBuffer object, then make a SampleModel, to finally create a WritableRaster and then BufferedImage (with additional ColorModel and Hashtable objects). It didn't work because I apparently don't have all the information I need (I have no idea what's the Hashtable for BufferedImage() constructor).
2) Using a ByteArrayInputStream. This didn't work because the byte[] array expected with ByteArrayInputStream has nothing to do with mine : it represents each byte of the file, and not each component of each pixel (with 3-4 bytes for each pixel)...
Could someone help me?
Try this:
private BufferedImage createImageFromBytes(byte[] imageData) {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
try {
return ImageIO.read(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
I have tried the approaches mentioned here but for some reason neither of them worked. Using ByteArrayInputStream and ImageIO.read(...) returns null, whereas byte[] array = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); returns a copy of the image data, not a direct reference to them (see also here).
However, the following worked for me. Let's suppose that the dimensions and the type of the image data are known. Let also byte[] srcbuf be the buffer of the data to be converted into BufferedImage. Then,
Create a blank image, for example
img=new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Convert the data array to Raster and use setData to fill the image, i.e.
img.setData(Raster.createRaster(img.getSampleModel(), new DataBufferByte(srcbuf, srcbuf.length), new Point() ) );
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
byte[] array = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(pixelArray, 0, array, 0, array.length);
This method does tend to get out of sync when you try to use the Graphics object of the resulting image. If you need to draw on top of your image, construct a second image (which can be persistant, i.e. not constructed every time but re-used) and drawImage the first one onto it.
Several people upvoted the comment that the accepted answer is wrong.
If the accepted answer isn't working, it may be because Image.IO doesn't have support for the type of image you're trying, for example tiff images.
To make it work, you need to add an extra jar to handle the image type.
You can add jai-imageio-core-1.3.1.jar to your classpath with:
<!-- https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core -->
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.3.1</version>
</dependency>
To add support for:
wbmp
bmp
pcx
pnm
raw
tiff
gif (write)
You can check the list of supported formats with:
for(String format : ImageIO.getReaderFormatNames())
System.out.println(format);
Note that you only have to drop the jar (jai-imageio-core-1.3.1.jar for example) into your classpath to make it work.
Other projects that add additional support for image types include:
https://github.com/haraldk/TwelveMonkeys
https://github.com/geosolutions-it/imageio-ext
The approach by using ImageIO.read directly is not right in some cases. In my case, the raw byte[] doesn't contain any information about the width and height and format of the image. By only using ImageIO.read, It is impossible for the program to construct a valid image.
It is necessary to pass the basic information of the image to BufferedImage object:
BufferedImage outBufImg = new BufferedImage(width, height, bufferedImage.TYPE_3BYTE_BGR);
Then set the data for the BufferedImage object by using setRGB or setData. (When using setRGB, it seems we must convert byte[] to int[] first. As a result, it may cause performance issues if the source image data is big. Maybe setData is a better idea for big byte[] typed source data.)

WPF BitmapImage and TIFF with CMYK + Alpha

I am using this code snippet to load various image files:
BitmapImage bitmap = new BitmapImage ();
bitmap.BeginInit ();
bitmap.UriSource = new System.Uri (path);
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit ();
This works fine for TIFF files stored as RGB, RGB+Alpha and CMYK. However, if I try to load a TIFF file using CMYK colors and an alpha channel, I get an exception (the file format is not recognized as being valid by the decoder).
I was previously using the FreeImage library and a thin C# wrapper on top of it. FreeImage 3.x has partial support for this kind of image format, i.e. I had to load the TIFF twice, once as CMYK without transparency and once as RGB+Alpha; this trick is needed since FreeImage only gives access to at most 4 simultaneous color channels.
I'd like to know if there is a supported way to load CMYK+Alpha bitmaps? Either directly in C# or by going through some interop code, but preferably without having to use a third-party DLL (other than the .NET 4 framework libraries).
An example of such a TIFF file can be found here.
EDIT : I can no longer reproduce the problem, the following code works just fine:
BitmapImage bitmap = new BitmapImage ();
bitmap.BeginInit ();
bitmap.UriSource = new System.Uri (path);
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit ();
byte[] pixels = new byte[bitmap.PixelHeight*bitmap.PixelWidth*5];
bitmap.CopyPixels (pixels, bitmap.PixelWidth * 5, 0);
But I am still stuck: how can I find out that the source image was encoded as CMYK plus Alpha channel? When looking at the Format property, I get only the information that the image has 40 bits per pixel. All the interesting stuff is stored in the following non-public properties:
bitmap.Format.FormatFlags == IsCMYK | NChannelAlpha;
bitmap.Format.HasAlpha == true;
Is there any official way of getting to them, without resorting to reflection?
I can only say this because I've had the issues with some files: it might be a better way to convert the tiff to png24 first and then load it up.
Even Photoshop puts up a warning if a user tries to save a CMYK Tiff file and ticks 'Transparency': "Many programs do not support transparency in TIFF. Save transparency information?"
So converting prior to opening might be the safe way to go.
Maybe http://msdn.microsoft.com/en-us/library/system.drawing.imageconverter.aspx would do it but I doubt it, you probably need some extra piping.
HTH.
It's only a guess, but GDI+ might be able to load such files.
System.Drawing.Image etc.
There is an interop class which can render GDI+ images in WPF.
I found another question on SO which linked to this library:
http://freeimage.sourceforge.net/
Good Tiff library for .NET
I hope this might help.

Byte Array copy in Jsp

I am trying to append 2 images (as byte[] ) in GoogleAppEngine Java and then ask HttpResponseServlet to display it.
However, it does not seem like the second image is being appended.
Is there anything wrong with the snippet below?
...
resp.setContentType("image/jpeg");
byte[] allimages = new byte[1000000]; //1000kB in size
int destPos = 0;
for(Blob savedChart : savedCharts) {
byte[] imageData = savedChart.getBytes(); //imageData is 150k in size
System.arraycopy(imageData, 0, allimages, destPos, imageData.length);
destPos += imageData.length;
}
resp.getOutputStream().write(allimages);
return;
Regards
I would expect the browser/client to issue 2 separate requests for these images, and the servlet would supply each in turn.
You can't just concatenate images together (like most other data structures). What about headers etc.? At the moment you're providing 2 jpegs butted aainst one another and a browser won't handle that at all.
If you really require 2 images together, you're going to need some image processing library to do this for you (or perhaps, as noted, AWT). Check out the ImageIO library.
Seem that you have completely wrong concept about image file format and how they works in HTML.
In short, the arrays are copied very well without problem. But it is not the way how image works.
You will need to do AWT to combine images in Java

Resources