openCV的几个例子

原创
2011/10/14 10:40
阅读数 150
/*  程序名:hello.c
   功能:从磁盘中读入图像文件,并将图像显示在屏幕上
*/
#include "cv.h"
#include "highgui.h"
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
 
//int main (int argc, char **argv)
//{
//  IplImage *src = 0;
//  IplImage *dst = 0;
//  clock_t start;
//  clock_t finish;
//  double   duration;
//
//  /* the first command line parameter must be image file name */
//  if ((argc == 2) && (src = cvLoadImage (argv[1], -1)) != 0)
//    {
//      int delta = 1;
//      int angle = 0;
//      int opt = 1;        // 1: 旋转加缩放
//      // 0:  仅仅旋转
//      double factor;
//
//      dst = cvCloneImage (src);
//      cvNamedWindow ("src", 1);
//      cvShowImage ("src", src);
//
//      for (;;)
//  {
//
//    float m[6];
//    // Matrix m looks like:
//    //
//    // [ m0  m1  m2 ] ===>  [ A11  A12   b1 ]
//    // [ m3  m4  m5 ]       [ A21  A22   b2 ]
//    //
//
//    CvMat M = cvMat (2, 3, CV_32F, m);
//
//    int w = src->width;
//    int h = src->height;
//
//     start=clock();
//    if (opt)      // 旋转加缩放
//      factor = (cos (angle * CV_PI / 180.) + 1.0) * 2;
//    else          //  仅仅旋转
//      factor = 1;
//    m[0] = (float) (factor * cos (-angle * 2 * CV_PI / 180.));
//    m[1] = (float) (factor * sin (-angle * 2 * CV_PI / 180.));
//    m[3] = -m[1];
//    m[4] = m[0];
//    // 将旋转中心移至图像中间
//    m[2] = w * 0.5f;
//    m[5] = h * 0.5f;
//    //  dst(x,y) = A * src(x,y) + b
//    cvZero (dst);
//    cvGetQuadrangleSubPix (src, dst, &M);
//    cvNamedWindow ("dst", 1);
//    cvShowImage ("dst", dst);
//    finish = clock();
//    duration = (double)(finish - start) / CLOCKS_PER_SEC;
//      printf( "%f seconds\n", duration );
//
//    if (cvWaitKey (1) == 27)  //ESC
//      break;
//    angle = (int) (angle + delta) % 360;
//  }           // for-loop
//    }
//  return 0;
//  }
 
//int main(int argc, char* argv[])
//{
//    IplImage *src = 0;            //源图像指针
//    IplImage *dst = 0;            //目标图像指针
//    float scale = 5.618;      //缩放倍数为0.618倍
//    CvSize dst_cvsize;            //目标图像尺寸
//
//    /* the first command line parameter must be image file name */
//    if ( argc == 2 && (src = cvLoadImage(argv[1], -1))!=0 )
//    {
//        //如果命令行传递了需要打开的图片就无须退出,所以注释掉下面一行!
//  //return -1;
//    }
//    else
//    {
//        src = cvLoadImage("C:\vim.png", -1);    //载入工作目录下文件名为“tiger.jpg”的图片。
//    }
//
//    dst_cvsize.width = src->width * scale;     //目标图像的宽为源图象宽的scale倍
//    dst_cvsize.height = src->height * scale;   //目标图像的高为源图象高的scale倍
//
//    dst = cvCreateImage( dst_cvsize, src->depth, src->nChannels);   //构造目标图象
//    cvResize(src, dst, CV_INTER_LINEAR);  //缩放源图像到目标图像
//
//    cvNamedWindow( "src",   CV_WINDOW_AUTOSIZE ); //创建用于显示源图像的窗口
//    cvNamedWindow( "dst",   CV_WINDOW_AUTOSIZE ); //创建用于显示目标图像的窗口
//
//    cvShowImage( "src", src );        //显示源图像
//    cvShowImage( "dst", dst );        //显示目标图像
//
//    cvWaitKey(-1);        //等待用户响应
//
//    cvReleaseImage(&src); //释放源图像占用的内存
//    cvReleaseImage(&dst); //释放目标图像占用的内存
//    cvDestroyWindow( "src" ); //销毁窗口“src”
//    cvDestroyWindow( "dst" ); //销毁窗口“dst”
//    //void cvDestroyAllWindows(void);
//
//    return 0;
//}
 
/*  程序名:hello.c
   功能:从磁盘中读入图像文件,并将图像显示在屏幕上
*/
//#include "cv.h"
//#include "highgui.h"
//#include "stdio.h"
//#include "stdlib.h"
//#include "time.h"
 
int main( int argc, char ** argv )
{
    /* IplImage* pImg; //声明IplImage指针
     IplImage* oImg;
     CvSize oSize;
     clock_t start, finish;
     double   duration;
     CvRect rect ;
 
     rect.height = 120;
     rect.width = 120;
     rect.x = 0 ;
     rect.y = 0 ;
 
     oSize.width = 1600;
     oSize.height = 1200;
     //载入图像
 
     pImg = cvLoadImage("c:/1.jpg", 1);
 
     cvSetImageROI(pImg,rect);
 
     oImg = cvCreateImage(oSize,pImg->depth,pImg->nChannels);
     start = clock();
     cvResize(pImg,oImg,CV_INTER_LINEAR);
     finish = clock();
     cvSaveImage("c:/2.jpg",oImg,0);
 
     duration = (double)(finish - start) / CLOCKS_PER_SEC;
     printf( "%f seconds\n", duration );
 
     cvNamedWindow( "Image", 1 ); //创建窗口
     cvShowImage( "Image", pImg ); //显示图像
     cvWaitKey(0); //等待按键
     cvDestroyWindow( "Image" );//销毁窗口
     cvReleaseImage( &pImg ); //释放图像
     cvReleaseImage( &oImg ); //释放图像
     return 0;*/
 
     IplImage* pImg; //声明IplImage指针
     IplImage* oImg;
     CvSize oSize;
     clock_t start, finish;
     double   duration;
     CvRect rect ;
     CvRect rectDes ;
     rect.height = 120;
     rect.width = 120;
     rect.x = 100 ;
     rect.y = 100 ;
 
     rectDes.height = 120;
     rectDes.width = 120;
     rectDes.x = 200 ;
     rectDes.y = 200 ;
     oSize.width = 1600;
     oSize.height = 1200;
     //载入图像
 
     pImg = cvLoadImage( "d:/1.jpg" , 1);
     oImg = cvLoadImage( "d:/2.jpg" , 1);
 
     cvSetImageROI(pImg,rect);
     cvSetImageROI(oImg,rectDes);
     cvCopy(pImg,oImg,NULL);
     cvResetImageROI(oImg);
     cvSaveImage( "d:/3.jpg" ,oImg,0);
     printf ( "size of image=:%d" ,oImg->nSize);
 
     cvNamedWindow( "Image" , 1 ); //创建窗口
     cvShowImage( "Image" , pImg ); //显示图像
     cvWaitKey(0); //等待按键
     cvDestroyWindow( "Image" ); //销毁窗口
     cvReleaseImage( &pImg ); //释放图像
     cvReleaseImage( &oImg ); //释放图像
     return 0;
 
}
<pre class = "brush:cpp" >#include <jni.h>
#include <cstdlib>
#include <stdexcept>
#include <string>
#include <sstream>
#include <opencv2/core/core_c.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui_c.h>
 
using namespace cv;
 
/* This is a trivial JNI example where we use a native method
  * to return a new VM String. See the corresponding Java source
  * file located at:
  *
  *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
  */
extern "C"
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                   jobject thiz )
{
     Mat K(3,3,DataType< double >::type);
     std::stringstream ss;
     std::string s = "Hello from JNI ! " ;
     ss << s <<  K.cols << " " << K.rows;
     try
     {
         if (std:: getenv ( "NON_EXISTENT_ENVIRONMENT_VARIABLE" ) == NULL)
             throw std::runtime_error( "Hello from JNI (with full C++ support) !" );
     }
     catch (std::exception &ex)
     {
         s = ex.what();
     }
 
     IplImage* pImg; //声明IplImage指针
     IplImage* oImg;
     CvSize oSize;
     clock_t start, finish;
     double   duration;
     CvRect rect ;
     CvRect rectDes ;
     rect.height = 120;
     rect.width = 120;
     rect.x = 100 ;
     rect.y = 100 ;
 
     rectDes.height = 120;
     rectDes.width = 120;
     rectDes.x = 200 ;
     rectDes.y = 200 ;
     oSize.width = 1600;
     oSize.height = 1200;
     //载入图像
 
     pImg = cvLoadImage( "/sdcard/1.jpg" , 1);
     oImg = cvLoadImage( "/sdcard/2.jpg" , 1);
 
     cvSetImageROI(pImg,rect);
     cvSetImageROI(oImg,rectDes);
     cvCopy(pImg,oImg,NULL);
     cvResetImageROI(oImg);
     cvSaveImage( "/sdcard/3.jpg" ,oImg,0);
     printf ( "size of image=:%d" ,oImg->imageSize);
 
     cvReleaseImage( &pImg ); //释放图像
     cvReleaseImage( &oImg ); //释放图像
 
     return env->NewStringUTF(ss.str().c_str());
}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部