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);
Related
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);
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.
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();
Can anyone tell me that how can i detect keypoints of an image and draw that keypoints on that image in java?
I tried smt but i couldn't figure out how to draw them?
Any ideas for how should i proceed or any ideas for drawing for my code?
final IplImage image1 = cvLoadImage(
"C:/Users/Can/Desktop/panorama_image1.jpg",
CV_LOAD_IMAGE_GRAYSCALE);
final CanvasFrame canvas1 = new CanvasFrame("Image1");
canvas1.showImage(image1);
canvas1.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
SIFT sift = new SIFT();
KeyPoint keypoint1 = new KeyPoint();
sift.detect(image1, null, keypoint1);
System.out.println("Keypoints for image1: " + keypoint1.capacity());
Assuming you or anyone else still requires this, you can do the following.
Using Java, after you have computed your keypoints you can do the following using the Features2d class in OpenCV.
// draw keypoints on image
Mat outputImage = new Mat();
// Your image, keypoints, and output image
Features2d.drawKeypoints(image, keypoints, outputImage);
String filename = "keypoints.jpg";
System.out.println(String.format("Writing %s...", filename));
Highgui.imwrite(filename, outputImage);
If you or others still need an answer, I believe the possible way of doing that is
opencv_features2d.drawKeypoints(_image1, keypoint1, Mat.EMPTY);
Then you may save your _image1 to file using
ImageIO.write(_image1.getBufferedImage(), "png", new File("image1.png"));
But before that you'll have to open your image1 as a Mat object:
Mat _image1 = new Mat(image1);
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.