Skip to content

Commit

Permalink
Full source code of book on 8th Dec 2012
Browse files Browse the repository at this point in the history
  • Loading branch information
shervinemami committed Dec 9, 2012
1 parent 15306e2 commit 3a37319
Show file tree
Hide file tree
Showing 321 changed files with 184,039 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Chapter1_AndroidCartoonifier/Cartoonifier_Android/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="lib" path="/OpenCV-2.4.2/bin/opencv-2.4.2.jar"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
67 changes: 67 additions & 0 deletions Chapter1_AndroidCartoonifier/Cartoonifier_Android/.cproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?>

<cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="0.1521208700">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="0.1521208700" moduleId="org.eclipse.cdt.core.settings" name="Default">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.VCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildProperties="" description="" id="0.1521208700" name="Default" parent="org.eclipse.cdt.build.core.prefbase.cfg">
<folderInfo id="0.1521208700." name="/" resourcePath="">
<toolChain id="org.eclipse.cdt.build.core.prefbase.toolchain.644195364" name="No ToolChain" resourceTypeBasedDiscovery="false" superClass="org.eclipse.cdt.build.core.prefbase.toolchain">
<targetPlatform id="org.eclipse.cdt.build.core.prefbase.toolchain.644195364.845035839" name=""/>
<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"/>
<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"/>
<tool id="org.eclipse.cdt.build.core.settings.holder.433559962" name="Assembly" superClass="org.eclipse.cdt.build.core.settings.holder">
<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">
<listOptionValue builtIn="false" value="/usr/local/include"/>
<listOptionValue builtIn="false" value="/usr/local/include/opencv"/>
</option>
<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"/>
</tool>
<tool id="org.eclipse.cdt.build.core.settings.holder.1688292960" name="GNU C++" superClass="org.eclipse.cdt.build.core.settings.holder">
<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">
<listOptionValue builtIn="false" value="/usr/local/include"/>
<listOptionValue builtIn="false" value="/usr/local/include/opencv"/>
</option>
<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"/>
</tool>
<tool id="org.eclipse.cdt.build.core.settings.holder.607536332" name="GNU C" superClass="org.eclipse.cdt.build.core.settings.holder">
<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">
<listOptionValue builtIn="false" value="/usr/local/include"/>
<listOptionValue builtIn="false" value="/usr/local/include/opencv"/>
</option>
<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"/>
</tool>
</toolChain>
</folderInfo>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.pathentry"/>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="Cartoonifier.null.556056689" name="Cartoonifier"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="0.1521208700">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="refreshScope" versionNumber="1">
<resource resourceType="PROJECT" workspacePath="/Cartoonifier"/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>
</cproject>
101 changes: 101 additions & 0 deletions Chapter1_AndroidCartoonifier/Cartoonifier_Android/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Cartoonifier</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
<dictionary>
<key>?name?</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.append_environment</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.autoBuildTarget</key>
<value>all</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildArguments</key>
<value>${NDKROOT}/ndk-build V=0</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildCommand</key>
<value>sh</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.cleanBuildTarget</key>
<value>clean</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.contents</key>
<value>org.eclipse.cdt.make.core.activeConfigSettings</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableAutoBuild</key>
<value>false</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableCleanBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableFullBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.fullBuildTarget</key>
<value>all</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.stopOnError</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
<value>false</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#Wed Jul 04 06:41:49 PDT 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.5
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Cartoonifier"
android:versionCode="1"
android:versionName="1.0">

<supports-screens android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />

<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name="CartoonifierApp"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

</manifest>
41 changes: 41 additions & 0 deletions Chapter1_AndroidCartoonifier/Cartoonifier_Android/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#/****************************************************************************
#* Cartoonifier, for Android.
#*****************************************************************************
#* by Shervin Emami, 5th Dec 2012 ([email protected])
#* http://www.shervinemami.info/
#*****************************************************************************
#* Ch1 of the book "Mastering OpenCV with Practical Computer Vision Projects"
#* Copyright Packt Publishing 2012.
#* http://www.packtpub.com/cool-projects-with-opencv/book
#****************************************************************************/


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

OPENCV_LIB_TYPE:=STATIC
OPENCV_INSTALL_MODULES:=on

# Path to OpenCV.mk file, which is generated when you build OpenCV for Android.
# include C:\OpenCV\android\build\OpenCV.mk
# include ~/OpenCV/android/build/OpenCV.mk
include ../includeOpenCV.mk
ifeq ("$(wildcard $(OPENCV_MK_PATH))","")
#try to load OpenCV.mk from default install location
include $(TOOLCHAIN_PREBUILT_ROOT)/user/share/OpenCV/OpenCV.mk
else
include $(OPENCV_MK_PATH)
endif

LOCAL_MODULE := cartoonifier
LOCAL_LDLIBS += -llog -ldl

# Since we have source + headers files in an external folder, we need to show where they are.
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_SRC_FILES += ../../Cartoonifier_Desktop/cartoon.cpp
LOCAL_SRC_FILES += ../../Cartoonifier_Desktop/ImageUtils_0.7.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Cartoonifier_Desktop


include $(BUILD_SHARED_LIBRARY)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
120 changes: 120 additions & 0 deletions Chapter1_AndroidCartoonifier/Cartoonifier_Android/jni/jni_part.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*****************************************************************************
* Cartoonifier, for Android.
******************************************************************************
* by Shervin Emami, 5th Dec 2012 ([email protected])
* http://www.shervinemami.info/
******************************************************************************
* Ch1 of the book "Mastering OpenCV with Practical Computer Vision Projects"
* Copyright Packt Publishing 2012.
* http://www.packtpub.com/cool-projects-with-opencv/book
*****************************************************************************/

#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>


#include "cartoon.h"
#include "ImageUtils.h" // Handy functions for debugging OpenCV images, by Shervin Emami.


using namespace std;
using namespace cv;

extern "C" {


// Just show the plain camera image without modifying it.
JNIEXPORT void JNICALL Java_com_Cartoonifier_CartoonifierView_ShowPreview(JNIEnv* env, jobject,
jint width, jint height, jbyteArray yuv, jintArray bgra)
{
// Get native access to the given Java arrays.
jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
jint* _bgra = env->GetIntArrayElements(bgra, 0);

// Prepare a cv::Mat that points to the YUV420sp data.
Mat myuv(height + height/2, width, CV_8UC1, (uchar *)_yuv);
// Prepare a cv::Mat that points to the BGRA output data.
Mat mbgra(height, width, CV_8UC4, (uchar *)_bgra);

// Convert the color format from the camera's
// NV21 "YUV420sp" format to an Android BGRA color image.
cvtColor(myuv, mbgra, CV_YUV420sp2BGRA);

// OpenCV can now access/modify the BGRA image if we want ...


// Release the native lock we placed on the Java arrays.
env->ReleaseIntArrayElements(bgra, _bgra, 0);
env->ReleaseByteArrayElements(yuv, _yuv, 0);
}


DECLARE_TIMING(CartoonifyImage);


// Modify the camera image using the Cartoonifier filter.
JNIEXPORT void JNICALL Java_com_Cartoonifier_CartoonifierView_CartoonifyImage(JNIEnv* env, jobject,
jint width, jint height, jbyteArray yuv, jintArray bgra,
jboolean sketchMode, jboolean alienMode, jboolean evilMode, jboolean debugMode)
{
START_TIMING(CartoonifyImage);

// Get native access to the given Java arrays.
jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
jint* _bgra = env->GetIntArrayElements(bgra, 0);

// Input color format (from camera):
// "myuv" is the color image in the camera's native NV21 YUV 420 "semi-planar" format, which means
// the first part of the array is the grayscale pixel array, followed by a quarter-sized pixel
// array that is the U & V color channels interleaved. So if we just want to access a grayscale
// image, we can get it directly from the 1st part of a YUV420sp semi-planar image without any
// conversions. But if we want a color image (eg: BGRA color format that is recommended for OpenCV
// on Android), then we must convert the color format using cvtColor().
Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv); // Wrapper around the _yuv data.
Mat mgray(height, width, CV_8UC1, (unsigned char *)_yuv); // Also a wrapper around the _yuv data.

// Output color format (for display):
// "mbgra" is the color image to be displayed on the Android device, in BGRA format (ie: OpenCV's
// default BGR which is RGB but in the opposite byte order, but with an extra 0 byte on the end
// of each pixel, so that each pixel is stored as Blue, Green, Red, 0). You can either do all
// your processing in OpenCV's default BGR format and then convert your final output from BGR to
// BGRA before display on the screen, or ideally you can ensure your image processing code can
// handle BGRA format instead of or in addition to BGR format. This is particularly important if
// you try to access pixels directly in the image!
Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);

// Convert the color format from the camera's YUV420sp semi-planar format to a regular BGR color image.
Mat mbgr(height, width, CV_8UC3); // Allocate a new image buffer.
cvtColor(myuv, mbgr, CV_YUV420sp2BGR);


//--- Beginning of custom C/C++ image processing with OpenCV ---//
Mat displayedFrame(mbgra.size(), CV_8UC3);

// Use debug type 1 (for mobile) if debug mode is enabled, since we can't show popup GUI windows.
int debugType = 0;
if (debugMode)
debugType = 1;

// Do the C/C++ image processing.
cartoonifyImage(mbgr, displayedFrame, sketchMode, alienMode, evilMode, debugType);

// Convert back from OpenCV's BGR format to Android's BGRA format, unless if we can handle BGRA in our code.
cvtColor(displayedFrame, mbgra, CV_BGR2BGRA);
//--- End of custom C/C++ image processing with OpenCV ---//


// Release the native lock we placed on the Java arrays.
env->ReleaseIntArrayElements(bgra, _bgra, 0);
env->ReleaseByteArrayElements(yuv, _yuv, 0);

STOP_TIMING(CartoonifyImage);
// Print the timing info.
SHOW_TIMING(CartoonifyImage, "CartoonifyImage");
}



}//end of extern "C" (global C/C++ functions that aren't part of a C++ Class)
Loading

0 comments on commit 3a37319

Please sign in to comment.