Top Banner
©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura [email protected]
37

©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura [email protected].

Dec 17, 2015

Download

Documents

Rhoda Rich
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 1

How to Make Hand Detector

Noritsuna [email protected]

Page 2: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 2

Agenda

PreparingBenchmarkHow to Load Files on NativeActivity

How to Make Hand DetectorCalculate Histgram of Skin ColorDetect Skin Area from CapImageCalculate the Largest Skin AreaMatching Histgrams

Page 3: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 3

Hand Detector

Page 4: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 4

Chart of Hand Detector

Calc Histgram of Skin Color

Detect Skin Area from CapImage

Calc the Leargest Skin

Area

Match Histgrams

Histgram

Labeling

Convex Hull

Feature Point

Distance

Page 5: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 5

Mat vs IplImageBenchmark

Page 6: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 6

About Mat & IplImage

cv::MatVersion 2.x and Upper Written by C++

AdvantageEasy to UseFaster

IplImageVersion 1.x and UpperWritten by C

AdvantageMany Documents

1. IplImage* img;2. for(int h = 0; h < img->height; h++)

{3. for(int w = 0; w < img-

>width; w++){4. img-

>imageData[img->widthStep * h + w * 3 + 0]=0;//B

5. img->imageData[img->widthStep * h + w * 3 + 1]=0;//G

6. img->imageData[img->widthStep * h + w * 3 + 2]=0;//R

7. }8. }

1. cv::Mat_<cv::Vec3b> img;2. for (int r = 0; r < img.rows; r++ ) {3. for(int c = 0; c < img.cols;

c++ ) {4. cv::Vec3b &v =

img.at<cv::Vec3b>(r,c);5. v[0] = 0;//B6. v[1] = 0;//G7. v[2] = 0;//R8. }9. }

Page 7: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 7

Benchmark on Android

Gray Scale

Page 8: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 8

How to Load File on NativeActivity

Page 9: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 9

AssetManager

“assets” dir is your resource file dir on Android

“res”(resource) dir is also same. But the file that is there is made “Resource ID” by R file.

Ex. I18n

How to UseNDK with Java

AAssetManager Class (C++)

NativeActivityNo Way……

Page 10: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 10

libassetmanager

assetmanager.hint setupAsset(const char *package_name);

Copy "assets" directory from APK file to under "/data/data/[Package Name]" directory.

int loadAseetFile(const char *package_name, const char *load_file_name);

Copy File of "load_file_name" from APK file to under "/data/data/[Package Name]/assets" directory.1. createAssetFile("assets/images/

skincolorsample.jpg");2. sprintf(file_path, "%s/%s/%s", DATA_PATH,

PACKAGE_NAME, "assets/images/skincolorsample.jpg");

3. skin_color_sample = cvLoadImage(file_path);

Page 11: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 11

How to Make Hand Detector

Page 12: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 12

Hand Detector

Page 13: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 13

Chart of Hand Detector

Calc Histgram of Skin Color

Detect Skin Area from CapImage

Calc the Largest Skin

Area

Matching Histgrams

Histgram

Convex Hull

Labeling

Feature Point

Distance

Page 14: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 14

Calculate Histgram of Skin Color

Page 15: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 15

What’s Histgram?

Frequency Distribution Chart.

Why Use it?For Checking Skin Color.

Each people’s Skin Color is NOT same.

One of Leveling algorithm.

Page 16: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 16

Step 1/2

Convert RGB to HSVRGB color is changed by Light Color.

HueSaturation/ChromaValue/Lightness/Brightness

1. cvCvtColor( src, hsv, CV_BGR2HSV );

2. IplImage* h_plane = cvCreateImage( size, IPL_DEPTH_8U, 1 );

3. IplImage* s_plane = cvCreateImage( size, IPL_DEPTH_8U, 1 );

4. IplImage* v_plane = cvCreateImage( size, IPL_DEPTH_8U, 1 );

Page 17: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 17

Step 2/2

cvCreateHist();Prameter

Dimension of HistgramSizeTypeRange of limitOver limit Use or Not

1. IplImage* planes[] = { h_plane, s_plane };2. *hist = cvCreateHist(2,3. hist_size,4. CV_HIST_ARRAY,5. ranges,6. 1);7. cvCalcHist( planes, *hist, 0, 0 );8. cvMinMaxLoc(v_plane, vmin, vmax);

Page 18: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 18

Detect Skin Area from CapImage

Page 19: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 19

How to Get Skin Area?

Use “Convex Hull” algorithm1. Check Image From Left-Top.2. Found Black Color Pixel is Start Point.3. Search Black Pixel by Right Image.4. Go to Black Pixel that First Found, this is

next point.5. Do 2-4 again, if back to Start Point, get

Convex Hull. ※Convert to Black-White Image

Page 20: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 20

Step 1/3

Delete V(Lightness/Brightness) Color1. Calculate Back Project Image by Skin Color

Histgram.2. Threshold by V(Lightness/Brightness) Color.3. And Operation between Mask and Back

Project.4. Threshold to Back Project. (Adjustment)1. cvCalcBackProject(planes,

backProjectImage, hist);

2. cvThreshold(v_plane, maskImage, *v_min, *v_max, CV_THRESH_BINARY);

3. cvAnd(backProjectImage, maskImage, backProjectImage);

4. cvThreshold(backProjectImage, dstImage, 10, 255, CV_THRESH_BINARY);

Page 21: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 21

Step 2/3

Noise Reduction1. Erode (scale-down)2. Dilate (scale-up)

1. cvErode(dstImage, dstImage, NULL, 1);2. cvDilate(dstImage, dstImage, NULL, 1);

1/4

Page 22: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 22

Step 3/3

Convex HullcvFindContours();

Source ImageConvex that is detectedFirst Convex Pointer that detected

1. cvFindContours(dstImage, storage, &contours);

Page 23: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 23

Calculate the Largest Skin Area

Page 24: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 24

What’s Labeling?

LabelingArea Marking Algorithm.

4-Connection8-Connection

Page 25: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 25

Labeling Algorithm 1/4

1, Scan Image by Raster2, If you got a White Pixel,

1, Check Right Image Pixels2, All “0”, Put the Latest Number + 1 in

Pixel

Page 26: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 26

Labeling Algorithm 2/4

1, If you got a White Pixel, 1, Check Right Image Orange Pixels2, Not “0”,

The Lowest Orange Pixels Number in Pixel

Page 27: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 27

Labeling Algorithm 3/4

1, If got 2 more Number in Orange Pixeles,

1, Put The Lowest Number in Pixel, Change Other Numbers’ “Look up table”

to The Lowest Number.

Page 28: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 28

Labeling Algorithm 4/4

1, After finish, Check “Look up Table”.1, If Dst is NOT Serial Number,

Change to Serial Number2, Src is changed Dst Number.

Page 29: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 29

Get Area Size

cvContourArea();

1. for (CvSeq* c= contours; c != NULL; c = c->h_next){

2. double area = abs(cvContourArea(c, CV_WHOLE_SEQ));

3. if (maxArea < area) {4. maxArea = area;5. hand_ptr = c;6. }7. }

Page 30: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 30

Matching Histgrams

Page 31: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 31

Matching Histgrams

Histgram of Oriented Gradients (HoG)Split Some Area, And Calc Histgram of each Area.

Page 32: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 32

Why Use HoG?

Matching Hand Shape.Use Feature Point Distance with Each HoG.

Page 33: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 33

Step 1/3

Calculate each Cell (Block(3x3) with Edge Pixel(5x5))

luminance gradient momentluminance gradient degree=deg

1. for(int y=0; y<height; y++){2. for(int x=0; x<width; x++){3. if(x==0 || y==0 || x==width-1 ||

y==height-1){4. continue;5. }6. double dx = img->imageData[y*img-

>widthStep+(x+1)] - img->imageData[y*img->widthStep+(x-1)];

7. double dy = img->imageData[(y+1)*img->widthStep+x] - img->imageData[(y-1)*img->widthStep+x];

8. double m = sqrt(dx*dx+dy*dy);9. double deg = (atan2(dy, dx)+CV_PI) * 180.0

/ CV_PI;10. int bin = CELL_BIN * deg/360.0;11. if(bin < 0) bin=0;12. if(bin >= CELL_BIN) bin = CELL_BIN-1;13. hist[(int)(x/CELL_X)][(int)(y/CELL_Y)][bin] +=

m;14. }15. }

Page 34: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 34

Step 2/3

Calculate Feature Vector of Each Block(Go to Next Page)

1. for(int y=0; y<BLOCK_HEIGHT; y++){2. for(int x=0; x<BLOCK_WIDTH; x++){

3. //Calculate Feature Vector in Block4. double vec[BLOCK_DIM];5. memset(vec, 0, BLOCK_DIM*sizeof(double));6. for(int j=0; j<BLOCK_Y; j++){7. for(int i=0; i<BLOCK_X; i++){8. for(int d=0; d<CELL_BIN;

d++){9. int index =

j*(BLOCK_X*CELL_BIN) + i*CELL_BIN + d;10. vec[index] =

hist[x+i][y+j][d];11. }12. }13. }

Page 35: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 35

Step 3/3

(Continued)

Normalize VectorSet Feature Vector

1. //Normalization of Vector2. double norm = 0.0;3. for(int i=0; i<BLOCK_DIM; i++){4. norm += vec[i]*vec[i];5. }6. for(int i=0; i<BLOCK_DIM; i++){7. vec[i] /= sqrt(norm + 1.0);8. }

9. //Put feat10. for(int i=0; i<BLOCK_DIM; i++){11. int index =

y*BLOCK_WIDTH*BLOCK_DIM + x*BLOCK_DIM + i;12. feat[index] = vec[i];13. }14. }15. }

Page 36: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 36

How to Calc Approximation

Calc HoG Distance of each blockGet Average.

Page 37: ©SIProp Project, 2006-2008 1 How to Make Hand Detector Noritsuna Imamura noritsuna@siprop.org.

©SIProp Project, 2006-2008 37

Step 1/1

Calulate Feature Point Distance

1. double dist = 0.0;2. for(int i = 0; i < TOTAL_DIM; i++){3. dist += fabs(feat1[i] -

feat2[i])*fabs(feat1[i] - feat2[i]);4. }5. return sqrt(dist);