javacv实战篇

看到之前有248位小伙伴看我写的水文还是比较受鼓舞的,所以决定把这个细写一下:

就是javacv怎么实际应用一下:

其实无外乎导包,写测试代码。

这样 那我们先导包儿。

javacv-1.2-bin(jar包的下载在这里:找一样名字的就行:http://www.cnblogs.com/letben/p/5662619.html)

他好像可以去git上下载,我忘记了 下载地址了,所以 去找找吧。

然后解压缩F:\jarPackage\opencv\javacv-1.2-bin\javacv-bin一直到这层目录,下面有茫茫多的.jar 文件,所以 待会儿 我们我们把它们全部导入就可以了。

好了好了好了,,,又小小的写了一下,应该是没有什么问题了。

1、打开 eclipse 然后新建工程

2、导入刚下载的那一票java包(对了我下载的大小是169Mb的压缩包,解压以后是170Mb)

3、写写代码:

import org.bytedeco.javacpp.opencv_core.IplImage;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
import org.bytedeco.javacpp.opencv_core.CvMat;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.OpenCVFrameConverter;

public class JustDisplay {

    public static void main(String args[]){

        IplImage rawImage = null;
        rawImage = cvLoadImage("Rice.jpg");
        ShowImage(rawImage, "RawImage", 256);
    }
     public static void ShowImage(IplImage image, String caption, int size)
       {
           if(size < 128) size = 128;
           CvMat mat = image.asCvMat();
           int width = mat.cols(); if(width < 1) width = 1;
           int height = mat.rows(); if(height < 1) height = 1;
           double aspect = 1.0 * width / height;
           if(height != size) { height = size; width = (int) ( height * aspect ); }
           if(width != size) width = size;
           height = (int) ( width / aspect );
           ShowImage(image, caption, width, height);
       }
     public static void ShowImage(IplImage image, String caption, int width, int height)
       {
           CanvasFrame canvas = new CanvasFrame(caption, 1);   // gamma=1
           canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
           canvas.setCanvasSize(width, height);
           OpenCVFrameConverter converter = new OpenCVFrameConverter.ToIplImage();
           canvas.showImage(converter.convert(image));
       }
}

反正都是逐句摘得,别问为什么,这就是 人家写包的原因,底层应该是 看不到的吧,然后  应该是 调的 现成的 写好的 c/c++ 的代码。也就是说,如果想改该算法或者优化的话还是要在vs的环境里面,这里面只能是调用(这是我瞎说的,我记得之前读到过这么句话,如果有异议的话,请告诉我如何在java的语言下优化图像处理的算法)

然后导入一张名叫Rice.jpg 的图片。这样就写好了。

今天懒就不截图了

然后这是一个简单版本的,所有代码都是从这个里面抄来的,上一篇那个javacv里面应该有相关源代码:这里面提供了更多的代码使用方式:可以看看:(下面的代码跑不了,应该是出在了 没有导入图片上)

如果还是有问题可以留邮箱,发代码~~~

import static org.bytedeco.javacpp.opencv_core.CV_8UC1;
import static org.bytedeco.javacpp.opencv_core.IPL_DEPTH_8U;
import static org.bytedeco.javacpp.opencv_core.cvCreateImage;
import static org.bytedeco.javacpp.opencv_core.cvCreateMat;
import static org.bytedeco.javacpp.opencv_core.cvFlip;
import static org.bytedeco.javacpp.opencv_core.cvGetSize;
import static org.bytedeco.javacpp.opencv_core.cvNot;
import static org.bytedeco.javacpp.opencv_core.cvPoint;
import static org.bytedeco.javacpp.opencv_core.cvReleaseImage;
import static org.bytedeco.javacpp.opencv_core.cvScalar;
import static org.bytedeco.javacpp.opencv_core.cvSetZero;
import static org.bytedeco.javacpp.opencv_core.cvSize;
import static org.bytedeco.javacpp.opencv_core.cvSum;
import static org.bytedeco.javacpp.opencv_core.cvTranspose;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
import static org.bytedeco.javacpp.opencv_imgproc.CV_BGR2GRAY;
import static org.bytedeco.javacpp.opencv_imgproc.CV_THRESH_BINARY;
import static org.bytedeco.javacpp.opencv_imgproc.cvCvtColor;
import static org.bytedeco.javacpp.opencv_imgproc.cvDilate;
import static org.bytedeco.javacpp.opencv_imgproc.cvErode;
import static org.bytedeco.javacpp.opencv_imgproc.cvRectangle;
import static org.bytedeco.javacpp.opencv_imgproc.cvThreshold;

import org.bytedeco.javacpp.opencv_core.CvMat;
import org.bytedeco.javacpp.opencv_core.CvPoint;
import org.bytedeco.javacpp.opencv_core.CvScalar;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.Blobs;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.OpenCVFrameConverter;
public class tryy {
    public static void main(String[] args) {

         System.out.println("STARTING...\n");
            demo();
            System.out.println("ALL DONE");
    }

    public static void demo()
   {
       int MinArea = 6;
       int ErodeCount =0;
       int DilateCount = 0;

       IplImage RawImage = null;

       // Read an image.
       for(int k = 0; k < 7; k++)
       {
           if(k == 0) { RawImage = cvLoadImage("BlackBalls.jpg"); MinArea = 250; ErodeCount = 0; DilateCount = 1; }
           else if(k == 1) { RawImage = cvLoadImage("Shapes1.jpg"); MinArea = 6; ErodeCount = 0; DilateCount = 1; }
           else if(k == 2) { RawImage = cvLoadImage("Shapes2.jpg"); MinArea = 250; ErodeCount = 0; DilateCount = 1; }
           else if(k == 3) { RawImage = cvLoadImage("Blob1.jpg"); MinArea = 2800; ErodeCount = 1; DilateCount = 1; }
           else if(k == 4) { RawImage = cvLoadImage("Blob2.jpg"); MinArea = 2800; ErodeCount = 1; DilateCount = 1; }
           else if(k == 5) { RawImage = cvLoadImage("Blob3.jpg"); MinArea = 2800; ErodeCount = 1; DilateCount = 1; }
           else if(k == 6) { RawImage = cvLoadImage("Rice.jpg"); MinArea = 30; ErodeCount = 2; DilateCount = 1; }
           //ShowImage(RawImage, "RawImage", 512);

           IplImage GrayImage = cvCreateImage(cvGetSize(RawImage), IPL_DEPTH_8U, 1);
           cvCvtColor(RawImage, GrayImage, CV_BGR2GRAY);
           //ShowImage(GrayImage, "GrayImage", 512);

           IplImage BWImage = cvCreateImage(cvGetSize(GrayImage), IPL_DEPTH_8U, 1);
           cvThreshold(GrayImage, BWImage, 127, 255, CV_THRESH_BINARY);
           //ShowImage(BWImage, "BWImage");

           IplImage WorkingImage = cvCreateImage(cvGetSize(BWImage), IPL_DEPTH_8U, 1);
           cvErode(BWImage, WorkingImage, null, ErodeCount);
           cvDilate(WorkingImage, WorkingImage, null, DilateCount);
           //ShowImage(WorkingImage, "WorkingImage", 512);

           //cvSaveImage("Working.jpg", WorkingImage);
           //PrintGrayImage(WorkingImage, "WorkingImage");
           //BinaryHistogram(WorkingImage);

           Blobs Regions = new Blobs();
           Regions.BlobAnalysis(
                   WorkingImage,               // image
                   -1, -1,                     // ROI start col, row
                   -1, -1,                     // ROI cols, rows
                   1,                          // border (0 = black; 1 = white)
                   MinArea);                   // minarea
           Regions.PrintRegionData();

           for(int i = 1; i <= Blobs.MaxLabel; i++)
           {
               double [] Region = Blobs.RegionData[i];
               int Parent = (int) Region[Blobs.BLOBPARENT];
               int Color = (int) Region[Blobs.BLOBCOLOR];
               int MinX = (int) Region[Blobs.BLOBMINX];
               int MaxX = (int) Region[Blobs.BLOBMAXX];
               int MinY = (int) Region[Blobs.BLOBMINY];
               int MaxY = (int) Region[Blobs.BLOBMAXY];
               Highlight(RawImage,  MinX, MinY, MaxX, MaxY, 1);
           }

           ShowImage(RawImage, "RawImage", 256);//最后一个参数图片展示大小的。

           cvReleaseImage(GrayImage); GrayImage = null;
           cvReleaseImage(BWImage); BWImage = null;
           cvReleaseImage(WorkingImage); WorkingImage = null;
       }
       cvReleaseImage(RawImage); RawImage = null;
   }

   // Versions with 2, 3, and 4 parms respectively
   public static void ShowImage(IplImage image, String caption)
   {
       CvMat mat = image.asCvMat();
       int width = mat.cols(); if(width < 1) width = 1;
       int height = mat.rows(); if(height < 1) height = 1;
       double aspect = 1.0 * width / height;
       if(height < 128) { height = 128; width = (int) ( height * aspect ); }
       if(width < 128) width = 128;
       height = (int) ( width / aspect );
       ShowImage(image, caption, width, height);
   }
   public static void ShowImage(IplImage image, String caption, int size)
   {
       if(size < 128) size = 128;
       CvMat mat = image.asCvMat();
       int width = mat.cols(); if(width < 1) width = 1;
       int height = mat.rows(); if(height < 1) height = 1;
       double aspect = 1.0 * width / height;
       if(height != size) { height = size; width = (int) ( height * aspect ); }
       if(width != size) width = size;
       height = (int) ( width / aspect );
       ShowImage(image, caption, width, height);
   }
   public static void ShowImage(IplImage image, String caption, int width, int height)
   {
       CanvasFrame canvas = new CanvasFrame(caption, 1);   // gamma=1
       canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
       canvas.setCanvasSize(width, height);
       OpenCVFrameConverter converter = new OpenCVFrameConverter.ToIplImage();
       canvas.showImage(converter.convert(image));
   }

   public static void Highlight(IplImage image, int [] inVec)
   {
       Highlight(image, inVec[0], inVec[1], inVec[2], inVec[3], 1);
   }
   public static void Highlight(IplImage image, int [] inVec, int Thick)
   {
       Highlight(image, inVec[0], inVec[1], inVec[2], inVec[3], Thick);
   }
   public static void Highlight(IplImage image, int xMin, int yMin, int xMax, int yMax)
   {
       Highlight(image, xMin, yMin, xMax, yMax, 1);
   }
   public static void Highlight(IplImage image, int xMin, int yMin, int xMax, int yMax, int Thick)
   {
       CvPoint pt1 = cvPoint(xMin,yMin);
       CvPoint pt2 = cvPoint(xMax,yMax);
       CvScalar color = cvScalar(255,0,0,0);       // blue [green] [red]
       cvRectangle(image, pt1, pt2, color, Thick, 4, 0);
   }

   public static void PrintGrayImage(IplImage image, String caption)
   {
       int size = 512; // impractical to print anything larger
       CvMat mat = image.asCvMat();
       int cols = mat.cols(); if(cols < 1) cols = 1;
       int rows = mat.rows(); if(rows < 1) rows = 1;
       double aspect = 1.0 * cols / rows;
       if(rows > size) { rows = size; cols = (int) ( rows * aspect ); }
       if(cols > size) cols = size;
       rows = (int) ( cols / aspect );
       PrintGrayImage(image, caption, 0, cols, 0, rows);
   }
   public static void PrintGrayImage(IplImage image, String caption, int MinX, int MaxX, int MinY, int MaxY)
   {
       int size = 512; // impractical to print anything larger
       CvMat mat = image.asCvMat();
       int cols = mat.cols(); if(cols < 1) cols = 1;
       int rows = mat.rows(); if(rows < 1) rows = 1;

       if(MinX < 0) MinX = 0; if(MinX > cols) MinX = cols;
       if(MaxX < 0) MaxX = 0; if(MaxX > cols) MaxX = cols;
       if(MinY < 0) MinY = 0; if(MinY > rows) MinY = rows;
       if(MaxY < 0) MaxY = 0; if(MaxY > rows) MaxY = rows; 

       System.out.println("\n" + caption);
       System.out.print("   +");
       for(int icol = MinX; icol < MaxX; icol++) System.out.print("-");
       System.out.println("+");

       for(int irow = MinY; irow < MaxY; irow++)
       {
           if(irow<10) System.out.print(" ");
           if(irow<100) System.out.print(" ");
           System.out.print(irow);
           System.out.print("|");
           for(int icol = MinX; icol < MaxX; icol++)
           {
               int val = (int) mat.get(irow,icol);
               String C = " ";
               if(val == 0) C = "*";
               System.out.print(C);
           }
           System.out.println("|");
       }
       System.out.print("   +");
       for(int icol = MinX; icol < MaxX; icol++) System.out.print("-");
       System.out.println("+");
   }

   public static void PrintImageProperties(IplImage image)
   {
       CvMat mat = image.asCvMat();
       int cols = mat.cols();
       int rows = mat.rows();
       int depth = mat.depth();
       System.out.println("ImageProperties for " + image + " : cols=" + cols + " rows=" + rows + " depth=" + depth);
   }

   public static float BinaryHistogram(IplImage image)
   {
       CvScalar Sum = cvSum(image);
       float WhitePixels = (float) ( Sum.getVal(0) / 255 );
       CvMat mat = image.asCvMat();
       float TotalPixels = mat.cols() * mat.rows();
       //float BlackPixels = TotalPixels - WhitePixels;
       return WhitePixels / TotalPixels;
   }

   // Counterclockwise small angle rotation by skewing - Does not stretch border pixels
   public static IplImage SkewGrayImage(IplImage Src, double angle)    // angle is in radians
   {
       //double radians = - Math.PI * angle / 360.0;   // Half because skew is horizontal and vertical
       double sin = - Math.sin(angle);
       double AbsSin = Math.abs(sin);

       int nChannels = Src.nChannels();
       if(nChannels != 1)
       {
           System.out.println("ERROR: SkewGrayImage: Require 1 channel: nChannels=" + nChannels);
           System.exit(1);
       }

       CvMat SrcMat = Src.asCvMat();
       int SrcCols = SrcMat.cols();
       int SrcRows = SrcMat.rows();

       double WidthSkew = AbsSin * SrcRows;
       double HeightSkew = AbsSin * SrcCols;

       int DstCols = (int) ( SrcCols + WidthSkew );
       int DstRows = (int) ( SrcRows + HeightSkew );

       CvMat DstMat = cvCreateMat(DstRows, DstCols, CV_8UC1);  // Type matches IPL_DEPTH_8U
       cvSetZero(DstMat);
       cvNot(DstMat, DstMat);

       for(int irow = 0; irow < DstRows; irow++)
       {
           int dcol = (int) ( WidthSkew * irow / SrcRows );
           for(int icol = 0; icol < DstCols; icol++)
           {
               int drow = (int) ( HeightSkew - HeightSkew * icol / SrcCols );
               int jrow = irow - drow;
               int jcol = icol - dcol;
               if(jrow < 0 || jcol < 0 || jrow >= SrcRows || jcol >= SrcCols) DstMat.put(irow, icol, 255);
               else DstMat.put(irow, icol, (int) SrcMat.get(jrow,jcol));
           }
       }

       IplImage Dst = cvCreateImage(cvSize(DstCols, DstRows), IPL_DEPTH_8U, 1);
       Dst = DstMat.asIplImage();
       return Dst;
   }

   public static IplImage TransposeImage(IplImage SrcImage)
   {
       CvMat mat = SrcImage.asCvMat();
       int cols = mat.cols();
       int rows = mat.rows();
       IplImage DstImage = cvCreateImage(cvSize(rows, cols), IPL_DEPTH_8U, 1);
       cvTranspose(SrcImage, DstImage);
       cvFlip(DstImage,DstImage,1);
       return DstImage;
   }

}
时间: 2024-10-08 22:04:49

javacv实战篇的相关文章

《黑客攻防技术宝典Web实战篇》.Dafydd.Stuttard.第2版中文高清版pdf

下载地址:网盘下载 内容简介 编辑 <黑客攻防技术宝典(Web实战篇第2版)>从介绍当前Web应用程序安全概况开始,重点讨论渗透测试时使用的详细步骤和技巧,最后总结书中涵盖的主题.每章后还附有习题,便于读者巩固所学内容. <黑客攻防技术宝典(Web实战篇第2版)>适合各层次计算机安全人士和Web开发与管理领域的技术人员阅读.本书由斯图塔德.平托著. 目录 编辑 第1章 Web应用程序安全与风险 1.1 Web应用程序的发展历程 [1] 1.1.1 Web应用程序的常见功能 1.1.

caffe框架下目标检测——faster-rcnn实战篇操作

原有模型 1.下载fasrer-rcnn源代码并安装 git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git 1)  经常断的话,可以采取两步: git clone https://github.com/rbgirshick/py-faster-rcnn.git 2)  到py-faster-rcnn中,继续下载caffe-faster-rcnn,采取后台跑: git submodule update --in

《nodejs+gulp+webpack基础实战篇》课程笔记(四)-- 实战演练

一.用gulp 构建前端页面(1)---静态构建 npm install gulp-template --save-dev 通过这个插件,我们可以像写后台模板(譬如PHP)一样写前端页面.我们首先学习一下写法. 现在我们创建一个新任务:创建一个裸的index.html文件,然后在body里面写上 ,我的年龄是:<%= age %> 下载好gulp-template,我们引用并配置 var gulp_tpl = require("gulp-template"); gp.tas

XenApp_XenDesktop_7.6实战篇之十九:安装、升级和配置StoreFront 3.0

CitrixXenDesktop7.6 Feature Pack 2中发布了StoreFront 3.0,本章节为大家分享如何全新部署.升级和配置StoreFront 3.0.默认XenDesktop 7.6光盘自带的StoreFront版本为2.6.0.5031.注意升级和全新部署StoreFront 3.0使用的安装程序是一致的. 1.  全新部署StoreFront 3.0 1.1   将CTXSF01服务器加入域,并使用具备本地管理员权限的域账户进行登录 1.2   运行StoreFro

2天驾驭DIV+CSS (实战篇)(转)

这是去年看到的一片文章,感觉在我的学习中,有不少的影响.于是把它分享给想很快了解css的兄弟们.本文是实战篇. 基础篇[知识一] “DIV+CSS” 的叫法是不准确的[知识二] “DIV+CSS” 将你引入两大误区[知识三] 什么是W3C标准?[基础一] CSS如何控制页面[基础二] CSS选择器[基础三] CSS选择器命名及常用命名[基础四] 盒子模型[基础五] 块状元素和内联元素 实战篇[第一课] 实战小热身[第二课] 浮动[第三课] 清除浮动[第四课] 导航条(上) | 导航条(下)[大练

Java 基础:认识&理解关键字 native 实战篇

Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket 泥瓦匠初次遇见 navicat 是在 java.lang.Object 源码中的一个hashCode方法: 1 public native int hashCode(); 为什么有个navicat呢?这是我所要学习的地方.所以今天泥瓦匠想要总结下navicat. 一.认识 native 即 JNI,Java Native Interface 凡是一种语言,都希望是纯.比如解决某一个方案都喜欢就单单这

漫游kafka实战篇之搭建Kafka开发环境

转载注明出处:http://blog.csdn.net/honglei915/article/details/37563647 上篇文章中我们搭建了kafka的服务器,并可以使用Kafka的命令行工具创建topic,发送和接收消息.下面我们来搭建kafka的开发环境. 添加依赖 搭建开发环境需要引入kafka的jar包,一种方式是将Kafka安装包中lib下的jar包加入到项目的classpath中,这种比较简单了.不过我们使用另一种更加流行的方式:使用maven管理jar包依赖. 创建好mav

Systemd 入门教程:实战篇

Systemd 入门教程:实战篇 上一篇文章,介绍了 Systemd 的主要命令,这篇文章主要介绍如何使用 Systemd 来管理我们的服务,以及各项的含义: 一.开机启动 对于那些支持 Systemd 的软件,安装的时候,会自动在/usr/lib/systemd/system目录添加一个配置文件,如果你想让该软件开机启动,就执行下面的命令(以httpd.service为例). $ sudo systemctl enable httpd 上面的命令相当于在/etc/systemd/system目

javamail模拟邮箱功能发送电子邮件-中级实战篇【新增附件发送方法】(javamail API电子邮件实例)

引言: 此篇是紧随上篇文章而封装出来的,阅读本篇章建议先阅读上一篇  --> javamail模拟邮箱功能发送电子邮件-基础实战篇 上一篇章简单讲解了javamail发送邮件的基本基础和用到的几个类,并且所有初始化和发送方法都封装在一个类里面(为了代码方便演示),本章节将对各个模块进行拆分和优化,并且引入附件发送的方法 要想邮件发送附件,就要引入两个新类  Multipart 和 BodyPart 两类 Multipart(报文部件容器) 实则为一个报文容器,而邮件消息  Msage 是由各个子