mecobalamin’s diary

人間万事塞翁が馬

https://help.hatenablog.com/entry/developer-option

OpenCVを使って物体認識その3、不正解データの作成

猫の顔を認識させたい
mecobalamin.hatenablog.com

この続き

不正解画像を作成する
ImageJのコードはほぼ同じ

入力画像を工夫する
オリジナルの画像を反転させて使ってみる
f:id:mecobalamin:20200117185200j:plain

2 July 2020 追記
オリジナル画像を反転させて使ったのは
このとき手元にちょうどよい画像のセットがなかったため

ImageNetから画像をダウンロードして使うのが良いと思う
mecobalamin.hatenablog.com
追記ここまで


ROIで切り抜いた画像
f:id:mecobalamin:20200117194300j:plain

コードは以下の通り

// --- Reset status ---
String.resetBuffer;
List.clear;
run("Clear Results");

// --- Set Directories ---
inputdirectory = getDirectory("Select a Directory of Input Images");
WorkingDirectory = File.getParent(inputdirectory);
OutputDirectory = WorkingDirectory+"\\_NegativeImages\\";
RoiDirectory = WorkingDirectory + "\\_Rois\\";
TextDirectory = WorkingDirectory + "\\_Text\\";
RoiList = getFileList(RoiDirectory);
ImageList = getFileList(inputdirectory);

// --- Create text window for cascade file---
TextFileTitle = "negative.txt";
WindowTitle = "["+TextFileTitle+"]";
if (isOpen(TextFileTitle)){
	print(WindowTitle, "\\Update:");
}else{
	run("Text Window...", "name="+WindowTitle+" width=80 height=20 menu");
}

selectWindow(TextFileTitle);
saveAs("text", TextDirectory + "\\" + TextFileTitle);
//saveAs("text", WorkingDirectory + "\\" + TextFileTitle);

// ---Create Output Directory---
File.makeDirectory(OutputDirectory);
File.makeDirectory(TextDirectory);

// --- Set Batch mode ---
setBatchMode(true);
print("\\Clear");

// --- add dummy ROI to clear ROI manager ---
newImage("dummyROI", "32-bit", 512, 512, 1);
makeRectangle(0, 0, 100, 100);
roiManager("add");
close("dummyROI");

// ---- MAIN ----
print("Crop Cat Faces by ROIs.");

// Compare list sizes.
// the Macro will stop if list sizes are different.
if(RoiList.length != ImageList.length){
	setBatchMode("exit and display");
	print("File numbers are not same.\nPlease confirm number of files in each directories.");
	print("Macro finished.");
	selectWindow(TextFileTitle);
	close(TextFileTitle);
	exit("File numbers are not same.\nPlease cofirm number of files");
}

// Repeat trimming and saving images for number of images in imput directory.
for (i = 0; i < ImageList.length; i++){
	roiManager("Deselect");
	roiManager("Delete");

	roiManager("Open", RoiDirectory + RoiList[i]);
	numRoi = roiManager("count");
	open(inputdirectory + ImageList[i]);

	// Flip horizontally and vertically
	run("Flip Horizontally");
	run("Flip Vertically");

	for (j = 0; j < numRoi; j++){

		// trimming and saving image
		RoiInfo = CropROIs(j, ImageList[i]);
		cropfname = "negative_" + RoiInfo[0];
		selectWindow(RoiInfo[0]);
		rename(cropfname);
		selectWindow(cropfname);
		saveAs("jpeg", OutputDirectory + cropfname);
		close(cropfname);

		// show statistics for saving
		print(WindowTitle, OutputDirectory + cropfname + "\n");
		run("Clear Results");
	}
	close(ImageList[i]);
}

roiManager("Deselect");
roiManager("Delete");

// --- save text file ---
selectWindow(TextFileTitle);
saveAs("text", TextDirectory + "\\" + TextFileTitle);
close(TextFileTitle);

// --- Exit Batch mode ---
setBatchMode("exit and display");
print("operation completed.");

// --- end of main --- //


// --- --------- --- //
// --- functions --- //
// --- --------- --- //

// --- Crop image by Rois --- //
function CropROIs(n, fname){
	selectWindow(fname);
	DuplicateName = "duplicate";

	roiManager("Select", n);
	roiname = Roi.getName;

	// get statistics inside ROI
	getStatistics(area, mean, min, max, std);

	// duplicate image with ROI
	run("Duplicate...", "title="+DuplicateName+" duplicate");
	RoiX = getWidth();
	RoiY = getHeight();

	// convert outside ROI to black
	selectWindow("duplicate");
	// CopyonBlackimage("duplicate", RoiX, RoiY);

	// transform image size
	if(RoiX >= RoiY){
		RoiWidth = 256;
		RoiHeight = round(256 / RoiX * RoiY);
	} else {
		RoiWidth = round(256 / RoiY * RoiX);
		RoiHeight = 256;
	}

	selectWindow(DuplicateName);
	run("Size...", "width=RoiWidth height=RoiHeight constrain average interpolation=None");
	run("Canvas Size...", "width=256 height=256 position=Center");

	// add ROI name in file name
	cropfname = replace(fname, ".jpg", "_" + roiname + ".jpg");
	rename(cropfname);

	// get position of ROI on image.
	PositionX = round(128 - RoiWidth / 2);
	PositionY = round(128 - RoiHeight / 2);
	RoiInfo = newArray(cropfname, PositionX, PositionY, RoiWidth, RoiHeight, mean);

	// return image data by Array.
	return RoiInfo;
}

// --- copy cropped image on black background --- //
function CopyonBlackimage(fname, RoiX, RoiY){
	newImage("tmpImage", "RGB black", RoiX, RoiY, 1);
	selectWindow("duplicate");
	run("Copy");
	selectWindow("tmpImage");
	run("Paste");
	selectWindow("duplicate");
	close(fname);
	selectWindow("tmpImage");
	rename(fname);
}

続きます

22 February 2020 追記
不正解画像に使えそうなサイトがあった
というか機械学習用に画像を集めているサイト
URLからダウンロードするスクリプトを書いた
mecobalamin.hatenablog.com
追記ここまで