亲宝软件园·资讯

展开

OpenCV JVM矩阵处理图像

Java璐到底 人气:0

submat(int rowStart, int rowEnd, int colStart, int colEnd) 函数的返回值是一个矩阵对象。内容是原图的子矩阵或子区域。

首先我们用imread来读取图片,然后输出矩阵对象本身的一些信息

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Core;
import org.opencv.core.MatOfInt;
import org.opencv.imgcodecs.Imgcodecs;
import origami.Origami;

public class HelloCv {
    public static void main(String[] args) throws Exception {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat = Imgcodecs.imread("./images/test.jpg",Imgcodecs.IMREAD_GRAYSCALE);
        System.out.println(mat);
    }
}

由于这个矩阵是原始图片,所以它的isSubmat是false。

现在我们使用submat函数的第一种形式,输入参数是每一行和每一列的起始和终止值。

图片裁剪

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Core;
import org.opencv.core.MatOfInt;
import org.opencv.imgcodecs.Imgcodecs;
import origami.Origami;

public class HelloCv {
    public static void main(String[] args) throws Exception {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat = Imgcodecs.imread("C:/HWKJ/ZRQ/OpenCv/matrixcv/images/test.jpg");
        System.out.println(mat);
        Mat submat = mat.submat(200, 240, 300, 350);
        System.out.println(submat);
    }
}

这里注意submat里的尺寸,尺寸根据原图的尺寸,超出原图的尺寸会报错,报错如下

然后我们输出裁剪的图片。

那么如何确认你想要截取图片的区域范围呢?也就是说怎么确定这四个参数的填写?我们以下图为例

截取后的图片

另外两种submat方式

Range​(int row,int column)

row:宽开始结束范围

column:高开始结束范围

Mat submat2 = mat.submat(new Range(20,300),new Range(100,500));
Imgcodecs.imwrite("./images/output2.png",submat2);

Rect​(int x, int y,int width, int height)

x:横坐标

y:纵坐标

width :宽

height:高

Mat submat3 = mat.submat(new Rect(0,200,100,100));
//submat3.setTo(new Scalar(255,0,0));//将图片绘制为蓝色
Imgcodecs.imwrite("./images/output3.png",submat3);

打开setTo如下:

Imgcodecs.imwrite("./images/blurtest.png",mat);

完整代码:

import org.opencv.core.CvType;
import org.opencv.core.Scalar;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Range;
import org.opencv.core.Core;
import org.opencv.core.Size;
import org.opencv.core.MatOfInt;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import origami.Origami;

public class HelloCv {
    public static void main(String[] args) throws Exception {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat = Imgcodecs.imread("C:/HWKJ/ZRQ/OpenCv/matrixcv/images/test.jpg");
        System.out.println(mat);
        Mat submat = mat.submat(200, 400, 200, 550);
        //System.out.println(submat);
        Imgcodecs.imwrite("./images/output.png",submat);
        Mat submat2 = mat.submat(new Range(20,300),new Range(100,500));
        Imgcodecs.imwrite("./images/output2.png",submat2);
        Mat submat3 = mat.submat(new Rect(0,200,400,200));
        submat3.setTo(new Scalar(255,0,0));
        Imgcodecs.imwrite("./images/output3.png",submat3);

        //Imgproc.blur(submat,submat,new Size(25.0,25.0));
        Imgcodecs.imwrite("./images/blurtest.png",mat);
    }
}

图片模糊处理

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Core;
import org.opencv.core.Size;
import org.opencv.core.MatOfInt;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import origami.Origami;

public class HelloCv {
    public static void main(String[] args) throws Exception {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat = Imgcodecs.imread("C:/HWKJ/ZRQ/OpenCv/matrixcv/images/test.jpg");
        System.out.println(mat);
        Mat submat = mat.submat(200, 400, 200, 550);
        //System.out.println(submat);
        //Imgcodecs.imwrite("./images/output.png",submat);
        Imgproc.blur(submat,submat,new Size(25.0,25.0));
        System.out.println("after:"+mat);
        Imgcodecs.imwrite("./images/blurtest.png",mat);
    }
}

子矩阵生成矩阵

setTo和copyTo是OpenCv中两个非常重要的函数。

setTo可以将一个矩阵中的所有像素设置为指定的颜色

copyTo可以将一个已有的矩阵复制到另一个矩阵之中。

第一个颜色值代表蓝色的深度,第二个值代表绿色的深度,最后一个值代表红色的深度。

//获取红绿蓝
Scalar Red = new Scalar(0,0,255);
Scalar Green = new Scalar(0,255,0);
Scalar Blue = new Scalar(255,0,0);

我们把这些颜色当作RGB的补充色。因此把其他通道设置为最大值255,主通道设置为0。蓝绿色是红色的补充色,所以红色值通道被设为0,而另外两个通道为255;

定义蓝绿色、品红色和黄色

Scalar cyan = new Scalar(255,255,0);
Scalar  magena= new Scalar(255,0,255);
Scalar yellow = new Scalar(0,255,255);

下面我们使用setTo将子矩阵设置为给定的Scalar颜色

    private void setColors(Mat mat ,boolean comp,int row){
      for (int i = 0; i <3 ; i++) {
          Mat sub = mat.submat(row*100,row*100+100,i*100,i*100+100);
          if(comp){
             //RGB
             if (i==0){
                 sub.setTo(Red);
             }if (i==1){
                  sub.setTo(Green);
              }if (i==2){
                  sub.setTo(Blue);
              }
          }else {
              //cmy
              if (i==0){
                  sub.setTo(cyan);
              }if (i==1){
                  sub.setTo(magena);
              }if (i==2){
                  sub.setTo(yellow);
              }
          }
      }
  }

接下来,我们创建一个包含三个颜色通道矩阵,并且填充它的第一行和第二行

完整代码:

import org.opencv.core.CvType;
import org.opencv.core.Scalar;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Range;
import org.opencv.core.Core;
import org.opencv.core.Size;
import org.opencv.core.MatOfInt;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import origami.Origami;

public class HelloCv1 {
public static  Scalar Red = new Scalar(0,0,255);
public static   Scalar Green = new Scalar(0,255,0);
public static   Scalar Blue = new Scalar(255,0,0);
public static   Scalar cyan = new Scalar(255,255,0);
public static   Scalar  magena= new Scalar(255,0,255);
public static   Scalar yellow = new Scalar(0,255,255);
    public static void main(String[] args) throws Exception {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat = new Mat(200,300,CvType.CV_8UC3);
        setColors(mat,false,1);
        setColors(mat,true,0);
        Imgcodecs.imwrite("./images/rgbcmy.png",mat);

    }

    static void setColors(Mat mat ,boolean comp,int row){
      for (int i = 0; i <3 ; i++) {
          Mat submat = mat.submat(row*100,row*100+100,i*100,i*100+100);
          if(comp){
             //RGB
             if (i==0){
               submat.setTo(Red);
             }if (i==1){
               submat.setTo(Green);
              }if (i==2){
                submat.setTo(Blue);
              }
          }else {
              //cmy
              if (i==0){
                submat.setTo(cyan);
              }if (i==1){
                submat.setTo(magena);
              }if (i==2){
                submat.setTo(yellow);
              }
          }
      }
  }
}

从图片子矩阵生成矩阵

首先创建一个大小为200x200的矩阵和子矩阵:一个是主矩阵的上部,一个是主矩阵的下部

int width = 200,height = 200;
Mat mat1 = new Mat(height,width,CvType.CV_8UC3);
Mat top = mat.submat(0,height/2,0,width);
Mat bottom = mat.submat(height/2,height,0,width);

然后加载一个图片以创建另一个小矩阵,并把它的大小调整为上部(或下部)的子矩阵大小。这里会引入Imgproc类中的resize函数。

完整代码:

import org.opencv.core.CvType;
import org.opencv.core.Scalar;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Range;
import org.opencv.core.Core;
import org.opencv.core.Size;
import org.opencv.core.MatOfInt;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import origami.Origami;

public class HelloCv1 {
public static  Scalar Red = new Scalar(0,0,255);
public static   Scalar Green = new Scalar(0,255,0);
public static   Scalar Blue = new Scalar(255,0,0);
public static   Scalar cyan = new Scalar(255,255,0);
public static   Scalar  magena= new Scalar(255,0,255);
public static   Scalar yellow = new Scalar(0,255,255);
    public static void main(String[] args) throws Exception {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        int width = 200,height = 300;
        Mat mat1 = new Mat(height,width,CvType.CV_8UC3);
        Mat top = mat1.submat(0,height/2,0,width);
        Mat bottom = mat1.submat(height/2,height,0,width);

        Mat small = Imgcodecs.imread("./images/test.jpg");
        Imgproc.resize(small,small,top.size());
        small.copyTo(top);
        small.copyTo(bottom);
        Imgcodecs.imwrite("./images/matofpictures.png",mat1);
    }

注意:设置大小的步骤很关键。复制能够成功,是因为小矩阵和子矩阵的大小是完全相同的,因此复制的时候没有出现任何问题

加载全部内容

相关教程
猜你喜欢
用户评论