1 | #include "opencv2/imgproc/imgproc.hpp"
|
2 | #include "opencv2/highgui/highgui.hpp"
|
3 | #include <cv.h>
|
4 | #include <highgui.h>
|
5 | #include "BlobResult.h"
|
6 | #include <jni.h>
|
7 |
|
8 | using namespace cv;
|
9 | using namespace std;
|
10 |
|
11 | char *filepath1 = "/sdcard/CLOWN-FACE.jpg";
|
12 | double alpha = 0.2;
|
13 | double beta = 1.0 - alpha;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | extern "C" {
|
25 | JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial3_Sample3View_FindFeatures(JNIEnv* env, jobject thiz, jint width, jint height, jbyteArray yuv, jintArray bgra)
|
26 | {
|
27 | int col,row;
|
28 | jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
|
29 | jint* _bgra = env->GetIntArrayElements(bgra, 0);
|
30 |
|
31 | Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv);
|
32 | Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);
|
39 | IplImage imageBGR = mbgra;
|
40 |
|
41 | IplImage* imageHSV = cvCreateImage( cvGetSize(&imageBGR), 8, 3);
|
42 | cvCvtColor(&imageBGR, imageHSV, CV_BGR2HSV);
|
43 |
|
44 |
|
45 | IplImage* planeH = cvCreateImage( cvGetSize(&imageBGR), 8, 1);
|
46 | IplImage* planeS = cvCreateImage( cvGetSize(&imageBGR), 8, 1);
|
47 | IplImage* planeV = cvCreateImage( cvGetSize(&imageBGR), 8, 1);
|
48 | cvCvtPixToPlane(imageHSV, planeH, planeS, planeV, 0);
|
49 | IplImage* tempPlaneH = cvCreateImage( cvGetSize(&imageBGR), 8, 1);
|
50 | cvCopy(planeH,tempPlaneH);
|
51 |
|
52 |
|
53 |
|
54 | cvInRangeS(tempPlaneH, cvScalarAll(0), cvScalarAll(18), tempPlaneH);
|
55 | cvInRangeS(planeH, cvScalarAll(170), cvScalarAll(179), planeH);
|
56 | cvThreshold(planeS, planeS, 30, UCHAR_MAX, CV_THRESH_BINARY);
|
57 | cvThreshold(planeV, planeV, 35, UCHAR_MAX, CV_THRESH_BINARY);
|
58 | cvOr(tempPlaneH,planeH,planeH);
|
59 |
|
60 |
|
61 |
|
62 | IplImage* imageSkinPixels = cvCreateImage( cvGetSize(&imageBGR), 8, 1);
|
63 | cvAnd(planeH, planeS, imageSkinPixels);
|
64 | cvAnd(imageSkinPixels, planeV, imageSkinPixels);
|
65 |
|
66 |
|
67 | CBlobResult blobs;
|
68 | CBlob biggestBlob;
|
69 |
|
70 | blobs = CBlobResult(imageSkinPixels, NULL, 0);
|
71 | int minArea = 2000;
|
72 | blobs.Filter(blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, minArea);
|
73 | blobs.GetNthBlob( CBlobGetArea(), 0, biggestBlob );
|
74 |
|
75 | CvRect faces1;
|
76 | faces1 = biggestBlob.GetBoundingBox();
|
77 |
|
78 | if (faces1.height>(1.3*faces1.width) && faces1.height<(1.7*faces1.width))
|
79 | {
|
80 | cvRectangle( &imageBGR,cvPoint( faces1.x, faces1.y ), cvPoint( faces1.x + faces1.width, faces1.y + faces1.height ),CV_RGB( 255, 0, 0 ), 10, 8, 0 );
|
81 |
|
82 |
|
83 | cvSetImageROI(&imageBGR,faces1);
|
84 | IplImage *temp1 = cvLoadImage(filepath1);
|
85 | IplImage *temp2 = cvCreateImage(cvSize(faces1.width,faces1.height),imageBGR.depth,imageBGR.nChannels);
|
86 | cvResize(temp1,temp2);
|
87 |
|
88 | cvAddWeighted(&imageBGR,alpha,temp2,beta,0.0,&imageBGR);
|
89 | cvResetImageROI(&imageBGR);
|
90 |
|
91 | }
|
92 |
|
93 |
|
94 | cvReleaseImage( &imageHSV );
|
95 | cvReleaseImage( &tempPlaneH );
|
96 | cvReleaseImage( &planeH );
|
97 | cvReleaseImage( &planeS );
|
98 | cvReleaseImage( &planeV );
|
99 | cvReleaseImage( &imageSkinPixels );
|
100 |
|
101 |
|
102 | env->ReleaseIntArrayElements(bgra, _bgra, 0);
|
103 | env->ReleaseByteArrayElements(yuv, _yuv, 0);
|
104 |
|
105 |
|
106 | |
107 | int i;
|
108 | storage = cvCreateMemStorage( 0 );
|
109 | jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
|
110 | jint* _bgra = env->GetIntArrayElements(bgra, 0);
|
111 |
|
112 | Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv);
|
113 | Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);
|
114 |
|
115 | //Please make attention about BGRA byte order
|
116 | //ARGB stored in java as int array becomes BGRA at native level
|
117 |
|
118 | cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);
|
119 |
|
120 |
|
121 |
|
122 | CvSeq *faces = cvHaarDetectObjects(&imgbgra, cascadeFile,storage,1.3, 3, 0 , cvSize(0,0), cvSize(0,0));
|
123 | for( i= 0 ; i < ( faces ? faces->total : 0 ) ; i++ )
|
124 | {
|
125 | CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
|
126 | cvRectangle( &imgbgra,cvPoint( r->x, r->y ), cvPoint( r->x + r->width, r->y + r->height ),CV_RGB( 255, 0, 0 ), 10, 8, 0 );
|
127 | }
|
128 |
|
129 |
|
130 | env->ReleaseIntArrayElements(bgra, _bgra, 0);
|
131 | env->ReleaseByteArrayElements(yuv, _yuv, 0);
|
132 | /*jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
|
133 | jint* _bgra = env->GetIntArrayElements(bgra, 0);
|
134 |
|
135 | Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv);
|
136 | Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);
|
137 | Mat mgray(height, width, CV_8UC1, (unsigned char *)_yuv);
|
138 |
|
139 | //Please make attention about BGRA byte order
|
140 | //ARGB stored in java as int array becomes BGRA at native level
|
141 | cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);
|
142 |
|
143 | vector<KeyPoint> v;
|
144 |
|
145 | FastFeatureDetector detector(50);
|
146 | detector.detect(mgray, v);
|
147 | for( size_t i = 0; i < v.size(); i++ )
|
148 | circle(mbgra, Point(v[i].pt.x, v[i].pt.y), 10, Scalar(0,0,255,255));*/
|
149 | #endif
|
150 | }
|
151 |
|
152 | }
|