Skip to content

Instantly share code, notes, and snippets.

@wanewang
Created August 24, 2011 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wanewang/1167548 to your computer and use it in GitHub Desktop.
Save wanewang/1167548 to your computer and use it in GitHub Desktop.
jni format opencv
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
extern "C"{
JNIEXPORT void JNICALL Java_com_test_opencv_Opencvtest_setimage
(JNIEnv *env, jobject obj) {
IplImage* imgIn = cvLoadImage("/mnt/sdcard/c.jpg");
if(!imgIn){
}else{
IplImage* imgOut = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U,3);
cvSmooth(imgIn,imgOut);
const char* result1 = "/mnt/sdcard/c_1.jpg";
cvSaveImage(result1,imgOut);
IplImage* imgGray = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U,1);
cvCvtColor(imgIn,imgGray,CV_BGR2GRAY);
const char* result2 = "/mnt/sdcard/c_2.jpg";
cvSaveImage(result2,imgGray);
IplImage* imgThr = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U,1);
cvThreshold(imgGray,imgThr,128,1,CV_THRESH_BINARY);
int height = imgThr->height;
int width = imgThr->width;
int step = imgThr->widthStep/sizeof(uchar);
uchar* data = (uchar *)imgThr->imageData;
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
if(data[i*step+j]==1){
data[i*step+j]=255;
}
}
}
const char* result3 = "/mnt/sdcard/c_3.jpg";
cvSaveImage(result3,imgThr);
cvReleaseImage(&imgIn);
cvReleaseImage(&imgOut);
cvReleaseImage(&imgGray);
cvReleaseImage(&imgThr);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment