■HelloWorld.java
package test;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.imgcodecs.Imgcodecs;
public class HelloWorld {
public static void fillColor(Mat mat, int r, int g, int b) {
for (int row = 0; row < mat.size().height; ++row) {
for (int col = 0; col < mat.size().width; ++col) {
mat.put(row, col, g, b, r);
}
}
return;
}
public static void pasteMat(Mat baseMat, Mat pasteMat, int pasteRow, int pasteCol) {
for (int row = 0; row < pasteMat.size().height; ++row) {
for (int col = 0; col < pasteMat.size().width; ++col) {
baseMat.put(row, col, pasteMat.get(row, col));
}
}
return;
}
public static void pasteMatTransparency(Mat baseMat, Mat pasteMat, int pasteRow, int pasteCol, int trR, int trG, int trB) {
for (int row = 0; row < pasteMat.size().height; ++row) {
for (int col = 0; col < pasteMat.size().width; ++col) {
double[] bgr = pasteMat.get(row, col);
if ((bgr[0] != trB) && (bgr[1] != trG) && (bgr[2] != trR)) {
baseMat.put(pasteRow + row, pasteCol + col, pasteMat.get(row, col));
}
}
}
return;
}
public static Image toBufferedImage(Mat m){
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return image;
}
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
ArrayList<ImageInfo> inputImageInfoList = new ArrayList<ImageInfo>();
inputImageInfoList.add(new ImageInfo(1024, 655, "sw/baijin_rw.png", 110, 120));
inputImageInfoList.add(new ImageInfo(1024, 655, "sw/baijin_rw.png", 210, 220));
inputImageInfoList.add(new ImageInfo(1024, 710, "sw/tokyo_sw.png", 210, 220));
inputImageInfoList.add(new ImageInfo(1024, 710, "sw/tokyo_sw.png", 310, 320));
inputImageInfoList.add(new ImageInfo(1024, 1032, "sw/paris_rw.png", 410, 420));
inputImageInfoList.add(new ImageInfo(1024, 1032, "sw/paris_rw.png", 510, 520));
String markImagePath = "sw/mark.png";
int markHotSpotX = 32;
int markHotSpotY = 63;
String outputImagePath = "result.png";
int frameR = 201;
int frameG = 202;
int frameB = 203;
int trR = 255;
int trG = 255;
int trB = 255;
Mat markMat = Imgcodecs.imread(markImagePath);
int frameCol = (int) markMat.size().width;
int frameRow = (int) markMat.size().height;
int outputImageHeight = 0;
String lastFilePath = "";
for (ImageInfo inputImageInfo : inputImageInfoList) {
if (! inputImageInfo.filePath.equals(lastFilePath)) {
lastFilePath = inputImageInfo.getFilePath();
outputImageHeight += frameRow + inputImageInfo.getImageY();
}
}
outputImageHeight += frameRow;;
int outputImageWidth = frameCol * 2 + inputImageInfoList.get(0).getImageX();
Mat outputMat = new Mat(outputImageHeight, outputImageWidth, CvType.CV_8UC3);
fillColor(outputMat, frameR, frameG, frameB);
lastFilePath = "";
Mat inputMat = new Mat();
int currRow = 0;
int nextRow = 0;
Mat roi = null;
for (ImageInfo inputImageInfo : inputImageInfoList) {
if (! inputImageInfo.filePath.equals(lastFilePath)) {
lastFilePath = inputImageInfo.getFilePath();
inputMat = Imgcodecs.imread(lastFilePath);
currRow = nextRow;
nextRow = (int) (currRow + frameRow + inputMat.size().height);
roi = new Mat(outputMat, new Rect(frameCol, currRow + frameRow, inputImageInfo.getImageX(), inputImageInfo.getImageY()));
pasteMat(roi, inputMat, currRow, frameCol);
}
pasteMatTransparency(roi, markMat, inputImageInfo.getMarkY() - markHotSpotY, inputImageInfo.getMarkX() - markHotSpotX, trR, trG, trB);
}
BufferedImage outputBufferedImage = new ImageTranslater().Mat2BI(outputMat);
try (OutputStream outputStream = new FileOutputStream(outputImagePath)) {
// 画像を出力する
ImageIO.write(outputBufferedImage, "png", outputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Done!");
}
}
■ImageInfo.java
package test;
public class ImageInfo {
int imageX;
int imageY;
String filePath;
int markX;
int markY;
public ImageInfo(int imageX, int imageY, String filePath, int markX, int markY) {
super();
this.imageX = imageX;
this.imageY = imageY;
this.filePath = filePath;
this.markX = markX;
this.markY = markY;
}
public int getImageX() {
return imageX;
}
public void setImageX(int imageX) {
this.imageX = imageX;
}
public int getImageY() {
return imageY;
}
public void setImageY(int imageY) {
this.imageY = imageY;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public int getMarkX() {
return markX;
}
public void setMarkX(int markX) {
this.markX = markX;
}
public int getMarkY() {
return markY;
}
public void setMarkY(int markY) {
this.markY = markY;
}
}
■ImageTranslater.java
package test;
import java.awt.image.BufferedImage;
import org.opencv.core.Mat;
/**
* javaとprocessingとopencvのための画像オブジェクト変換用クラス
* ビルド・パスにopencv-xxx.jar(openCV)とcore.jar(Processing)を追加しておいてください.
*
* @version 1.1
*/
public class ImageTranslater {
/**
* Mat型をBufferedImage型(TYPE_BYTE_GRAYまたはTYPE_3BYTE_BGR)に変換します
* @param mat 変換したいMat型
* @return 変換したBufferedImage
*/
public static BufferedImage MatToBufferedImageBGR(Mat mat) {
int dataSize = mat.cols() * mat.rows() * (int) mat.elemSize();
byte data[] = new byte[dataSize];
mat.get(0, 0, data);
int type;
if (mat.channels() == 1) {
type = BufferedImage.TYPE_BYTE_GRAY;
} else {
type = BufferedImage.TYPE_3BYTE_BGR;
for (int i = 0; i < dataSize; i += 3) {
byte blue = data[i + 0];
data[i + 0] = data[i + 2];
data[i + 2] = blue;
}
}
BufferedImage img = new BufferedImage(mat.cols(), mat.rows(), type);
img.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);
return img;
}
public BufferedImage Mat2BI(Mat mat) {
return MatToBufferedImageBGR(mat);
}
}