OpenCV之基本繪圖(在Mat和Bitmap上)
在之前的部落格中,關於通過Canvas配合Paint繪圖儲存在Bitmap上,最後展示在ImageView上,我還專門做了一個畫圖釋義的Demo,具體請轉至我用2D繪圖API畫了一隻好醜的雞,Canvas配合MotionEvent實現畫板功能檢視。具體就來介紹一下如何通過OpenCV4Android提供的API繪製這些基本形狀並儲存至Mat物件,然後轉化為Bitmap在ImageView上顯示出來。
1,繪圖API
當前在Mat上繪圖的實現在Imgproc模組中,那我們就來看看這個模組究竟有哪些關於繪製基本圖形的介面。這裡我想宣告一下,因為其方法數量實在太多,這裡不全都列舉,只給出幾個和今天介紹相關的介面方法;還有就是該類中的所有方法均為static方法。
- 畫圓:
static void |
circle(Mat img, Point center, int radius, Scalar color) |
static void |
circle(Mat img, Point center, int radius, Scalar color, int thickness) |
static void |
circle(Mat img, Point center, int radius, Scalar color, int thickness, int lineType) |
static void |
circle(Mat img, Point center, int radius, Scalar color, int thickness, int lineType, int shift) |
其中:
img:表示接收繪製資訊的Mat物件
center:圓心座標(單位畫素)
radius:半徑(單位畫素)
color:顏色物件
thickness:線條粗細
lintType:線條型別
shift:位置偏移
- 畫橢圓
static void |
ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle,Scalar color) |
static void |
ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle,Scalar color, int thickness) |
static void |
ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle,Scalar color, int thickness, int lineType) |
static void |
ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle,Scalar color, int thickness, int lineType, int shift) |
static void |
ellipse(Mat img, RotatedRect box, Scalar color) |
static void |
ellipse(Mat img, RotatedRect box, Scalar color, int thickness) |
static void |
ellipse(Mat img, RotatedRect box, Scalar color, int thickness, int lineType) |
imag:接收繪製資訊的Mat物件
center:橢圓圓心座標
axes:長軸與短軸大小
angle:旋轉角度
startAngle:開始角度
endAngle:結束角度
color:顏色物件
thickness:線條粗細
lineType:線條型別
shift:位置偏移
- 畫線:
static void |
line(Mat img, Point pt1, Point pt2, Scalar color) |
static void |
line(Mat img, Point pt1, Point pt2, Scalar color, int thickness) |
static void |
line(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType) |
static void |
line(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType, int shift) |
imag:接收繪製資訊的Mat物件
pt1:起始點座標
pt2:終點座標
color:顏色物件
thickness:線條粗細
lineType:線條型別
shift:位置偏移
- 畫矩形:
static void |
rectangle(Mat img, Point pt1, Point pt2, Scalar color) |
static void |
rectangle(Mat img, Point pt1, Point pt2, Scalar color, int thickness) |
static void |
rectangle(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType) |
static void |
rectangle(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType, int shift) |
imag:接收繪製資訊的Mat物件
pt1:左上角座標
pt2:右下角座標
color:顏色物件
thickness:線條粗細
lineType:線條型別
shift:位置偏移
- 寫文字:
static void |
putText(Mat img, java.lang.String text, Point org, int fontFace, double fontScale, Scalar color) |
static void |
putText(Mat img, java.lang.String text, Point org, int fontFace, double fontScale, Scalar color, int thickness) |
static void |
putText(Mat img, java.lang.String text, Point org, int fontFace, double fontScale, Scalar color, int thickness, int lineType) |
static void |
putText(Mat img, java.lang.String text, Point org, int fontFace, double fontScale, Scalar color, int thickness, int lineType, boolean bottomLeftOrigin) |
imag:接收繪製資訊的Mat物件
text:文字內容
org:開始位置點座標
fontFace:字型型別
fontScale:字型大小
color:顏色物件
thickness:線條粗細
lineType:線條型別
shift:位置偏移
2 示例
上面就是基本繪圖API以及其引數介紹,其實呼叫就很賤單了,下面我們就來舉一個示例說明一下使用的方法,還是一樣,先看一下效果圖:
activity_draw.xml程式碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hfut.operationopencvmain.Mat_Test.DrawActivity">
<LinearLayout
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="120px">
<ImageView
android:id="@+id/draw_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400px"
android:orientation="horizontal">
<ImageView
android:id="@+id/draw_rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/draw_circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400px"
android:orientation="horizontal">
<ImageView
android:id="@+id/draw_ellipse"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/draw_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="drawRectangle"
android:text="矩形" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="drawCircle"
android:text="圓形" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="drawEllipse"
android:text="橢圓" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="drawLine"
android:text="直線" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="drawText"
android:text="文字" />
</LinearLayout>
</LinearLayout>
DrawActivity.java程式碼:
package com.hfut.operationopencvmain.Mat_Test;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.hfut.operationopencvmain.R;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
/**
* @author why
* @date 2018-11-28 10:04:45
*/
public class DrawActivity extends AppCompatActivity {
ImageView ranctangleImageView;
ImageView circleImageView;
ImageView ellipseImageView;
ImageView lineImageView;
ImageView textImageView;
private static final String TAG = "DrawActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
boolean status = OpenCVLoader.initDebug();
if (status) {
Log.e(TAG, "onCreate: Succese");
} else {
Log.e(TAG, "onCreate: Failed");
}
ranctangleImageView = findViewById(R.id.draw_rectangle);
circleImageView = findViewById(R.id.draw_circle);
ellipseImageView = findViewById(R.id.draw_ellipse);
lineImageView = findViewById(R.id.draw_line);
textImageView = findViewById(R.id.draw_text);
//imageView.setImageDrawable(getResources().getDrawable(R.drawable.test));
}
//繪製矩形
public void drawRectangle(View view) {
Mat srcMat = Mat.zeros(400, 400, CvType.CV_8UC3);
//綠色,寬度5個畫素
Imgproc.rectangle(srcMat, new Point(100, 100), new Point(300, 300), new Scalar(0, 255, 0), 5);
Bitmap destbitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.RGB_565);
Utils.matToBitmap(srcMat, destbitmap);
ranctangleImageView.setImageBitmap(destbitmap);
}
//繪製圓形
public void drawCircle(View view) {
Mat srcMat = Mat.zeros(400, 400, CvType.CV_8UC3);
//紅色,寬度5個畫素
Imgproc.circle(srcMat, new Point(200, 200), 120, new Scalar(0, 0, 255), 5);
Bitmap destbitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.RGB_565);
Utils.matToBitmap(srcMat, destbitmap);
circleImageView.setImageBitmap(destbitmap);
}
//繪製橢圓
public void drawEllipse(View view) {
Mat srcMat = Mat.zeros(400, 400, CvType.CV_8UC3);
//紅色,寬度5個畫素
Imgproc.ellipse(srcMat, new Point(200, 200), new Size(100, 50),
360, 0, 360, new Scalar(255, 0, 0), 5, 8, 0);
Bitmap destbitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.RGB_565);
Utils.matToBitmap(srcMat, destbitmap);
ellipseImageView.setImageBitmap(destbitmap);
}
//繪製直線
public void drawLine(View view) {
Mat srcMat = Mat.zeros(400, 400, CvType.CV_8UC3);
//藍色,寬度5個畫素
Imgproc.line(srcMat, new Point(100, 100), new Point(300, 300), new Scalar(255, 0, 0), 5);
Imgproc.line(srcMat, new Point(300, 100), new Point(100, 300), new Scalar(255, 0, 0), 5);
Bitmap destbitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.RGB_565);
Utils.matToBitmap(srcMat, destbitmap);
lineImageView.setImageBitmap(destbitmap);
}
//繪製文字
public void drawText(View view) {
Mat srcMat = Mat.zeros(100, 600, CvType.CV_8UC3);
//藍色,寬度5個畫素
Imgproc.putText(srcMat, "Welcome to OpenCV4Android", new Point(50, 50),
Core.FONT_HERSHEY_PLAIN, 2, new Scalar(255, 0, 0), 5);
Bitmap destbitmap = Bitmap.createBitmap(600, 100, Bitmap.Config.RGB_565);
Utils.matToBitmap(srcMat, destbitmap);
textImageView.setImageBitmap(destbitmap);
}
}
好了,這部分關於在Mat和Bitmap上繪圖的知識就介紹完了,更多內容請參考:
下一篇: