Ask Your Question
0

i want to generate a gray-scale image from byte array,is it possible to do it with opencv ?

asked 2018-04-30 00:44:23 -0600

Ann162 gravatar image

I am using java to read an image and to generate its byte array on which few functions are performed ,now i want to obtain an image from this altered bytes.Is it possible in opencv.

edit retag flag offensive close merge delete

Comments

sure that is possible, but you have to tell us, where your byte array comes from, and what is in it.

berak gravatar imageberak ( 2018-04-30 00:57:05 -0600 )edit

I have taken a gray-scale image and have read it using java .then have taken 8 bits for each pixel of the image.further on each of this pixel i have performed various functions in the end i have again taken the decimal value of this pixels in attempt to regenerate the image.But so far i am unsuccessful,so now how can i read the pixel value obtained to again reconstruct the image of same height and width. i am getting something like this[43 25 29 13 0 12 48 60 21 29 4 31 27 3 35 54 17 49 12 1 22 24 ]

Ann162 gravatar imageAnn162 ( 2018-04-30 01:12:46 -0600 )edit

not enough information. width ? height ? channels ? how long is you array ?

berak gravatar imageberak ( 2018-04-30 01:34:09 -0600 )edit

The image that i am getting using your solution is a noisy image.The input image i am taking is lena.png 256*256 grayscale image.I am not changing the pixels just trying to regenerate it.

//1. Convert Image to byte code 
    ImgDemo im = new ImgDemo();
    String path = "C:\\Users\\domin\\Desktop\\Sop\\MyImgOut.png";
    byte[] pixels = im.ByteConvert(path);

//2. Convert byte to image Mat img1 = new Mat(256,256, CvType.CV_8UC1);

    img1.put(0, 0, pixels);
     String filename = "C:\\Users\\domin\\Desktop\\Sop\\dreams.png";
     Highgui.imwrite(filename, img1);
Ann162 gravatar imageAnn162 ( 2018-05-01 05:18:17 -0600 )edit

@Ann162 , and ? what are you trying to say ?

also, why not simply use imread() to read the image ?

berak gravatar imageberak ( 2018-05-01 05:26:35 -0600 )edit

imread doesnot return the byte array of the image,which is required for further processing.

Ann162 gravatar imageAnn162 ( 2018-05-01 23:52:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-04-30 01:24:18 -0600

berak gravatar image

updated 2018-04-30 01:41:03 -0600

you can construct a new Mat with your pixels like this:

Mat img = new Mat(height, width, CvType.CV_8UC1);
img.put(0, 0, byteArray);
edit flag offensive delete link more

Comments

Exception in thread "main" java.lang.UnsupportedOperationException: Provided data element number (35221) should be multiple of the Mat channels count (3) at org.opencv.core.Mat.put(Mat.java:2554) at ImgDemo.main(ImgDemo.java:120) I have tried this earlier but getting the above error

Ann162 gravatar imageAnn162 ( 2018-04-30 01:30:07 -0600 )edit

you obviously have tried with CV_8UC3, not CV_8UC1 before

berak gravatar imageberak ( 2018-04-30 01:33:23 -0600 )edit

ok sorry i didnt notice.Now its working thank you

Ann162 gravatar imageAnn162 ( 2018-04-30 01:38:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-30 00:44:23 -0600

Seen: 3,483 times

Last updated: Apr 30 '18