I am a newbie for OpenCV and working with Android. For my case I need to find out the differences between two images and mark them with rectangles to the second image like the following image. Kindly help me for this. Thanks in advanced.
1 | initial version |
I am a newbie for OpenCV and working with Android. For my case I need to find out the differences between two images and mark them with rectangles to the second image like the following image. Kindly help me for this. Thanks in advanced.
I am a newbie for OpenCV and working with Android. For my case I need to find out the differences between two images and mark them with rectangles to the second image like the following image. Kindly help me for this. \
I worked on comparing the two source images(second and third) and got the output image(fourth) as below. And tried to get contours of the output image and draw rectangles on that. But it didn't work. I have attached the activity code. Kindly look into that and help me to achieve this.
Thanks in advanced.
CODE:
package com.example.akshika.opencvtest;
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ImageView;
import org.opencv.android.BaseLoaderCallback; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.android.Utils; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.imgproc.Imgproc;
import java.util.ArrayList; import java.util.List;
public class Main2Activity extends AppCompatActivity { Mat firstMat, secondMat, outputImageMat, outputMat = new Mat(); Bitmap firstBitmap, secondBitmap; ImageView firstImage, secondImage, outputImage;
static {
if (!OpenCVLoader.initDebug())
Log.d("ERROR", "Unable to load OpenCV");
else
Log.d("SUCCESS", "OpenCV loaded");
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
Log.v("OpenCV", "OpenCV loaded successfully");
}
break;
default: {
Log.v("OpenCV", "OpenCV loaded fail");
super.onManagerConnected(status);
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
firstImage = (ImageView) findViewById(R.id.imageinput1);
secondImage = (ImageView) findViewById(R.id.imageinput2);
outputImage = (ImageView) findViewById(R.id.imageoutput);
firstBitmap = drawableToBitmap(R.drawable.image1);
secondBitmap = drawableToBitmap(R.drawable.image2);
firstImage.setImageBitmap(firstBitmap);
secondImage.setImageBitmap(secondBitmap);
outputImage.setImageResource(R.drawable.img);
firstMat = setBitmapToMat(firstBitmap);
secondMat = setBitmapToMat(secondBitmap);
outputImageMat = new Mat(secondMat.size(), CvType.CV_8UC1);
Core.compare(firstMat, secondMat, outputImageMat, Core.CMP_LE);
// Core.absdiff(firstMat, secondMat, outputImageMat);
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.cvtColor(outputImageMat, outputMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.findContours(outputMat, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) {
Rect rect = Imgproc.boundingRect(contours.get(i));
Imgproc.rectangle(outputImageMat, new Point(rect.x, rect.y), new Point(rect.x + rect.width,
rect.y + rect.height), new Scalar(0, 0, 255), 2);
}
outputImage.setImageBitmap(setOutputMatToBitmap(outputImageMat));
}
Bitmap drawableToBitmap(int drawable) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
return BitmapFactory.decodeResource(getResources(), drawable, opt);
}
Bitmap setOutputMatToBitmap(Mat mat) {
Bitmap scale = Bitmap.createBitmap(mat.width(), mat.height(), Bitmap.Config.ARGB_8888);
// Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2RGBA); Utils.matToBitmap(mat, scale); return scale; }
Mat setBitmapToMat(Bitmap bitmap) {
Mat mat = new Mat();
Bitmap bmp32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp32, mat);
return mat;
}
}
I am a newbie for OpenCV and working with Android. For my case I need to find out the differences between two images and mark them with rectangles to the second image like the following image. Kindly help me for this. \
I worked on comparing the two source images(second and third) and got the output image(fourth) as below. And tried to get contours of the output image and draw rectangles on that. But it didn't work. I have attached the activity code. Kindly look into that and help me to achieve this.
Thanks in advanced.
CODE:
package com.example.akshika.opencvtest;
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ImageView;
import org.opencv.android.BaseLoaderCallback; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.android.Utils; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.imgproc.Imgproc;
import java.util.ArrayList; import java.util.List;
public class Main2Activity extends AppCompatActivity { Mat firstMat, secondMat, outputImageMat, outputMat = new Mat(); Bitmap firstBitmap, secondBitmap; ImageView firstImage, secondImage, outputImage;
static {
if (!OpenCVLoader.initDebug())
Log.d("ERROR", "Unable to load OpenCV");
else
Log.d("SUCCESS", "OpenCV loaded");
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
Log.v("OpenCV", "OpenCV loaded successfully");
}
break;
default: {
Log.v("OpenCV", "OpenCV loaded fail");
super.onManagerConnected(status);
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
firstImage = (ImageView) findViewById(R.id.imageinput1);
secondImage = (ImageView) findViewById(R.id.imageinput2);
outputImage = (ImageView) findViewById(R.id.imageoutput);
firstBitmap = drawableToBitmap(R.drawable.image1);
secondBitmap = drawableToBitmap(R.drawable.image2);
firstImage.setImageBitmap(firstBitmap);
secondImage.setImageBitmap(secondBitmap);
outputImage.setImageResource(R.drawable.img);
firstMat = setBitmapToMat(firstBitmap);
secondMat = setBitmapToMat(secondBitmap);
outputImageMat = new Mat(secondMat.size(), CvType.CV_8UC1);
Core.compare(firstMat, secondMat, outputImageMat, Core.CMP_LE);
// Core.absdiff(firstMat, secondMat, outputImageMat);
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.cvtColor(outputImageMat, outputMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.findContours(outputMat, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) {
Rect rect = Imgproc.boundingRect(contours.get(i));
Imgproc.rectangle(outputImageMat, new Point(rect.x, rect.y), new Point(rect.x + rect.width,
rect.y + rect.height), new Scalar(0, 0, 255), 2);
}
outputImage.setImageBitmap(setOutputMatToBitmap(outputImageMat));
}
Bitmap drawableToBitmap(int drawable) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
return BitmapFactory.decodeResource(getResources(), drawable, opt);
}
Bitmap setOutputMatToBitmap(Mat mat) {
Bitmap scale = Bitmap.createBitmap(mat.width(), mat.height(), Bitmap.Config.ARGB_8888);
// Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2RGBA); Utils.matToBitmap(mat, scale); return scale; }
Mat setBitmapToMat(Bitmap bitmap) {
Mat mat = new Mat();
Bitmap bmp32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp32, mat);
return mat;
}
}