BackgroundSubtractor getBackgroundImage() function return empty Image - java

I am using OpenCV 3.0 in Java, I want to use BackgroundSubtractor to get foreground objects and also I want to get the background image, but when I call getBackgroundImage() method of BackgroundSubtractor return me an empty image. The code is bellow:
Mat backgroundImage = new Mat();
mog2.getBackgroundImage(backgroundImage);
According to the OpenCV Documentation this method can return the background image and put the data to the given Mat:
Computes a background image.
C++: void BackgroundSubtractor::getBackgroundImage(OutputArray backgroundImage) const
Parameters:
backgroundImage – The output background image.
What will be the problem? If anyone knows about this, please let me know.
Thanks in advance!

Related

Applying sobel filter on jpg picture with OpenCV 2.4.10 in Java

I'm trying to add sobel operator on a jpg picture with Java. I found example here: http://www.tutorialspoint.com/java_dip/applying_sobel_operator.htm but it doesn't work. Instead it prints black image. Could someone explain to me what I did wrong, please? Other imgproc functions works well.
Here is my code:
Mat sourceImage = Highgui.imread(sourcePath, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
Mat destinationImage = new Mat(sourceImage.rows(), sourceImage.cols(), sourceImage.type());
Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F){
{
put(0,0,-1);
put(0,1,0);
put(0,2,1);
put(1,0-2);
put(1,1,0);
put(1,2,2);
put(2,0,-1);
put(2,1,0);
put(2,2,1);
}
};
Imgproc.filter2D(sourceImage, destinationImage, -1, kernel);
Highgui.imwrite(destinationPath, destinationImage);
//display
new ShowImage(sourcePath, sourceImage);
new ShowImage(destinationPath, destinationImage);
First up, is there a reason why you're not using
Imgproc.Sobel(Mat src, Mat dst, int ddepth, int dx, int dy);
I'm not sure of the Java syntax used here. When does the block of puts get executed? Is it assumed to part of a constructor for a subclass of Mat that you define ? Having said that, assuming that this does what it looks like it's intended to do then it's possible that your output file type is not correctly specified; what is the value of destinationPath ?
Have you tried opening the saved file in an alternative image viewer to determine if it's the ShowImage() code or the saved file that's at fault ?
Have you tried opening the saved file in a hex editor to see if it has 'sensible' looking values or is all zeros ?

processing copy/duplicate resize issue

not posted before, so be patient.
I'm having an issue in the processing IDE with copying PImages and then resizing. Resizing a copy of an image also appears to resize the original.
void setup(){
size(10,10,P2D);
PImage img;
img = loadImage("nickwire.jpg");
println(img.width);
PImage dupe;
dupe=img;
dupe.resize(10,10);
print(img.width);print("\t");println(dupe.width);
}
//console outputs:
//263
//10 10
//I'm expecting
//263
//263 10
What am I doing wrong?
You are not creating a copy of your image, you are creating a new reference and pointing it to the same image.
In order to copy the image to the new reference, take a look at the PImage#get() method.

Android Image Masking not rendering as intended

I am using this blog post to generate a the masking effect. I might be missing some thing , I am not able to achieve the intended behavior. I am new to image processing. based on my internet research I am assuming I got to have a JPEG image for the originol image and the PNG format for the mask image with the same dimension . I even tried to create images as below
My Images :
some of the images I created to create masking effect :
But the resulted Image using this logic looks like this ,
.
Why is it ? Am I missing some thing here ? The only Images working so far is the one in the example on that link.
My masking code :
public static Bitmap getMaskedBitmap(Resources res, int sourceResId, int maskResId) {
BitmapFactory.Options options = new BitmapFactory.Options();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
options.inMutable = true;
}
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap source = BitmapFactory.decodeResource(res, sourceResId, options);
Bitmap bitmap;
if (source.isMutable()) {
bitmap = source;
} else {
bitmap = source.copy(Bitmap.Config.ARGB_8888, true);
source.recycle();
}
bitmap.setHasAlpha(true);
Canvas canvas = new Canvas(bitmap);
Bitmap mask = BitmapFactory.decodeResource(res, maskResId);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(mask, 0, 0, paint);
mask.recycle();
return bitmap;
}
Please let me know where am I going wrong ?
Is there any requirement for the Images ? especially Mask Image
I have tested on Samsung S3 OS 4.2
UPDate :
I tried the mask Image from the blog post with my own JPEG .IT works perfect so the issue is narrowed down to mask Image. It expects some kind of configuration. Please let me know about it if any one know faced this before or am I missing some thing .
Latest UPDate :
I finally figured out. it is the alpha channel inside a mask image that makes the difference . If you are a programmer without a UI designer then you got to take care of this alpha channel
Referring to this SO answer Android: Bitmap recycle() how does it work?
This particular line of code is likely the problem. I can say you probably should not call
mask.recycle();
I haven't tested it but I think its worth a shot.This should work since when you recycle mask the masked image itself is GCed. So if you don't call it it should work.
I finally figured out , the mask image got to have a alpha channel . On Photoshop you have to add a transparency layer to get set the alpha channel enabled.

How can I make this transformation of an image in Android using Java

Is there a way to process an image / drawable in Android (Java) to make the last X pixels of the image stretch or repeat to the bottom of the screen. I would like to know if this is possible to achieve and if there is a name for this effect or existing algorithm. Thank you in advance. Just please note that I am aware of 9patch images and it is not the way I want to go with, I explicitly need to apply this in code. Thank you in advance, bellow is an image
The best option to achieve that is using a Draw 9-patch image
Tutorials :
Android 9 Patch Image Tutorial
Draw9patch Tutorial [HD]
Possible algorithm:
1) get Bitmap object of your image
2) read image pixels(bitmap.getPixels(...) method) into an int[] array
3) create a blank bitmap of desired height
4) use setPixels() method to copy initial bitmap data to a new one
5) use same method to copy last line to new bitmap as many times as you want(use offset parameter wisely)
This sounds too complicated for such a small task, but you asked for a way of acheiving it programmatically, so here you go
EDIT Easier way for your situation:
Make use of Shader.TileMode.CLAMP
1) Create BitmapDrawable from your bitmap or from resources
2) Use setTileModeY(TilMode.CLAMP);
You can take a strip from the image and use repeat like this
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/your_pattern"
android:tileMode="repeat" />
EDIT : If you wanted programmatically
1- crop from the image using this code from cut the portion of bitmap :
private Bitmap cropBitmap1()
{
Bitmap bmp2 = BitmapFactory.decodeResource(this.getResources(), R.drawable.image1);
Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), 1, Bitmap.Config.ARGB_8888);
Paint p = new Paint();
p.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
Canvas c = new Canvas(bmOverlay);
c.drawBitmap(bmp2, 0, 0, null);
c.drawRect(30, 30, 100, 100, p);
return bmOverlay;
}
2- The returned bitmap , convert it to bitmapdrawable:
BitmapDrawable navigationBackground =new BitmapDrawable(getResources(),bitmap);
navigationBackground.setTileModeX(Shader.TileMode.REPEAT);

How can I remove background from an (video)image using OpenCV and Java?

I just started learning OpneCV and started my project in Java. As Java wrapper of OpenCV is released recently, there isn't much documentation available.
I am trying to separate the background and foreground from video captured through webcam. I tried using the BackgroundSubtractorMog class in java but failed to get the desired output.
Here is my code:
VideoCapture capture = new VideoCapture(0);
Mat camImage = new Mat();
if (capture.isOpened()) {
while (true) {
capture.read(camImage);
BackgroundSubtractorMOG backgroundSubtractorMOG=new BackgroundSubtractorMOG();
Mat fgMask=new Mat();
backgroundSubtractorMOG.apply(camImage, fgMask,0.1);
Mat output=new Mat();
camImage.copyTo(output,fgMask);
displayImageOnScreen(output);
}
}
This code just gives a blackscreen output.
Move this line:
BackgroundSubtractorMOG backgroundSubtractorMOG=new BackgroundSubtractorMOG();
Out of the loop.
unfortunately, as of now, you can't use any of the BackgroundSubtractorXXX from java
(problem with the wrappers, the code is there, but the creator function is missing)
there's a pull request for this already, hope it will get accepted soon.

Categories

Resources