camera preview gives gray images - java

i am developing android app using android sdk camera. on camera preview i need the frame content so i am using PreviewCallback to return the data in byte array, now my problem is saving the data in mat object the mat return gray images:
public void onPreviewFrame(byte[] data, Camera camera) {
Mat src = new Mat(previewSize.height, previewSize.width, CvType.CV_8U, new Scalar(255));
src.put(0, 0, data);
Highgui.imwrite("/mnt/sdcard/1.jpg", src);
}
anybody can help me to generate argb images
note: i am using NV21 in preview image format.

Do this instead:
Mat src = new Mat(previewSize.height, previewSize.width, CvType.CV_8UC3);
If it does not work, it means your data is already gray, so you must have set it as gray somewhere in your code.

Related

Java OpenCV convert HSV back to BGR after inRange

I'd like to create a black&white image relying on HSV filtering. However, after converting the image from BGR to HSV and applying the inRange() method, the matrix is reduced to a single channel matrix (with values either 0 or 255) and cannot be converted back to BGR.
Is there an easy way to work around this? Do I even need that step of back-conversion or can I somehow display the new image with the information I have? I'm pretty new to OpenCV and already found a very similar question but I'm still kinda confused on what to do.
Example:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat img = Imgcodecs.imread(path);
Mat hsv = new Mat();
Mat img_new = new Mat();
Imgproc.cvtColor(img,hsv,Imgproc.COLOR_BGR2HSV);
Core.inRange(hsv, new Scalar(hue,saturation,value),new Scalar(hue,saturation,value),hsv);
Imgproc.cvtColor(hsv, img_new, Imgproc.COLOR_HSV2BGR); // This line doesn't work.
// display new image in JFrame
MatOfByte mob = new MatOfByte();
Imgcodecs.imencode(".tif", img_new, mob);
byte ba[] = mob.toArray();
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(ba));
newImgLabel.setIcon(new ImageIcon (bi));
Thank you in advance!
inRange function gives you a mask(actually, a single channel image with values 0 and 255), you can use it to select which areas you want to select.
Mat mask = new Mat();
Imgproc.cvtColor(img,hsv,Imgproc.COLOR_BGR2HSV);
Core.inRange(hsv, new Scalar(hue,saturation,value),new Scalar(hue,saturation,value),mask);
img.copyTo(img_new, mask);

Convert OpenCV mat to Android Bitmap

I am trying to convert an OpenCV mat to android Bitmap but that gives me images with a bluish tint(yellow colour turns to blue)! Even though I am not doing any processing on the image! I have no clue why this is happening. Below is some relevant code:
File file = new File(imgDecodableString);
image = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_COLOR);
resultBitmap = Bitmap.createBitmap(image.cols(), image.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(image, resultBitmap);
Bitmap mResult = resultBitmap;
ImageView imgView = (ImageView) findViewById(R.id.imgView);
imgView.setImageBitmap(mResult);
//imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
I am new to android app development so I might be missing something simple. Thanks for help!
EDIT:
I am uploading images for reference.
Based on the given suggestions, I made a function which converts Mat to Bitmap. This function works perfectly.!
private static Bitmap convertMatToBitMap(Mat input){
Bitmap bmp = null;
Mat rgb = new Mat();
Imgproc.cvtColor(input, rgb, Imgproc.COLOR_BGR2RGB);
try {
bmp = Bitmap.createBitmap(rgb.cols(), rgb.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgb, bmp);
}
catch (CvException e){
Log.d("Exception",e.getMessage());
}
return bmp;
}
As suspected, the issue is with the RGB color convention, Android follows RGB color convention, but OpenCV follows BGR color convention, You can rectify it using Imgproc.cvtColor(mat, Imgproc.COLOR_BGR2RGBA), before displaying it in the ImageView.

How to access pixel data from video from webcam in JavaCV

I am trying to access pixel data from video from webcam in JavaCV.
This is the code i used to display video in screen.
CvCapture capture =opencv_highgui.cvCreateCameraCapture(0);
opencv_highgui.cvSetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT, 720);
opencv_highgui.cvSetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FRAME_WIDTH, 720);
IplImage grabbedimage = opencv_highgui.cvQueryFrame(capture);
CanvasFrame frame = new CanvasFrame("Camera");
while(frame.isVisible() && (grabbedimage = opencv_highgui.cvQueryFrame(capture))!=null){
frame.showImage(grabbedimage);
}
Is it possible to covert IplImage to Buffered image. Can i access pixel data from buffered image.
Thank you
You have this little code here
IplImage originalImage = cvLoadImage(image);
BufferedImage bi=originalImage.getBufferedImage();

Reshaping an IplImage in javacv

I am trying to resize an IplImage image in javacv. I found the cvResize function that does such a thing. MY workflow is: to open the image to transform it to gray_scale to resize it to desirable size and finally to reshape it. I have already read it transform it to gray_scale and resize it. What is my problem the final step, reshaping. I have the cvReshape which does what I want. I ve got an IplImage, I have to transform it to cvMat. The next step is reshaping, which takes 4 arguments, a cvArr a cvMat and the desirable dimensions.
// read an image
final IplImage image = cvLoadImage("ef.jpg");
//create image window named "My Image"
final CanvasFrame canvas = new CanvasFrame("My Image");
// request closing of the application when the image window is closed
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
// show image on window
canvas.showImage(image);
IplImage GrayImage = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
cvCvtColor(image, GrayImage, CV_BGR2GRAY);
IplImage img = IplImage.create(60, 60, IPL_DEPTH_8U, 1);
//resize the image
cvResize(GrayImage, img, CV_INTER_LINEAR);
cvSaveImage("4-rjb" + ".jpg", img);
//cvReleaseImage(result1);
System.out.println("The size of the image: "+img);
CvMat mtx = CvMat.createHeader(img.height(), img.width());
cvGetMat(img, mtx, null, 0);
System.out.println(mtx);
cvReshape(img, mtx, 3600, 1);
I am receiving the error:
OpenCV Error: Bad number of channels () in unknown function, file .\src\array.cpp,
line 2721
I ve just want to reshape a 2d image, why that bad number of channels error happened??
With some Googling I found that your output matrix must contain at least 3 channels. (Blue, Green and Red). Where Blue and Green will be completely empty and you put your grayscale image as Red channel of the output image. Any other number of channels will cause the error you are getting.

How to convert Ipl image to Mat image in java

I am trying to make a mood detection app in opencv, java. But there is a lag in processing the image and then displaying the emoticon. So I want to directly use the image captured , instead of copying the image to the hard disk. For that I need convert iplimage to matimage .
Try
IplImage *ipl_img;
Mat mat_img(ipl_img);
Try this
IplImage img;
bmp = Bitmap.createBitmap(img.width(), img.height(), Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(img.getByteBuffer());
Mat mROI = new Mat(new Size(img.width(), img.height()), CV_8UC4);
Utils.bitmapToMat(bmp, mROI);

Categories

Resources