Skip to content

Commit 810c075

Browse files
committed
switching between fast and precise page segmentation (via the navigation drawer)
removed debug output in FocusMeasure.cpp
1 parent b2b34ec commit 810c075

File tree

9 files changed

+57
-49
lines changed

9 files changed

+57
-49
lines changed

app/src/main/java/at/ac/tuwien/caa/docscan/CameraActivity.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,18 @@ private void selectDrawerItem(MenuItem menuItem) {
826826

827827
break;
828828

829+
// Switch between the two page segmentation methods:
830+
case R.id.action_precise_page_seg:
831+
832+
if (NativeWrapper.useLab()) {
833+
NativeWrapper.setUseLab(false);
834+
menuItem.setTitle(R.string.precise_page_seg_text);
835+
}
836+
else {
837+
NativeWrapper.setUseLab(true);
838+
menuItem.setTitle(R.string.fast_page_seg_text);
839+
}
840+
829841

830842
}
831843

app/src/main/java/at/ac/tuwien/caa/docscan/NativeWrapper.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
*/
3434
public class NativeWrapper {
3535

36+
private static boolean mUseLab = true;
37+
3638

3739
/**
3840
* Returns an array of Patch objects, containing focus measurement results.
@@ -54,7 +56,7 @@ public static Patch[] getFocusMeasures(Mat src) {
5456
*/
5557
public static DkPolyRect[] getPageSegmentation(Mat src) {
5658

57-
return nativeGetPageSegmentation(src.getNativeObjAddr());
59+
return nativeGetPageSegmentation(src.getNativeObjAddr(), mUseLab);
5860

5961
}
6062

@@ -77,7 +79,7 @@ public static double getIllumination(Mat src, DkPolyRect polyRect) {
7779
* @return array of DKPolyRect objects
7880
*/
7981
@SuppressWarnings("JniMissingFunction")
80-
private static native DkPolyRect[] nativeGetPageSegmentation(long src);
82+
private static native DkPolyRect[] nativeGetPageSegmentation(long src, boolean useLab);
8183

8284
/**
8385
* Native method for illumination computation.
@@ -88,6 +90,14 @@ public static double getIllumination(Mat src, DkPolyRect polyRect) {
8890
@SuppressWarnings("JniMissingFunction")
8991
private static native double nativeGetIllumination(long src, DkPolyRect polyRect);
9092

93+
public static void setUseLab(boolean useLab) {
94+
mUseLab = useLab;
95+
}
96+
97+
public static boolean useLab() {
98+
return mUseLab;
99+
}
100+
91101

92102
// Callbacks:
93103
public interface CVCallback {

app/src/main/jni/DocScanInterface.cpp

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ JNIEXPORT jdouble JNICALL Java_at_ac_tuwien_caa_docscan_NativeWrapper_nativeGetI
176176

177177
}
178178

179-
JNIEXPORT jobjectArray JNICALL Java_at_ac_tuwien_caa_docscan_NativeWrapper_nativeGetPageSegmentation(JNIEnv * env, jclass cls, jlong src) {
179+
JNIEXPORT jobjectArray JNICALL Java_at_ac_tuwien_caa_docscan_NativeWrapper_nativeGetPageSegmentation(JNIEnv * env, jclass cls, jlong src, jboolean useLab) {
180180

181181

182182

183183

184184
// call the main function:
185-
std::vector<dsc::DkPolyRect> polyRects = dsc::DkPageSegmentation::apply(*((cv::Mat*)src));
185+
std::vector<dsc::DkPolyRect> polyRects = dsc::DkPageSegmentation::apply(*((cv::Mat*)src), useLab);
186186

187187
jclass polyRectClass = env->FindClass("at/ac/tuwien/caa/docscan/cv/DkPolyRect");
188188

@@ -205,46 +205,16 @@ JNIEXPORT jobjectArray JNICALL Java_at_ac_tuwien_caa_docscan_NativeWrapper_nativ
205205

206206
std::vector<cv::Point> points = polyRects[i].toCvPoints();
207207

208-
if (points.empty()) {
209-
210-
std::stringstream strs;
211-
strs << i;
212-
std::string temp_str = strs.str();
213-
char* char_type = (char*) temp_str.c_str();
214-
215-
__android_log_write(ANDROID_LOG_INFO, "DocScanInterfaceEmpty", char_type);
216-
208+
if (points.empty())
217209
continue;
218210

219-
220-
}
221-
else {
222-
223-
std::stringstream strs;
224-
strs << i;
225-
std::string temp_str = strs.str();
226-
char* char_type = (char*) temp_str.c_str();
227-
}
228-
229211
polyRect = env->NewObject(polyRectClass, cnstrctr,
230212
(float) points[0].x, (float) points[0].y, (float) points[1].x, (float) points[1].y, (float) points[2].x, (float) points[2].y, (float) points[3].x, (float) points[3].y);
231213

232214
env->SetObjectArrayElement(outJNIArray, i, polyRect);
233215

234-
235216
}
236217

237-
/*
238-
std::stringstream strs;
239-
strs << polyRects.size();
240-
std::string temp_str = strs.str();
241-
char* char_type = (char*) temp_str.c_str();
242-
243-
__android_log_write(ANDROID_LOG_INFO, "DocScanInterface", char_type);
244-
*/
245-
246-
247-
248218
return outJNIArray;
249219

250220
}

app/src/main/jni/DocScanInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
extern "C" {
3131
JNIEXPORT jobjectArray JNICALL Java_at_ac_tuwien_caa_docscan_NativeWrapper_nativeGetFocusMeasures(JNIEnv *, jclass, jlong);
32-
JNIEXPORT jobjectArray JNICALL Java_at_ac_tuwien_caa_docscan_NativeWrapper_nativeGetPageSegmentation(JNIEnv *, jclass, jlong);
32+
JNIEXPORT jobjectArray JNICALL Java_at_ac_tuwien_caa_docscan_NativeWrapper_nativeGetPageSegmentation(JNIEnv *, jclass, jlong, jboolean useLab);
3333
JNIEXPORT jdouble JNICALL Java_at_ac_tuwien_caa_docscan_NativeWrapper_nativeGetIllumination(JNIEnv *, jclass, jlong, jobject);
3434

3535
JNIEXPORT void JNICALL Java_at_ac_tuwien_caa_docscan_NativeWrapper_nativeGetPageSegmentationTest(JNIEnv*, jobject, jint, jint, jbyteArray, jintArray);

app/src/main/jni/FocusMeasure.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,6 @@ namespace dsc {
588588
/// <returns>A vector with image patches containing the fm value.</returns>
589589
std::vector<dsc::Patch> dsc::FocusEstimation::apply(const cv::Mat & src, const double globalFMThr) {
590590

591-
Utils::print("FocusMeasure", "1");
592-
593591
static dsc::FocusEstimation fe;
594592
int w = src.cols < src.rows ? src.cols : src.rows;
595593
int ws = (int)ceil((double)w / 5.0);
@@ -599,7 +597,6 @@ namespace dsc {
599597
fe.setImg(src);
600598
fe.setGlobalFMThreshold(globalFMThr);
601599

602-
dsc::Utils::print("FocusMeasure", "2");
603600

604601
////version 1
605602
//fe.compute(dsc::FocusEstimation::FocusMeasure::LAPV);
@@ -632,8 +629,6 @@ namespace dsc {
632629

633630
}
634631

635-
dsc::Utils::print("FocusMeasure", "3");
636-
637632
return resultP;
638633

639634

app/src/main/jni/PageSegmentation.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ DkPolyRect DkPageSegmentation::getMaxRect() const {
8484
// return mImg; // no document page found
8585
//}
8686

87-
void DkPageSegmentation::compute() {
87+
void DkPageSegmentation::compute(bool useLab) {
8888

8989
// compute scale factor
9090
int maxImgSide = std::max(mImg.rows, mImg.cols);
@@ -97,7 +97,20 @@ void DkPageSegmentation::compute() {
9797
if (scale > 0.8f || scale <= 0.0f)
9898
scale = 1.0f;
9999

100-
findRectanglesLab(mImg, mRects);
100+
if (useLab)
101+
findRectanglesLab(mImg, mRects);
102+
else {
103+
cv::Mat imgLab;
104+
cv::cvtColor(mImg, imgLab, CV_RGB2Lab); // luminance channel is better than grayscale
105+
106+
int ch[] = {0, 0};
107+
cv::Mat imgL(mImg.size(), CV_8UC1);
108+
mixChannels(&imgLab, 1, &imgL, 1, ch, 1);
109+
cv::normalize(imgL, imgL, 255, 0, cv::NORM_MINMAX);
110+
imgLab.release(); // early release
111+
112+
findRectangles(imgL, mRects);
113+
}
101114

102115

103116
std::cout << "[DkPageSegmentation] " << mRects.size() << " rectangles found resize factor: " << scale << std::endl;
@@ -123,8 +136,8 @@ void DkPageSegmentation::findRectanglesLab(const cv::Mat & img, std::vector<DkPo
123136

124137
void DkPageSegmentation::findRectangles(const cv::Mat& img, std::vector<DkPolyRect>& rects) const {
125138

126-
cv::Mat imgL;
127-
cv::normalize(img, imgL, 255, 0, cv::NORM_MINMAX);
139+
cv::Mat imgL;
140+
cv::normalize(img, imgL, 255, 0, cv::NORM_MINMAX);
128141

129142
// downscale
130143
if (scale != 1.0f)
@@ -411,13 +424,13 @@ void DkPageSegmentation::draw(cv::Mat& img, const std::vector<DkPolyRect>& rects
411424
}
412425
}
413426

414-
std::vector<DkPolyRect> DkPageSegmentation::apply(const cv::Mat& src) {
427+
std::vector<DkPolyRect> DkPageSegmentation::apply(const cv::Mat& src, bool useLab) {
415428

416429
std::vector<DkPolyRect> pageRects;
417430

418431
// run the page segmentation
419432
DkPageSegmentation segM(src);
420-
segM.compute();
433+
segM.compute(useLab);
421434
segM.filterDuplicates();
422435

423436
//pageRects = segM.getRects();

app/src/main/jni/PageSegmentation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class DllCoreExport DkPageSegmentation {
4747
public:
4848
DkPageSegmentation(const cv::Mat& colImg = cv::Mat());
4949

50-
virtual void compute();
50+
virtual void compute(bool useLab);
5151
virtual void filterDuplicates(float overlap = 0.6f, float areaRatio = 0.5f);
5252
virtual void filterDuplicates(std::vector<DkPolyRect>& rects, float overlap = 0.6f, float areaRatio = 0.1f) const;
5353

@@ -60,7 +60,7 @@ class DllCoreExport DkPageSegmentation {
6060
DkPolyRect getMaxRect() const;
6161
DkPolyRect getDocumentRect() const;
6262

63-
static std::vector<DkPolyRect> apply(const cv::Mat& src);
63+
static std::vector<DkPolyRect> apply(const cv::Mat& src, bool useLab = true);
6464

6565
protected:
6666
cv::Mat mImg;

app/src/main/res/menu/drawer_view_menu.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@
2020
android:title="@string/show_guide_text" />
2121
</group>
2222

23+
<group android:checkableBehavior="single">
24+
<item
25+
android:id="@+id/action_precise_page_seg"
26+
android:title="@string/fast_page_seg_text" />
27+
</group>
28+
2329
</menu>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<string name="show_fm_values_text">Show focus values</string>
1010
<string name="show_guide_text">Show guide lines</string>
1111
<string name="hide_guide_text">Hide guide lines</string>
12+
<string name="fast_page_seg_text">Use fast page segmentation</string>
13+
<string name="precise_page_seg_text">Use precise page segmentation</string>
1214
<string name="hide_fm_values_text">Hide focus values</string>
1315
<string name="taking_picture_text">Taking picture</string>
1416

0 commit comments

Comments
 (0)