Convert OpenCv DFT example from C++ to Android -
i want implement follwoing opencv example in android app:
http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html
my code foll0wing:
//first convert bitmap 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 optimal size int m = core.getoptimaldftsize(imagemat.rows()); int n = core.getoptimaldftsize(imagemat.cols()); // on border add 0 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 expanded plane zeros core.dft(complexi, complexi); // way result may fit in source matrix // compute magnitude , switch 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 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 quadrants of fourier image origin @ 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 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 bottom-right) q0.copyto(tmp); q3.copyto(q0); tmp.copyto(q3); q1.copyto(tmp); // swap quadrant (top-right 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 processed mat bitmap bitmap resultbitmap = bitmap.createbitmap(imagemat.cols(), imagemat.rows(),bitmap.config.argb_8888);; utils.mattobitmap(imagemat, resultbitmap); //set member result bitmap. member displayed in imageview mresult = resultbitmap;
(note: image
input bitmap , mresult
output bitmap shown in imageview)
i following error:
error: 08-08 12:17:36.207: a/libc(1594): fatal signal 11 (sigsegv) @ 0x0000000a (code=1), thread 1594 (xxxx)
is able see error?
i copied code , got work on android. there's few changes i've made, not sure necessary here are:
- having dst , src same ok in c++ i'm not sure java implementation tolerant. tend create different objects these avoid conflicts
the 'padded' mat object: i've initialised size:
mat padded = new mat(new size(n, m), cvtype.cv_64fc1)
mat complexi should of type cv_64fc2 think.
- i set upper bound variable core.normalize call 255
- i convert results cv_8uc1 can display on implementation.
here 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 border // add 0 // 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 // 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 expanded plane // zeros core.dft(complexi, complexi2); // way result may fit in // source matrix // compute magnitude , switch 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 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 quadrants of fourier image origin @ // 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 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 bottom-right) q0.copyto(tmp); q3.copyto(q0); tmp.copyto(q3); q1.copyto(tmp); // swap quadrant (top-right 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 example of results; background original image; bottom left single channel version passed function , top right image returned function.
Comments
Post a Comment