I want to compare two pictures similarity
Code:
Mat mat1=Highgui.imread("/mnt/sdcard/91.png");
Mat mat2=Highgui.imread("/mnt/sdcard/92.png");
double distance = Imgproc.compareHist(mat1, mat2, Imgproc.CV_COMP_CORREL); //(this line throws an exception)
Exception information:
01-30 10:48:20.203: E/AndroidRuntime(3540): Caused by: CvException
[org.opencv.core.CvException:
/home/andreyk/OpenCV2/trunk/opencv/modules/imgproc/src/histogram.cpp:1387:
error: (-215) H1.type() == H2.type() && H1.type() == CV_32F in
function double cv::compareHist(const cv::_InputArray&, const
cv::_InputArray&, int)
Can anybody help me? How should I solve this?
At first make sure that both images have 1 channel (if not, than convert them to grayscale with cvtColor or choose one channel witn cvSplit) and have one type, for instance, CV_8UC1.
Then calculate histograms of this images.
Example of code:
int histSize = 180;
float range[] = {0, 180};
const float* histRange = {range};
bool uniform = true;
bool accumulate = false;
cv::Mat hist1, hist2;
cv::calcHist(&mat1, 1, 0, cv::Mat(), hist1, 1, &histSize, &histRange, uniform, accumulate );
cv::calcHist(&mat2, 1, 0, cv::Mat(), hist2, 1, &histSize, &histRange, uniform, accumulate );
double result = cv::compareHist( hist1, hist2, CV_COMP_CORREL);
On Android the code would be similar to:
Mat image0 = ...; //
Mat image1 = ...;
Mat hist0 = new Mat();
Mat hist1 = new Mat();
int hist_bins = 30; //number of histogram bins
int hist_range[]= {0,180};//histogram range
MatOfFloat ranges = new MatOfFloat(0f, 256f);
MatOfInt histSize = new MatOfInt(25);
Imgproc.calcHist(Arrays.asList(image0), new MatOfInt(0), new Mat(), hist0, histSize, ranges);
Imgproc.calcHist(Arrays.asList(image1), new MatOfInt(0), new Mat(), hist1, histSize, ranges);
double res = Imgproc.compareHist(image0, image01, Imgproc.CV_COMP_CORREL);
#skornos your code is wrong.
It should be
Mat image0 = ...; //
Mat image1 = ...;
Mat hist0 = new Mat();
Mat hist1 = new Mat();
int hist_bins = 30; //number of histogram bins
int hist_range[]= {0,180};//histogram range
MatOfFloat ranges = new MatOfFloat(0f, 256f);
MatOfInt histSize = new MatOfInt(25);
Imgproc.calcHist(Arrays.asList(image0), new MatOfInt(0), new Mat(), hist0, histSize, ranges);
Imgproc.calcHist(Arrays.asList(image1), new MatOfInt(0), new Mat(), hist1, histSize, ranges);
double res = Imgproc.compareHist(hist0, hist1, Imgproc.CV_COMP_CORREL);
Note the last line, it should be hist0 compare to hist1 not comparing images
Mat hsvRef = new Mat();
Mat hsvCard = new Mat();
Mat srcRef = new Mat(refImage.getHeight(), refImage.getWidth(), CvType.CV_8UC2);
Utils.bitmapToMat(refImage, srcRef);
Mat srcCard = new Mat(cardImage.getHeight(), cardImage.getWidth(), CvType.CV_8UC2);
Utils.bitmapToMat(cardImage, srcCard);
/// Convert to HSV
Imgproc.cvtColor(srcRef, hsvRef, Imgproc.COLOR_BGR2HSV);
Imgproc.cvtColor(srcCard, hsvCard, Imgproc.COLOR_BGR2HSV);
/// Using 50 bins for hue and 60 for saturation
int hBins = 50;
int sBins = 60;
MatOfInt histSize = new MatOfInt( hBins, sBins);
// hue varies from 0 to 179, saturation from 0 to 255
MatOfFloat ranges = new MatOfFloat( 0f,180f,0f,256f );
// we compute the histogram from the 0-th and 1-st channels
MatOfInt channels = new MatOfInt(0, 1);
Mat histRef = new Mat();
Mat histCard = new Mat();
ArrayList<Mat> histImages=new ArrayList<Mat>();
histImages.add(hsvRef);
Imgproc.calcHist(histImages,
channels,
new Mat(),
histRef,
histSize,
ranges,
false);
Core.normalize(histRef,
histRef,
0,
1,
Core.NORM_MINMAX,
-1,
new Mat());
histImages=new ArrayList<Mat>();
histImages.add(hsvCard);
Imgproc.calcHist(histImages,
channels,
new Mat(),
histCard,
histSize,
ranges,
false);
Core.normalize(histCard,
histCard,
0,
1,
Core.NORM_MINMAX,
-1,
new Mat());
double resp1 = Imgproc.compareHist(histRef, histCard, 0);
Log.d(TAG, "HIST COMPARE 0" + resp1);
double resp2 = Imgproc.compareHist(histRef, histCard, 1);
Log.d(TAG, "HIST COMPARE 1" + resp2);
double resp3 = Imgproc.compareHist(histRef, histCard, 2);
Log.d(TAG, "HIST COMPARE 2" + resp3);
double resp4 = Imgproc.compareHist(histRef, histCard, 3);
Log.d(TAG, "HIST COMPARE 3" + resp4);
Related
I have devised some code for image comparison. The matching part is still a bit flawed and I would love some assitance. The project can be found at - Github .
I have these two images img1 and img2 .
These are the images of same person . When I want to compare these two images , the following code returns false .
bmpimg1 = Bitmap.createScaledBitmap(bmpimg1, 100, 100, true);
bmpimg2 = Bitmap.createScaledBitmap(bmpimg2, 100, 100, true);
Mat img1 = new Mat();
Utils.bitmapToMat(bmpimg1, img1);
Mat img2 = new Mat();
Utils.bitmapToMat(bmpimg2, img2);
Imgproc.cvtColor(img1, img1, Imgproc.COLOR_RGBA2GRAY);
Imgproc.cvtColor(img2, img2, Imgproc.COLOR_RGBA2GRAY);
img1.convertTo(img1, CvType.CV_32F);
img2.convertTo(img2, CvType.CV_32F);
//Log.d("ImageComparator", "img1:"+img1.rows()+"x"+img1.cols()+" img2:"+img2.rows()+"x"+img2.cols());
Mat hist1 = new Mat();
Mat hist2 = new Mat();
MatOfInt histSize = new MatOfInt(180);
MatOfInt channels = new MatOfInt(0);
ArrayList<Mat> bgr_planes1= new ArrayList<Mat>();
ArrayList<Mat> bgr_planes2= new ArrayList<Mat>();
Core.split(img1, bgr_planes1);
Core.split(img2, bgr_planes2);
MatOfFloat histRanges = new MatOfFloat (0f, 180f);
boolean accumulate = false;
Imgproc.calcHist(bgr_planes1, channels, new Mat(), hist1, histSize, histRanges, accumulate);
Core.normalize(hist1, hist1, 0, hist1.rows(), Core.NORM_MINMAX, -1, new Mat());
Imgproc.calcHist(bgr_planes2, channels, new Mat(), hist2, histSize, histRanges, accumulate);
Core.normalize(hist2, hist2, 0, hist2.rows(), Core.NORM_MINMAX, -1, new Mat());
img1.convertTo(img1, CvType.CV_32F);
img2.convertTo(img2, CvType.CV_32F);
hist1.convertTo(hist1, CvType.CV_32F);
hist2.convertTo(hist2, CvType.CV_32F);
double compare = Imgproc.compareHist(hist1, hist2, Imgproc.CV_COMP_CHISQR);
Log.d("ImageComparator", "compare: "+compare);
if(compare>0 && compare<1500) {
Toast.makeText(MainActivity.this, "Images may be possible duplicates, verifying", Toast.LENGTH_LONG).show();
new asyncTask(MainActivity.this).execute();
}
else if(compare==0)
Toast.makeText(MainActivity.this, "Images are exact duplicates", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Images are not duplicates", Toast.LENGTH_LONG).show();
startTime = System.currentTimeMillis();
How can I change my code so that on compare these two images , it returns true ? Any advice is of great help .
You seem to only be comparing limited feature vectors, which in this case is only a histogram of the image.
The algorithm you have used is not suitable for facial recognition as it only identifies on the image color spectrum.
See this possible duplicate on how to perform facial recognition.
In openCV I want to compare two histograms. First I transform Bitmap to Mat:
Bitmap puzzleBmp = BitmapFactory.decodeFile(photoPath, options)
mat = new Mat(puzzleBmp.getHeight(), puzzleBmp.getWidth(), CvType.CV_8U, new Scalar(4));
Utils.bitmapToMat(puzzleBmp, mat);
Next I want to create histogram of this image:
Mat mRgba = new Mat();
Imgproc.cvtColor(mat, mRgba, Imgproc.COLOR_RGBA2RGB);
Imgproc.GaussianBlur(mRgba, mRgba, new Size(5, 5), 0, Imgproc.BORDER_DEFAULT);
Mat mHSV = new Mat();
Imgproc.cvtColor(mRgba, mHSV, Imgproc.COLOR_RGB2HSV_FULL);
Mat hist = new Mat();
int h_bins = 30;
int s_bins = 32;
MatOfInt mHistSize = new MatOfInt (h_bins, s_bins);
MatOfFloat mRanges = new MatOfFloat(0, 179, 0, 255);
MatOfInt mChannels = new MatOfInt(0, 1);
List<Mat> lHSV = Arrays.asList(mHSV);
Mat mask2 = new Mat();
mask2 = Mat.zeros( mRgba.rows() + 2, mRgba.cols() + 2, CvType.CV_8UC1 );
Range rowRange = new Range( 1, mask2.rows() - 1 );
Range colRange = new Range( 1, mask2.cols() - 1 );
Mat mask = new Mat();
mask = mask2.submat(rowRange, colRange);
boolean accumulate = false;
Imgproc.calcHist(lHSV, mChannels, mask, hist, mHistSize, mRanges, accumulate);
Core.normalize(hist, hist, 0, 255, Core.NORM_MINMAX, -1, new Mat());
From this part I get hist. But when I make these transformations with two different images i always get from method Imgproc.compareHist() same values, which means they are the same pictures.
Currently I'm developing an app that will detect colored circles. I'm trying to do this by following this tutorial, where guy detects red circles on image with Python. I've written the same code, just for Java.
Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(),
CvType.CV_8UC3);
Mat hsv_image = new Mat();
Utils.bitmapToMat(bitmap, mat);
Imgproc.cvtColor(mat, hsv_image, Imgproc.COLOR_BGR2HSV);
Mat lower_red_hue_range = new Mat();
Mat upper_red_hue_range = new Mat();
Core.inRange(hsv_image, new Scalar(0, 100, 100), new Scalar(10, 255, 255), lower_red_hue_range);
Core.inRange(hsv_image, new Scalar(160, 100, 100), new Scalar(179, 255, 255), upper_red_hue_range);
Utils.matToBitmap(hsv_image, bitmap);
mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
image.setImageBitmap(mutableBitmap);
Image I use is identical to one from tutorial:
This is image with applied BGR2HSV:
When I execute the code using lower red hue range, it detects the blue circle. When I use upper red hue range it gives me black bmp(doesn't detect anything). How can it be? What am I doing wrong? This is literally copy moved from python to Java. Why's the result different then?
Thanks in advance.
Your mat is of CvType.CV_8UC1 image, i.e. you are working on a grayscale image. Try with CvType.CV_8UC3
Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC3);
hsv_image should look like this:
How to select a custom range:
You may want to detect a green circle.
Well, in HSV, tipically the range is:
H in [0,360]
S,V in [0,100]
However, for CV_8UC3 images, each component H,S,V can be represented by only 256 values at most, since it's stored in 1 byte. So, in OpenCV, the ranges H,S,V for CV_8UC3 are:
H in [0,180] <- halved to fit in the range
S,V in [0,255] <- stretched to fit the range
So to switch from typical range to OpenCV range you need to:
opencv_H = typical_H / 2;
opencv_S = typical_S * 2.55;
opencv_V = typical_V * 2.55;
So, green colors are around the value of hue of 120. The hue can have a value in the interval [0,360].
However, for Mat3b HSV images, the range for H is in [0,180], i.e. is halved so it can fit in a 8 bit representation with at most 256 possible values.
So, you want the H value to be around 120 / 2 = 60, say from 50 to 70.
You also set a minimum value for S,V to 100 in order to prevent very dark (almost black) colors.
Mat green_hue_range
inRange(hsv_image, cv::Scalar(50, 100, 100), cv::Scalar(70, 255, 255), green_hue_range);
use the following code and pass color to Blob detector and then pass an image to the detector
private Scalar converScalarRgba2HSV(Scalar rgba) {
Mat pointMatHsv= new Mat();
Mat pointMatRgba = new Mat(1, 1, CvType.CV_8UC3, rgba);
Imgproc.cvtColor(pointMatRgba,pointMatHsv, Imgproc.COLOR_RGB2HSV_FULL, 4);
return new Scalar(pointMatHsv.get(0, 0));}
// Blob Detector
public class ColorBlobDetector {
// Lower and Upper bounds for range checking in HSV color space
private Scalar mLowerBound = new Scalar(0);
private Scalar mUpperBound = new Scalar(0);
// Minimum contour area in percent for contours filtering
private static double mMinContourArea = 0.1;
// Color radius for range checking in HSV color space
private Scalar mColorRadius = new Scalar(25,50,50,0);
private Mat mSpectrum = new Mat();
private List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();
Mat mPyrDownMat = new Mat();
Mat mHsvMat = new Mat();
Mat mMask = new Mat();
Mat mDilatedMask = new Mat();
Mat mHierarchy = new Mat();
public void setColorRadius(Scalar radius) {
mColorRadius = radius;
}
public void setHsvColor(Scalar hsvColor) {
double minH = (hsvColor.val[0] >= mColorRadius.val[0]) ? hsvColor.val[0]-mColorRadius.val[0] : 0;
double maxH = (hsvColor.val[0]+mColorRadius.val[0] <= 255) ? hsvColor.val[0]+mColorRadius.val[0] : 255;
mLowerBound.val[0] = minH;
mUpperBound.val[0] = maxH;
mLowerBound.val[1] = hsvColor.val[1] - mColorRadius.val[1];
mUpperBound.val[1] = hsvColor.val[1] + mColorRadius.val[1];
mLowerBound.val[2] = hsvColor.val[2] - mColorRadius.val[2];
mUpperBound.val[2] = hsvColor.val[2] + mColorRadius.val[2];
mLowerBound.val[3] = 0;
mUpperBound.val[3] = 255;
Mat spectrumHsv = new Mat(1, (int)(maxH-minH), CvType.CV_8UC3);
for (int j = 0; j < maxH-minH; j++) {
byte[] tmp = {(byte)(minH+j), (byte)255, (byte)255};
spectrumHsv.put(0, j, tmp);
}
Imgproc.cvtColor(spectrumHsv, mSpectrum, Imgproc.COLOR_HSV2RGB_FULL, 4);
}
public Mat getSpectrum() {
return mSpectrum;
}
public void setMinContourArea(double area) {
mMinContourArea = area;
}
public void process(Mat rgbaImage) {
Imgproc.pyrDown(rgbaImage, mPyrDownMat);
Imgproc.pyrDown(mPyrDownMat, mPyrDownMat);
Imgproc.cvtColor(mPyrDownMat, mHsvMat, Imgproc.COLOR_RGB2HSV_FULL);
Core.inRange(mHsvMat, mLowerBound, mUpperBound, mMask);
Imgproc.dilate(mMask, mDilatedMask, new Mat());
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(mDilatedMask, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
// Find max contour area
double maxArea = 0;
Iterator<MatOfPoint> each = contours.iterator();
while (each.hasNext()) {
MatOfPoint wrapper = each.next();
double area = Imgproc.contourArea(wrapper);
if (area > maxArea)
maxArea = area;
}
// Filter contours by area and resize to fit the original image size
mContours.clear();
each = contours.iterator();
while (each.hasNext()) {
MatOfPoint contour = each.next();
if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) {
Core.multiply(contour, new Scalar(4,4), contour);
mContours.add(contour);
}
}
}
public List<MatOfPoint> getContours() {
return mContours;
}}
now set detector
public void initDetector() {
mDetector = new ColorBlobDetector();
mSpectrum = new Mat();
mBlobColorRgba = new Scalar(255);
mBlobColorHsv = new Scalar(255);
SPECTRUM_SIZE = new org.opencv.core.Size(500, 64);
CONTOUR_COLOR = new Scalar(0, 255, 0, 255);
mDetector.setHsvColor(converScalarRgba2HSV(new Scalar(0,255,255,255)));
Imgproc.resize(mDetector.getSpectrum(), mSpectrum, SPECTRUM_SIZE, 0, 0, Imgproc.INTER_LINEAR_EXACT);
mIsColorSelected = true;
}
now pass an image to a detector object
Mat mRgba = new Mat(inputFrame.height(), inputFrame.width(), CvType.CV_8UC4);
mRgba = inputFrame;
mDetector.process(mRgba);
List<MatOfPoint> contours = mDetector.getContours();
Log.e(TAG, "Contours count: " + contours.size());
drawContours(mRgba, contours, -1, CONTOUR_COLOR);
return mRgba;
Happy Codeing !!!
I'm trying to train my svm with 4 image. all of my images are 300*400. I resize them to 304*400 so i can get the HOGDescriptor of my images because of 16*16 block. then I use Core.hconcat(mats, trainData) to gather all of my images into one Mat. after that when I try to set lables for my trainData, in train part I get below Error. I'm new to openCV. what is wrong?
Mat rose1 = new Mat();
Mat rose2 = new Mat();
Mat rose3 = new Mat();
Mat rose4 = new Mat();
Mat rose5 = new Mat();
try {
rose1 = org.opencv.android.Utils.loadResource(
getApplicationContext(), R.drawable.rose1);
rose2 = org.opencv.android.Utils.loadResource(
getApplicationContext(), R.drawable.rose2);
rose3 = org.opencv.android.Utils.loadResource(
getApplicationContext(), R.drawable.rose3);
rose4 = org.opencv.android.Utils.loadResource(
getApplicationContext(), R.drawable.rose4);
rose5 = org.opencv.android.Utils.loadResource(
getApplicationContext(), R.drawable.rose5);
} catch (IOException e) {
e.printStackTrace();
}
Mat rose1Resized = new Mat();
Mat rose2Resized = new Mat();
Mat rose3Resized = new Mat();
Mat rose4Resized = new Mat();
Size sz = new Size(304, 400);
Imgproc.resize(rose1, rose1Resized, sz);
Imgproc.resize(rose2, rose2Resized, sz);
Imgproc.resize(rose3, rose3Resized, sz);
Imgproc.resize(rose4, rose4Resized, sz);
// HOG
MatOfFloat rose1Float = new MatOfFloat();
MatOfFloat rose2Float = new MatOfFloat();
MatOfFloat rose3Float = new MatOfFloat();
MatOfFloat rose4Float = new MatOfFloat();
HOGDescriptor hog = new HOGDescriptor(new Size(304, 400), new Size(16,
16), new Size(new Point(8, 8)), new Size(new Point(8, 8)), 9);
hog.compute(rose1Resized, rose1Float);
hog.compute(rose2Resized, rose2Float);
hog.compute(rose3Resized, rose3Float);
hog.compute(rose4Resized, rose4Float);
ArrayList<Mat> mats = new ArrayList<>();
mats.add(rose1Float);
mats.add(rose2Float);
mats.add(rose3Float);
mats.add(rose4Float);
// SVM
Mat trainData = new Mat();
Core.hconcat(mats, trainData);
float[] lableFloat = { 1, 1, 1, 1 };
Mat lables = new Mat(1, 4, CvType.CV_32FC1);
lables.put(0, 0, lableFloat);
CvSVM svm = new CvSVM();
CvSVMParams params = new CvSVMParams();
params.set_svm_type(CvSVM.C_SVC);
params.set_kernel_type(CvSVM.LINEAR);
params.set_term_crit(new TermCriteria(TermCriteria.EPS, 100, 1e-6));
svm.train(trainData, lables, new Mat(), new Mat(), params);
Error is:
E/AndroidRuntime(27347): CvException [org.opencv.core.CvException: cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/ml/src/inner_functions.cpp:671: error: (-209) Response array must contain as many elements as the total number of samples in function cvPreprocessCategoricalResponses
first of all i reshape MatOfFloat after get HOG. because rose1Float was 65268*1 and i need it in one row Mat.
Mat roseReshaped1 = rose1Float.reshape(1, 1);
Mat roseReshaped2 = rose2Float.reshape(1, 1);
Mat roseReshaped3 = rose3Float.reshape(1, 1);
Mat roseReshaped4 = rose4Float.reshape(1, 1);
then i used push_back instead of "Core.hconcat(mats, trainData)"
Mat trainData = new Mat(0, sizeOfCols, CvType.CV_32FC1);
trainData.push_back(roseReshaped1);
trainData.push_back(roseReshaped2);
trainData.push_back(roseReshaped3);
trainData.push_back(roseReshaped4);
my trainData would be 4*65268 and this is my label. or as opencv say, response!
int[] l = { 1, 2, 3, 4 };
Mat lables = new Mat(4, 1, CvType.CV_32SC1);
lables.put(0, 0, l);
now everything works perfectly fine. Thanks to #berak.
I want to implement follwoing OpenCV example in a Android app:
http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html
My code is the foll0wing:
//First convert Bitmap to Mat
Mat ImageMat = new Mat ( image.getHeight(), image.getWidth(), CvType.CV_64FC1, new Scalar(4));
Bitmap myBitmap32 = image.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, ImageMat);
Imgproc.cvtColor(ImageMat, ImageMat, Imgproc.COLOR_RGB2GRAY);
Mat padded = new Mat(CvType.CV_64FC1); //expand input image to optimal size
int m = Core.getOptimalDFTSize(ImageMat.rows());
int n = Core.getOptimalDFTSize(ImageMat.cols()); // on the border add zero values
Imgproc.copyMakeBorder(ImageMat, padded, 0, m - ImageMat.rows(), 0, n - ImageMat.cols(), Imgproc.BORDER_CONSTANT);
List<Mat> planes = new ArrayList<Mat>();
planes.add(padded);
planes.add(Mat.zeros(padded.rows(), padded.cols(), CvType.CV_64FC1));
Mat complexI = Mat.zeros(padded.rows(), padded.cols(), CvType.CV_64FC1);
Core.merge(planes, complexI); // Add to the expanded another plane with zeros
Core.dft(complexI, complexI); // this way the result may fit in the source matrix
// compute the magnitude and switch to logarithmic scale
// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
Core.split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
Core.magnitude(planes.get(0), planes.get(1), planes.get(1));// planes[0] = magnitude
Mat magI = planes.get(0);
Core.add(magI, Mat.ones(padded.rows(), padded.cols(), CvType.CV_64FC1), magI); // switch to logarithmic scale
Core.log(magI, magI);
Mat crop = new Mat(magI, new Rect(0, 0, magI.cols() & -2, magI.rows() & -2));
magI = crop.clone();
// rearrange the quadrants of Fourier image so that the origin is at the image center
int cx = magI.cols()/2;
int cy = magI.rows()/2;
Rect q0Rect = new Rect (0, 0, cx, cy);
Rect q1Rect = new Rect (cx, 0, cx, cy);
Rect q2Rect = new Rect (0, cy, cx, cy);
Rect q3Rect = new Rect (cx, cy, cx, cy);
Mat q0 = new Mat(magI, q0Rect); // Top-Left - Create a ROI per quadrant
Mat q1 = new Mat(magI, q1Rect); // Top-Right
Mat q2 = new Mat(magI, q2Rect); // Bottom-Left
Mat q3 = new Mat(magI, q3Rect); // Bottom-Right
Mat tmp = new Mat(); // swap quadrants (Top-Left with Bottom-Right)
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
q2.copyTo(q1);
tmp.copyTo(q2);
Core.normalize(magI, magI, 0, 1, Core.NORM_MINMAX);
Mat realResult = new Mat();
magI.convertTo(realResult, CvType.CV_64FC1);
//Then convert the processed Mat to Bitmap
Bitmap resultBitmap = Bitmap.createBitmap(ImageMat.cols(), ImageMat.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(ImageMat, resultBitmap);
//Set member to the Result Bitmap. This member is displayed in an ImageView
mResult = resultBitmap;
(note: image is the Input Bitmap and mResult is the output bitmap that is shown in a ImageView)
I get the following error:
Error: 08-08 12:17:36.207: A/libc(1594): Fatal signal 11 (SIGSEGV) at 0x0000000a (code=1), thread 1594 (XXXX)
Is anyone able to see my error?
I copied the code and got it to work on Android. There's a few changes I've made, not sure all are necessary but here they are:
having the dst and src the same is ok in C++ but I'm not sure Java implementation is as tolerant. I tend to always create different objects for these to avoid any conflicts
The 'padded' Mat object: I've initialised with the size:
Mat padded = new Mat(new Size(n, m), CvType.CV_64FC1)
Mat complexI should be of type CV_64FC2 I think.
I set the upper bound variable for the Core.normalize call to 255
I convert the results back to a CV_8UC1 so I can display on my implementation.
Here is the code I've been using:
private Mat getDFT(Mat singleChannel) {
singleChannel.convertTo(image1, CvType.CV_64FC1);
int m = Core.getOptimalDFTSize(image1.rows());
int n = Core.getOptimalDFTSize(image1.cols()); // on the border
// add zero
// values
// Imgproc.copyMakeBorder(image1,
// padded, 0, m -
// image1.rows(), 0, n
Mat padded = new Mat(new Size(n, m), CvType.CV_64FC1); // expand input
// image to
// optimal size
Imgproc.copyMakeBorder(image1, padded, 0, m - singleChannel.rows(), 0,
n - singleChannel.cols(), Imgproc.BORDER_CONSTANT);
List<Mat> planes = new ArrayList<Mat>();
planes.add(padded);
planes.add(Mat.zeros(padded.rows(), padded.cols(), CvType.CV_64FC1));
Mat complexI = Mat.zeros(padded.rows(), padded.cols(), CvType.CV_64FC2);
Mat complexI2 = Mat
.zeros(padded.rows(), padded.cols(), CvType.CV_64FC2);
Core.merge(planes, complexI); // Add to the expanded another plane with
// zeros
Core.dft(complexI, complexI2); // this way the result may fit in the
// source matrix
// compute the magnitude and switch to logarithmic scale
// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
Core.split(complexI2, planes); // planes[0] = Re(DFT(I), planes[1] =
// Im(DFT(I))
Mat mag = new Mat(planes.get(0).size(), planes.get(0).type());
Core.magnitude(planes.get(0), planes.get(1), mag);// planes[0]
// =
// magnitude
Mat magI = mag;
Mat magI2 = new Mat(magI.size(), magI.type());
Mat magI3 = new Mat(magI.size(), magI.type());
Mat magI4 = new Mat(magI.size(), magI.type());
Mat magI5 = new Mat(magI.size(), magI.type());
Core.add(magI, Mat.ones(padded.rows(), padded.cols(), CvType.CV_64FC1),
magI2); // switch to logarithmic scale
Core.log(magI2, magI3);
Mat crop = new Mat(magI3, new Rect(0, 0, magI3.cols() & -2,
magI3.rows() & -2));
magI4 = crop.clone();
// rearrange the quadrants of Fourier image so that the origin is at the
// image center
int cx = magI4.cols() / 2;
int cy = magI4.rows() / 2;
Rect q0Rect = new Rect(0, 0, cx, cy);
Rect q1Rect = new Rect(cx, 0, cx, cy);
Rect q2Rect = new Rect(0, cy, cx, cy);
Rect q3Rect = new Rect(cx, cy, cx, cy);
Mat q0 = new Mat(magI4, q0Rect); // Top-Left - Create a ROI per quadrant
Mat q1 = new Mat(magI4, q1Rect); // Top-Right
Mat q2 = new Mat(magI4, q2Rect); // Bottom-Left
Mat q3 = new Mat(magI4, q3Rect); // Bottom-Right
Mat tmp = new Mat(); // swap quadrants (Top-Left with Bottom-Right)
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
q2.copyTo(q1);
tmp.copyTo(q2);
Core.normalize(magI4, magI5, 0, 255, Core.NORM_MINMAX);
Mat realResult = new Mat(magI5.size(), CvType.CV_8UC1);
magI5.convertTo(realResult, CvType.CV_8UC1);
return realResult;
}
Here is an example of the results; the background is the original image; bottom left is the single channel version passed to the function and top right is the image returned by the function.