0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

行列積(数学)の計算、行列の積(画像処理)の計算

Posted at

はじめに

目新しい知見はありません。

数学における行列積がある一方で、
画像を行列として扱う環境(OpenCV)が存在する。

後者の積は、ピクセルごとの単純な積となっている。
無意識的に別々のものとして理解していたけど、翌々かんがえると、行列を2通りに使うことが、変な気がしたので、行列積を可視化しました。

行列

CGでは4x4行列がよく使われます。
AIの分野では、かなり大きな行列が使われます。(pythonのnumpy)
(モノクロ)画像はそのまま行列としても扱うことができます。(OpenCVのcv::Mat)

環境 演算(子) 演算内容
Python OpenCV * 要素ごとの積
Python OpenCV m.dot(n) 行列積
C++ OpenCV Mat m.mul(n) 要素ごとの積
C++ OpenCV Mat * 行列積
Processing OpenCV m.mul(n) 要素ごとの積
Processing OpenCV m.matMul(n) 行列積
Processing PMatrix m.apply(n) 行列積

OpenCVにおいて、行列同士の積は各ピクセルの値同士の掛け算になります。
Java(Processing)は演算子のオーバーライドができません。

利用価値がないので、画像(2次元配列)を画像と行列の双方としてみなす環境はないみたいです。

行列積の計算例と可視化

Processing
PMatrix3D mat1 = new PMatrix3D();
PMatrix3D mat2 = new PMatrix3D();
mat1.set( 1,-1,-1, 1,
         -1, 1, 1,-1,
         -1, 1, 1,-1,
          1,-1,-1, 1);
mat2.set( 1, 1, 1, 1,
          0, 0, 1, 0,
          0, 1, 0, 0,
          1, 1, 1, 1);
mat1.apply(mat2);
mat1.print();
小数点以下削除
 2  1  1  2
-2 -1 -1 -2
-2 -1 -1 -2
 2  1  1  2

image.png

90度回転行列と可視化

Processing
PMatrix3D mat1 = new PMatrix3D();
PMatrix3D mat2 = new PMatrix3D();
mat1.set( 1,-1,-1, 1,
         -1, 1, 1,-1,
         -1, 1, 1,-1,
          1,-1,-1, 1);
mat2.set( 0,-1, 0, 0,
          1, 0, 0, 0,
          0, 0, 1, 0,
          0, 0, 0, 1);
mat1.apply(mat2);
mat1.print();
-1.0000 -1.0000 -1.0000  1.0000
 1.0000  1.0000  1.0000 -1.0000
 1.0000  1.0000  1.0000 -1.0000
-1.0000 -1.0000 -1.0000  1.0000

image.png

OpenCVで行列の積

以下の結果から、
グレー(127)×白(255) = 32385 -> 飽和して255
白(255)×グレー(127) = 32385 -> 飽和して255
となって、グレーにはならない。

元画像 積算する画像 結果画像
triangle.png check.png result.png
Processing OpenCV
import gab.opencv.*;
import org.opencv.core.Mat;
//import org.opencv.core.Core;

size(400, 400);

PImage tri, chk;
OpenCV ocvT, ocvC;

tri = loadImage("triangle.png");
chk = loadImage("check.png");
ocvT = new OpenCV(this, tri);
ocvC = new OpenCV(this, chk);
ocvT.gray();
ocvC.gray();

Mat matT = ocvT.getGray();
Mat matC = ocvC.getGray();
matT = matT.mul(matC);
//matT = matT.matMul(matC);
//Core.gemm(matT, matC, 1, new Mat(), 0, matT);

PImage disp = createImage(400, 400, RGB);
ocvT.toPImage( matT, disp);
image(disp, 0, 0);

matMul命令やgemmで、行列式として無理矢理計算しようとしても、フォーマットの指定があるみたいで、いろいろやってもうまくいかなかったので、諦めた。

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?