Skip to content

Commit 3a37319

Browse files
committed
Full source code of book on 8th Dec 2012
1 parent 15306e2 commit 3a37319

321 files changed

Lines changed: 184039 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
4+
<classpathentry kind="lib" path="/OpenCV-2.4.2/bin/opencv-2.4.2.jar"/>
5+
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
6+
<classpathentry kind="src" path="src"/>
7+
<classpathentry kind="src" path="gen"/>
8+
<classpathentry kind="output" path="bin/classes"/>
9+
</classpath>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?fileVersion 4.0.0?>
3+
4+
<cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
5+
<storageModule moduleId="org.eclipse.cdt.core.settings">
6+
<cconfiguration id="0.1521208700">
7+
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="0.1521208700" moduleId="org.eclipse.cdt.core.settings" name="Default">
8+
<externalSettings/>
9+
<extensions>
10+
<extension id="org.eclipse.cdt.core.VCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
11+
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
12+
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
13+
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
14+
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
15+
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
16+
</extensions>
17+
</storageModule>
18+
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
19+
<configuration artifactName="${ProjName}" buildProperties="" description="" id="0.1521208700" name="Default" parent="org.eclipse.cdt.build.core.prefbase.cfg">
20+
<folderInfo id="0.1521208700." name="/" resourcePath="">
21+
<toolChain id="org.eclipse.cdt.build.core.prefbase.toolchain.644195364" name="No ToolChain" resourceTypeBasedDiscovery="false" superClass="org.eclipse.cdt.build.core.prefbase.toolchain">
22+
<targetPlatform id="org.eclipse.cdt.build.core.prefbase.toolchain.644195364.845035839" name=""/>
23+
<builder arguments="${NDKROOT}/ndk-build V=0" command="sh" id="org.eclipse.cdt.build.core.settings.default.builder.1987118767" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="org.eclipse.cdt.build.core.settings.default.builder"/>
24+
<tool id="org.eclipse.cdt.build.core.settings.holder.libs.2081373699" name="holder for library settings" superClass="org.eclipse.cdt.build.core.settings.holder.libs"/>
25+
<tool id="org.eclipse.cdt.build.core.settings.holder.433559962" name="Assembly" superClass="org.eclipse.cdt.build.core.settings.holder">
26+
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.1980844946" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
27+
<listOptionValue builtIn="false" value="/usr/local/include"/>
28+
<listOptionValue builtIn="false" value="/usr/local/include/opencv"/>
29+
</option>
30+
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1274803686" languageId="org.eclipse.cdt.core.assembly" languageName="Assembly" sourceContentType="org.eclipse.cdt.core.asmSource" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
31+
</tool>
32+
<tool id="org.eclipse.cdt.build.core.settings.holder.1688292960" name="GNU C++" superClass="org.eclipse.cdt.build.core.settings.holder">
33+
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.2137969173" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
34+
<listOptionValue builtIn="false" value="/usr/local/include"/>
35+
<listOptionValue builtIn="false" value="/usr/local/include/opencv"/>
36+
</option>
37+
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.86516288" languageId="org.eclipse.cdt.core.g++" languageName="GNU C++" sourceContentType="org.eclipse.cdt.core.cxxSource,org.eclipse.cdt.core.cxxHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
38+
</tool>
39+
<tool id="org.eclipse.cdt.build.core.settings.holder.607536332" name="GNU C" superClass="org.eclipse.cdt.build.core.settings.holder">
40+
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.628867588" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
41+
<listOptionValue builtIn="false" value="/usr/local/include"/>
42+
<listOptionValue builtIn="false" value="/usr/local/include/opencv"/>
43+
</option>
44+
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.908330048" languageId="org.eclipse.cdt.core.gcc" languageName="GNU C" sourceContentType="org.eclipse.cdt.core.cSource,org.eclipse.cdt.core.cHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
45+
</tool>
46+
</toolChain>
47+
</folderInfo>
48+
</configuration>
49+
</storageModule>
50+
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
51+
</cconfiguration>
52+
</storageModule>
53+
<storageModule moduleId="org.eclipse.cdt.core.pathentry"/>
54+
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
55+
<project id="Cartoonifier.null.556056689" name="Cartoonifier"/>
56+
</storageModule>
57+
<storageModule moduleId="scannerConfiguration">
58+
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
59+
<scannerConfigBuildInfo instanceId="0.1521208700">
60+
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
61+
</scannerConfigBuildInfo>
62+
</storageModule>
63+
<storageModule moduleId="refreshScope" versionNumber="1">
64+
<resource resourceType="PROJECT" workspacePath="/Cartoonifier"/>
65+
</storageModule>
66+
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>
67+
</cproject>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Cartoonifier</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
10+
<triggers>clean,full,incremental,</triggers>
11+
<arguments>
12+
<dictionary>
13+
<key>?name?</key>
14+
<value></value>
15+
</dictionary>
16+
<dictionary>
17+
<key>org.eclipse.cdt.make.core.append_environment</key>
18+
<value>true</value>
19+
</dictionary>
20+
<dictionary>
21+
<key>org.eclipse.cdt.make.core.autoBuildTarget</key>
22+
<value>all</value>
23+
</dictionary>
24+
<dictionary>
25+
<key>org.eclipse.cdt.make.core.buildArguments</key>
26+
<value>${NDKROOT}/ndk-build V=0</value>
27+
</dictionary>
28+
<dictionary>
29+
<key>org.eclipse.cdt.make.core.buildCommand</key>
30+
<value>sh</value>
31+
</dictionary>
32+
<dictionary>
33+
<key>org.eclipse.cdt.make.core.cleanBuildTarget</key>
34+
<value>clean</value>
35+
</dictionary>
36+
<dictionary>
37+
<key>org.eclipse.cdt.make.core.contents</key>
38+
<value>org.eclipse.cdt.make.core.activeConfigSettings</value>
39+
</dictionary>
40+
<dictionary>
41+
<key>org.eclipse.cdt.make.core.enableAutoBuild</key>
42+
<value>false</value>
43+
</dictionary>
44+
<dictionary>
45+
<key>org.eclipse.cdt.make.core.enableCleanBuild</key>
46+
<value>true</value>
47+
</dictionary>
48+
<dictionary>
49+
<key>org.eclipse.cdt.make.core.enableFullBuild</key>
50+
<value>true</value>
51+
</dictionary>
52+
<dictionary>
53+
<key>org.eclipse.cdt.make.core.fullBuildTarget</key>
54+
<value>all</value>
55+
</dictionary>
56+
<dictionary>
57+
<key>org.eclipse.cdt.make.core.stopOnError</key>
58+
<value>true</value>
59+
</dictionary>
60+
<dictionary>
61+
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
62+
<value>false</value>
63+
</dictionary>
64+
</arguments>
65+
</buildCommand>
66+
<buildCommand>
67+
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
68+
<arguments>
69+
</arguments>
70+
</buildCommand>
71+
<buildCommand>
72+
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
73+
<arguments>
74+
</arguments>
75+
</buildCommand>
76+
<buildCommand>
77+
<name>org.eclipse.jdt.core.javabuilder</name>
78+
<arguments>
79+
</arguments>
80+
</buildCommand>
81+
<buildCommand>
82+
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
83+
<arguments>
84+
</arguments>
85+
</buildCommand>
86+
<buildCommand>
87+
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
88+
<triggers>full,incremental,</triggers>
89+
<arguments>
90+
</arguments>
91+
</buildCommand>
92+
</buildSpec>
93+
<natures>
94+
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
95+
<nature>org.eclipse.jdt.core.javanature</nature>
96+
<nature>org.eclipse.cdt.core.cnature</nature>
97+
<nature>org.eclipse.cdt.core.ccnature</nature>
98+
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
99+
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
100+
</natures>
101+
</projectDescription>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Wed Jul 04 06:41:49 PDT 2012
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.5
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.5
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.Cartoonifier"
4+
android:versionCode="1"
5+
android:versionName="1.0">
6+
7+
<supports-screens android:resizeable="true"
8+
android:smallScreens="true"
9+
android:normalScreens="true"
10+
android:largeScreens="true"
11+
android:anyDensity="true" />
12+
13+
<application android:label="@string/app_name" android:icon="@drawable/icon">
14+
<activity android:name="CartoonifierApp"
15+
android:label="@string/app_name"
16+
android:screenOrientation="landscape"
17+
android:configChanges="keyboardHidden|orientation">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN" />
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
<uses-sdk android:minSdkVersion="8" />
26+
27+
<uses-permission android:name="android.permission.CAMERA"/>
28+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
29+
<uses-feature android:name="android.hardware.camera" />
30+
<uses-feature android:name="android.hardware.camera.autofocus" />
31+
32+
</manifest>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#/****************************************************************************
2+
#* Cartoonifier, for Android.
3+
#*****************************************************************************
4+
#* by Shervin Emami, 5th Dec 2012 ([email protected])
5+
#* http://www.shervinemami.info/
6+
#*****************************************************************************
7+
#* Ch1 of the book "Mastering OpenCV with Practical Computer Vision Projects"
8+
#* Copyright Packt Publishing 2012.
9+
#* http://www.packtpub.com/cool-projects-with-opencv/book
10+
#****************************************************************************/
11+
12+
13+
LOCAL_PATH := $(call my-dir)
14+
15+
include $(CLEAR_VARS)
16+
17+
OPENCV_LIB_TYPE:=STATIC
18+
OPENCV_INSTALL_MODULES:=on
19+
20+
# Path to OpenCV.mk file, which is generated when you build OpenCV for Android.
21+
# include C:\OpenCV\android\build\OpenCV.mk
22+
# include ~/OpenCV/android/build/OpenCV.mk
23+
include ../includeOpenCV.mk
24+
ifeq ("$(wildcard $(OPENCV_MK_PATH))","")
25+
#try to load OpenCV.mk from default install location
26+
include $(TOOLCHAIN_PREBUILT_ROOT)/user/share/OpenCV/OpenCV.mk
27+
else
28+
include $(OPENCV_MK_PATH)
29+
endif
30+
31+
LOCAL_MODULE := cartoonifier
32+
LOCAL_LDLIBS += -llog -ldl
33+
34+
# Since we have source + headers files in an external folder, we need to show where they are.
35+
LOCAL_SRC_FILES := jni_part.cpp
36+
LOCAL_SRC_FILES += ../../Cartoonifier_Desktop/cartoon.cpp
37+
LOCAL_SRC_FILES += ../../Cartoonifier_Desktop/ImageUtils_0.7.cpp
38+
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Cartoonifier_Desktop
39+
40+
41+
include $(BUILD_SHARED_LIBRARY)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
APP_STL := gnustl_static
2+
APP_CPPFLAGS := -frtti -fexceptions
3+
APP_ABI := armeabi-v7a
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*****************************************************************************
2+
* Cartoonifier, for Android.
3+
******************************************************************************
4+
* by Shervin Emami, 5th Dec 2012 ([email protected])
5+
* http://www.shervinemami.info/
6+
******************************************************************************
7+
* Ch1 of the book "Mastering OpenCV with Practical Computer Vision Projects"
8+
* Copyright Packt Publishing 2012.
9+
* http://www.packtpub.com/cool-projects-with-opencv/book
10+
*****************************************************************************/
11+
12+
#include <jni.h>
13+
#include <opencv2/core/core.hpp>
14+
#include <opencv2/imgproc/imgproc.hpp>
15+
#include <opencv2/features2d/features2d.hpp>
16+
17+
18+
#include "cartoon.h"
19+
#include "ImageUtils.h" // Handy functions for debugging OpenCV images, by Shervin Emami.
20+
21+
22+
using namespace std;
23+
using namespace cv;
24+
25+
extern "C" {
26+
27+
28+
// Just show the plain camera image without modifying it.
29+
JNIEXPORT void JNICALL Java_com_Cartoonifier_CartoonifierView_ShowPreview(JNIEnv* env, jobject,
30+
jint width, jint height, jbyteArray yuv, jintArray bgra)
31+
{
32+
// Get native access to the given Java arrays.
33+
jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
34+
jint* _bgra = env->GetIntArrayElements(bgra, 0);
35+
36+
// Prepare a cv::Mat that points to the YUV420sp data.
37+
Mat myuv(height + height/2, width, CV_8UC1, (uchar *)_yuv);
38+
// Prepare a cv::Mat that points to the BGRA output data.
39+
Mat mbgra(height, width, CV_8UC4, (uchar *)_bgra);
40+
41+
// Convert the color format from the camera's
42+
// NV21 "YUV420sp" format to an Android BGRA color image.
43+
cvtColor(myuv, mbgra, CV_YUV420sp2BGRA);
44+
45+
// OpenCV can now access/modify the BGRA image if we want ...
46+
47+
48+
// Release the native lock we placed on the Java arrays.
49+
env->ReleaseIntArrayElements(bgra, _bgra, 0);
50+
env->ReleaseByteArrayElements(yuv, _yuv, 0);
51+
}
52+
53+
54+
DECLARE_TIMING(CartoonifyImage);
55+
56+
57+
// Modify the camera image using the Cartoonifier filter.
58+
JNIEXPORT void JNICALL Java_com_Cartoonifier_CartoonifierView_CartoonifyImage(JNIEnv* env, jobject,
59+
jint width, jint height, jbyteArray yuv, jintArray bgra,
60+
jboolean sketchMode, jboolean alienMode, jboolean evilMode, jboolean debugMode)
61+
{
62+
START_TIMING(CartoonifyImage);
63+
64+
// Get native access to the given Java arrays.
65+
jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
66+
jint* _bgra = env->GetIntArrayElements(bgra, 0);
67+
68+
// Input color format (from camera):
69+
// "myuv" is the color image in the camera's native NV21 YUV 420 "semi-planar" format, which means
70+
// the first part of the array is the grayscale pixel array, followed by a quarter-sized pixel
71+
// array that is the U & V color channels interleaved. So if we just want to access a grayscale
72+
// image, we can get it directly from the 1st part of a YUV420sp semi-planar image without any
73+
// conversions. But if we want a color image (eg: BGRA color format that is recommended for OpenCV
74+
// on Android), then we must convert the color format using cvtColor().
75+
Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv); // Wrapper around the _yuv data.
76+
Mat mgray(height, width, CV_8UC1, (unsigned char *)_yuv); // Also a wrapper around the _yuv data.
77+
78+
// Output color format (for display):
79+
// "mbgra" is the color image to be displayed on the Android device, in BGRA format (ie: OpenCV's
80+
// default BGR which is RGB but in the opposite byte order, but with an extra 0 byte on the end
81+
// of each pixel, so that each pixel is stored as Blue, Green, Red, 0). You can either do all
82+
// your processing in OpenCV's default BGR format and then convert your final output from BGR to
83+
// BGRA before display on the screen, or ideally you can ensure your image processing code can
84+
// handle BGRA format instead of or in addition to BGR format. This is particularly important if
85+
// you try to access pixels directly in the image!
86+
Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);
87+
88+
// Convert the color format from the camera's YUV420sp semi-planar format to a regular BGR color image.
89+
Mat mbgr(height, width, CV_8UC3); // Allocate a new image buffer.
90+
cvtColor(myuv, mbgr, CV_YUV420sp2BGR);
91+
92+
93+
//--- Beginning of custom C/C++ image processing with OpenCV ---//
94+
Mat displayedFrame(mbgra.size(), CV_8UC3);
95+
96+
// Use debug type 1 (for mobile) if debug mode is enabled, since we can't show popup GUI windows.
97+
int debugType = 0;
98+
if (debugMode)
99+
debugType = 1;
100+
101+
// Do the C/C++ image processing.
102+
cartoonifyImage(mbgr, displayedFrame, sketchMode, alienMode, evilMode, debugType);
103+
104+
// Convert back from OpenCV's BGR format to Android's BGRA format, unless if we can handle BGRA in our code.
105+
cvtColor(displayedFrame, mbgra, CV_BGR2BGRA);
106+
//--- End of custom C/C++ image processing with OpenCV ---//
107+
108+
109+
// Release the native lock we placed on the Java arrays.
110+
env->ReleaseIntArrayElements(bgra, _bgra, 0);
111+
env->ReleaseByteArrayElements(yuv, _yuv, 0);
112+
113+
STOP_TIMING(CartoonifyImage);
114+
// Print the timing info.
115+
SHOW_TIMING(CartoonifyImage, "CartoonifyImage");
116+
}
117+
118+
119+
120+
}//end of extern "C" (global C/C++ functions that aren't part of a C++ Class)

0 commit comments

Comments
 (0)