#include <jni.h> #include <string> #include <opencv2/opencv.hpp> #include <iostream> #include <vector> #include <android/log.h> #define LOGSHOW(X) __android_log_print(ANDROID_LOG_INFO,X,"PRINTRESULT:::"); // TODO #define POSE_COCO_COLORS_RENDER_GPU \ 255.f, 0.f, 85.f, \ 255.f, 0.f, 0.f, \ 255.f, 85.f, 0.f, \ 255.f, 170.f, 0.f, \ 255.f, 255.f, 0.f, \ 170.f, 255.f, 0.f, \ 85.f, 255.f, 0.f, \ 0.f, 255.f, 0.f, \ 0.f, 255.f, 85.f, \ 0.f, 255.f, 170.f, \ 0.f, 255.f, 255.f, \ 0.f, 170.f, 255.f, \ 0.f, 85.f, 255.f, \ 0.f, 0.f, 255.f, \ 255.f, 0.f, 170.f, \ 170.f, 0.f, 255.f, \ 255.f, 0.f, 255.f, \ 85.f, 0.f, 255.f using namespace cv; using namespace std; extern "C" { void denoising(Mat& mat, Mat& result) { Mat matc = mat.clone(); result.setTo(Scalar(0, 0, 0, 0)); Mat temp; temp.create(mat.rows + 2, mat.cols + 2, mat.type()); int flags = 8 | FLOODFILL_MASK_ONLY | FLOODFILL_FIXED_RANGE; for (int h = 0; h < mat.rows; h++) { for (int w = 0; w < mat.cols; w++) { if (matc.at<uchar>(h, w) == 255) { temp.setTo(Scalar(0, 0, 0, 0)); floodFill(matc, temp, cv::Point(w, h), Scalar(255, 255, 255, 255), 0, Scalar(0, 0, 0, 0), Scalar(0, 0, 0, 0), flags); if (countNonZero(temp) > 6000){ result.setTo(Scalar(255, 255, 255, 255), temp(Rect(1, 1, mat.cols, mat.rows))); } matc.setTo(Scalar(0, 0, 0, 0), temp(Rect(1, 1, mat.cols, mat.rows))); } } } } JNIEXPORT jint JNICALL Java_opencv_zhongke_ktcv_ndk_JniEnter_getDifPoint(JNIEnv *env, jclass type, jlong v1, jlong v2) { Mat *mat1=(Mat*)v1; Mat *mat2=(Mat*)v2; int rows=(*mat1).rows; int cols=(*mat1).cols; int dt=(*mat1).type(); Mat mat3(Size(rows, cols), dt); absdiff(*mat1,*mat2,mat3); threshold(mat3, mat3, 100, 255, 0); /* Mat result; result.create(rows, cols, dt); denoising(mat3,result);*/ jint count=countNonZero(mat3); return count; } JNIEXPORT jlong JNICALL Java_opencv_zhongke_ktcv_ndk_JniEnter_convolution(JNIEnv *env, jclass type, jlong v1, jintArray v_) { jint *b = env->GetIntArrayElements(v_, NULL); Mat *mat=(Mat*)v1; int rows=(*mat).rows; int cols=(*mat).cols; int ntype=(*mat).type(); static Mat mat2(Size(cols, rows), ntype); for (int i = 2; i < rows - 2; i++) { for (int j = 2; j < cols - 2; j++) { int a = (*mat).at<uchar>(i - 1, j - 1); int a1 = (*mat).at<uchar>(i, j - 1); int a2 = (*mat).at<uchar>(i + 1, j - 1); int ba = (*mat).at<uchar>(i - 1, j); int ba1 = (*mat).at<uchar>(i, j); int ba2 = (*mat).at<uchar>(i + 1, j); int ca = (*mat).at<uchar>(i - 1, j + 1); int ca1 = (*mat).at<uchar>(i, j - 1); int ca2 = (*mat).at<uchar>(i + 1, j + 1); int result = a*b[0] + a1*b[1] + a2*b[2] + ba*b[3] + ba1*b[4] + ba2*b[5] + ca*b[6] + ca1*b[7] + ca2*b[8]; if (result >= 0) { result = 255; } if (result < 0) { result = 0; } mat2.at<uchar>(i, j) = result; } } return (jlong)&mat2; } JNIEXPORT jdouble JNICALL Java_opencv_zhongke_ktcv_ndk_JniEnter_compareSimilar(JNIEnv *env, jclass type, jlong v1, jlong v2) { // TODO Mat *mat1 = (Mat *) v1; Mat *mat2 = (Mat *) v2; int r=(*mat1).rows; int c=(*mat1).cols; double a = 0; double sum = r*c; for (size_t i = 0; i < r; i++) { for (size_t j = 0; j < c; j++) { uchar a1 = (*mat1).at<uchar>(i, j); uchar a2 = (*mat2).at<uchar>(i, j); if (a1 == a2) a++; } } return a/sum; } extern "C" JNIEXPORT jint JNICALL Java_opencv_zhongke_ktcv_ndk_JniEnter_nCompareHist(JNIEnv *env, jclass type, jlong v1, jlong v2) { Mat* mat1=(Mat*)v1; Mat* mat2=(Mat*)v2; Mat out1,out2,com1,com2; float h_ranges[] = { 0,256 }; float s_ranges[] = { 0,256 }; const float*ranges[] = { h_ranges,s_ranges }; int h_bins = 50; int s_bins = 60; int histSize[] = { h_bins,s_bins }; int channels[] = { 0,1 }; cvtColor(*mat1, out1, COLOR_BGR2HSV); cvtColor(*mat2, out2, COLOR_BGR2HSV); calcHist(&out1, 1, channels, Mat(), com1, 2, histSize, ranges, true, false); normalize(com1, com1, 0, 1, NORM_MINMAX, -1, Mat()); calcHist(&out2 ,1, channels, Mat(), com2, 2, histSize, ranges, true, false); normalize(com2, com2, 0, 1, NORM_MINMAX, -1, Mat()); double v= compareHist(com1,com2,0); int va=v*100; return va; } extern "C" JNIEXPORT jlong JNICALL Java_opencv_zhongke_ktcv_ndk_JniEnter_equalizeHist(JNIEnv *env, jclass type, jlong v1) { Mat* mat=(Mat*)v1; Mat imageRGB[3]; split(*mat, imageRGB); for (int i = 0; i < 3; i++) { equalizeHist(imageRGB[i], imageRGB[i]); } merge(imageRGB, 3, *mat); return (jlong)&mat; } } float gett(Mat m1,Mat ma2) { float a = 0; for (size_t i = 0; i < m1.rows; i++) { for (size_t j = 0; j < m1.cols; j++) { uchar u = m1.at<uchar>(i, j); uchar u2 = ma2.at<uchar>(i, j); if (u == u2) { a++; } } } float s = m1.rows*ma2.cols; return a / s; } extern "C" JNIEXPORT jfloat JNICALL Java_opencv_zhongke_ktcv_ndk_JniEnter_charCompare(JNIEnv *env, jclass type, jlong v1, jlong v2) { // TODO Mat* mat1=(Mat*)v1; Mat* mat2=(Mat*)v2; Mat sobelx1, sobelx2; Sobel(*mat1, sobelx1, CV_32F, 1, 0); Sobel(*mat2, sobelx2, CV_32F, 1, 0); jfloat a = 0; int rows=(*mat1).rows; int cols=(*mat1).cols; for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < cols; j++) { uchar u = (*mat1).at<uchar>(i, j); uchar u2 = (*mat2).at<uchar>(i, j); if (u == u2) { a++; } } } jfloat s = rows*cols; return a / s; } extern "C" JNIEXPORT jlong JNICALL Java_opencv_zhongke_ktcv_ndk_JniEnter_lplsChange(JNIEnv *env, jclass type, jlong v1) { Mat kernelY =Mat::zeros(Size(3,3),CV_8UC1); Mat* vsd=(Mat*)v1; kernelY.at<float>(0,0)=0; kernelY.at<float>(0,1)=-1; kernelY.at<float>(0,2)=0; kernelY.at<float>(1,0)=0; kernelY.at<float>(1,1)=7; kernelY.at<float>(1,2)=0; kernelY.at<float>(2,0)=-1; kernelY.at<float>(2,1)=-2; kernelY.at<float>(2,2)=-1; } extern "C" #define THREVALUE 100 JNIEXPORT jdouble JNICALL Java_opencv_zhongke_ktcv_ndk_JniEnter_convoSimilar(JNIEnv *env, jclass type, jlong v1, jlong v2) { Mat *mat1 = (Mat *) v1; Mat *mat2 = (Mat *) v2; int r=(*mat1).rows; int c=(*mat1).cols; double a = 0; double sum = r*c; for (size_t i = 0; i < r; i++) { for (size_t j = 0; j < c; j++) { uchar a1 = (*mat1).at<uchar>(i, j); uchar a2 = (*mat2).at<uchar>(i, j); int rst=a1-a2; int rst2=rst>0?rst:-rst; if (rst2>THREVALUE){ a++; } } } return a; } extern "C" JNIEXPORT jlong JNICALL Java_opencv_zhongke_ktcv_ndk_JniEnter_getDifPoint2(JNIEnv *env, jclass type, jlong v1, jlong v2 ) { Mat *mat1=(Mat*)v1; Mat *mat2=(Mat*)v2; int rows=(*mat1).rows; int cols=(*mat1).cols; int dt=(*mat1).type(); static Mat mat3=Mat::zeros(Size(rows,cols),dt); absdiff(*mat1,*mat2,mat3); // threshold(mat3, mat3, va, 255, 0); return (jlong)&mat3; } template<typename T> inline T fastMax(const T a, const T b) { return (a > b ? a : b); } template<typename T> inline int intRound(const T a) { return int(a+0.5f); } int va[34]={1, 2, 1, 5, 2, 3, 3, 4, 5, 6, 6, 7, 1, 8, 8, 9, 9, 10, 1, 11, 11, 12, 12, 13, 1, 0, 0, 14, 14, 16, 0, 15, 15, 17}; const std::vector<unsigned int> POSE_COCO_PAIRS_RENDER(va,va+34); float va2[54]={ 255.f, 0.f, 85.f, 255.f, 0.f, 0.f, 255.f, 85.f, 0.f, 255.f, 170.f, 0.f, 255.f, 255.f, 0.f, 170.f, 255.f, 0.f, 85.f, 255.f, 0.f, 0.f, 255.f, 0.f, 0.f, 255.f, 85.f, 0.f, 255.f, 170.f, 0.f, 255.f, 255.f, 0.f, 170.f, 255.f, 0.f, 85.f, 255.f, 0.f, 0.f, 255.f, 255.f, 0.f, 170.f, 170.f, 0.f, 255.f, 255.f, 0.f, 255.f, 85.f, 0.f, 255.f}; const std::vector<float> POSE_COCO_COLORS_RENDER(va2, va2+54); void renderKeypointsCpu(Mat& frame, const vector<float>& keypoints, vector<int> keyshape, const std::vector<unsigned int>& pairs, const std::vector<float> colors, const float thicknessCircleRatio, const float thicknessLineRatioWRTCircle, const float threshold, float scale) { // Get frame channels { } const auto width = frame.cols; const auto height = frame.rows; const auto area = width * height; // Parameters const auto lineType = 8; const auto shift = 0; const auto numberColors = colors.size(); const auto thresholdRectangle = 0.1f; const auto numberKeypoints = keyshape[1]; // Keypoints for (auto person = 0; person < keyshape[0]; person++) { { const auto ratioAreas = 1; // Size-dependent variables const auto thicknessRatio = fastMax(intRound(std::sqrt(area)*thicknessCircleRatio * ratioAreas), 1); // Negative thickness in cv::circle means that a filled circle is to be drawn. const auto thicknessCircle = (ratioAreas > 0.05 ? thicknessRatio : -1); const auto thicknessLine = 2;// intRound(thicknessRatio * thicknessLineRatioWRTCircle); const auto radius = thicknessRatio / 2; LOGSHOW("11111---------4444444------------") // Draw lines for (auto pair = 0u; pair < pairs.size(); pair += 2) { //pairs=34; // int va[34]={1, 2, 1, 5, 2, 3, 3, 4, 5, 6, 6, 7, 1, 8, 8, 9, 9, 10, 1, 11, 11, 12, 12, 13, 1, 0, 0, 14, 14, 16, 0, 15, 15, 17}; const auto index1 = (person * numberKeypoints + pairs[pair]) * keyshape[2]; const auto index2 = (person * numberKeypoints + pairs[pair + 1]) * keyshape[2]; if (keypoints[index1 + 2] > threshold && keypoints[index2 + 2] > threshold) { const auto colorIndex = pairs[pair + 1] * 3; // Before: colorIndex = pair/2*3; const cv::Scalar color(colors[(colorIndex+2) % numberColors], colors[(colorIndex + 1) % numberColors], colors[(colorIndex + 0) % numberColors] ); const cv::Point keypoint1(intRound(keypoints[index1] * scale), intRound(keypoints[index1 + 1] * scale) ) ; const cv::Point keypoint2(intRound(keypoints[index2] * scale), intRound(keypoints[index2 + 1] * scale)) ; cv::line(frame, keypoint1, keypoint2, color, thicknessLine, lineType, shift); } } /* // Draw circles for (auto part = 0; part < numberKeypoints; part++) { const auto faceIndex = (person * numberKeypoints + part) * keyshape[2]; if (keypoints[faceIndex + 2] > threshold) { const auto colorIndex = part * 3; const cv::Scalar color( colors[(colorIndex+2) % numberColors], colors[(colorIndex + 1) % numberColors], colors[(colorIndex + 0) % numberColors] ); const cv::Point center(intRound(keypoints[faceIndex] * scale), intRound(keypoints[faceIndex + 1] * scale)); cv::circle(frame, center, radius, color, thicknessCircle, lineType, shift); } } */ } } } void renderPoseKeypointsCpu(Mat& frame, const vector<float>& poseKeypoints, vector<int> keyshape, const float renderThreshold, float scale, const bool blendOriginalFrame = true) { // Background if (!blendOriginalFrame) frame.setTo(0.f); // [0-255] // Parameters const auto thicknessCircleRatio = 1.f / 75.f; const auto thicknessLineRatioWRTCircle = 0.75f; const auto& pairs = POSE_COCO_PAIRS_RENDER; // Render keypoints renderKeypointsCpu(frame, poseKeypoints, keyshape, pairs, POSE_COCO_COLORS_RENDER, thicknessCircleRatio, thicknessLineRatioWRTCircle, renderThreshold, scale); } extern "C" JNIEXPORT jlong JNICALL Java_opencv_zhongke_ktcv_pedetion_Jniuti_hold(JNIEnv *env, jclass type, jlong mat, jfloatArray v_, jintArray v1_, jfloat scale,jint offlen) { jfloat *v = env->GetFloatArrayElements(v_, NULL); jint *v1 = env->GetIntArrayElements(v1_, NULL); Mat* mat1=(Mat*)mat; vector<float> poseKeypointsx(v,v+offlen); vector<int> keyshape(v1,v1+3); float scaler=scale; renderPoseKeypointsCpu(*mat1,poseKeypointsx,keyshape,0.05,scaler); env->ReleaseFloatArrayElements(v_, v, 0); env->ReleaseIntArrayElements(v1_, v1, 0); return (jlong) mat1; }
cv pose1
最新推荐文章于 2023-11-07 23:15:07 发布