背景
深層学習を用いた水中画像からの浮遊物除去する研究しています、pix2pixHDをやってみたいです、ペアデータセットは必要ですから、自分で作成しました。
原理
空の画像に対し、さまざまな形状とサイズの125個の白色パターンをランダムに生成し、Gaussian Blurを用いてぼかすことで人工浮遊物画像を生成する。
code
void noise(Mat image, int n)// パターンをランダムに生成する関数
{
int i, j;
for (int k = 0; k < n; k++)
{
int r = rand() % 29;
// rand()は乱数生成器です。
if (r == 1)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 2)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 3)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 4)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 5)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 6)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 7)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 8)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 9)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 10)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 11)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 12)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i - 1, j - 1)[0] = 255;
image.at<Vec3b>(i - 1, j - 1)[1] = 228;
image.at<Vec3b>(i - 1, j - 1)[2] = 181;
image.at<Vec3b>(i + 1, j + 1)[0] = 255;
image.at<Vec3b>(i + 1, j + 1)[1] = 228;
image.at<Vec3b>(i + 1, j + 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 13)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j - 1)[0] = 255;
image.at<Vec3b>(i + 1, j - 1)[1] = 228;
image.at<Vec3b>(i + 1, j - 1)[2] = 181;
image.at<Vec3b>(i - 1, j + 1)[0] = 255;
image.at<Vec3b>(i - 1, j + 1)[1] = 228;
image.at<Vec3b>(i - 1, j + 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 14)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 15)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 16)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 17)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 18)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i - 1, j - 1)[0] = 255;
image.at<Vec3b>(i - 1, j - 1)[1] = 228;
image.at<Vec3b>(i - 1, j - 1)[2] = 181;
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 19)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i - 1, j + 1)[0] = 255;
image.at<Vec3b>(i - 1, j + 1)[1] = 228;
image.at<Vec3b>(i - 1, j + 1)[2] = 181;
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 20)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j + 1)[0] = 255;
image.at<Vec3b>(i + 1, j + 1)[1] = 228;
image.at<Vec3b>(i + 1, j + 1)[2] = 181;
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 21)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j - 1)[0] = 255;
image.at<Vec3b>(i + 1, j - 1)[1] = 228;
image.at<Vec3b>(i + 1, j - 1)[2] = 181;
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 22)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 23)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j - 1)[0] = 255;
image.at<Vec3b>(i + 1, j - 1)[1] = 228;
image.at<Vec3b>(i + 1, j - 1)[2] = 181;
image.at<Vec3b>(i + 1, j + 1)[0] = 255;
image.at<Vec3b>(i + 1, j + 1)[1] = 228;
image.at<Vec3b>(i + 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j + 1)[0] = 255;
image.at<Vec3b>(i - 1, j + 1)[1] = 228;
image.at<Vec3b>(i - 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j - 1)[0] = 255;
image.at<Vec3b>(i - 1, j - 1)[1] = 228;
image.at<Vec3b>(i - 1, j - 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 24)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i + 1, j - 1)[0] = 255;
image.at<Vec3b>(i + 1, j - 1)[1] = 228;
image.at<Vec3b>(i + 1, j - 1)[2] = 181;
image.at<Vec3b>(i + 1, j + 1)[0] = 255;
image.at<Vec3b>(i + 1, j + 1)[1] = 228;
image.at<Vec3b>(i + 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j + 1)[0] = 255;
image.at<Vec3b>(i - 1, j + 1)[1] = 228;
image.at<Vec3b>(i - 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j - 1)[0] = 255;
image.at<Vec3b>(i - 1, j - 1)[1] = 228;
image.at<Vec3b>(i - 1, j - 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 25)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i + 1, j)[0] = 255;
image.at<Vec3b>(i + 1, j)[1] = 228;
image.at<Vec3b>(i + 1, j)[2] = 181;
image.at<Vec3b>(i + 1, j - 1)[0] = 255;
image.at<Vec3b>(i + 1, j - 1)[1] = 228;
image.at<Vec3b>(i + 1, j - 1)[2] = 181;
image.at<Vec3b>(i + 1, j + 1)[0] = 255;
image.at<Vec3b>(i + 1, j + 1)[1] = 228;
image.at<Vec3b>(i + 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j + 1)[0] = 255;
image.at<Vec3b>(i - 1, j + 1)[1] = 228;
image.at<Vec3b>(i - 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j - 1)[0] = 255;
image.at<Vec3b>(i - 1, j - 1)[1] = 228;
image.at<Vec3b>(i - 1, j - 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 26)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i - 1, j)[0] = 255;
image.at<Vec3b>(i - 1, j)[1] = 228;
image.at<Vec3b>(i - 1, j)[2] = 181;
image.at<Vec3b>(i + 1, j - 1)[0] = 255;
image.at<Vec3b>(i + 1, j - 1)[1] = 228;
image.at<Vec3b>(i + 1, j - 1)[2] = 181;
image.at<Vec3b>(i + 1, j + 1)[0] = 255;
image.at<Vec3b>(i + 1, j + 1)[1] = 228;
image.at<Vec3b>(i + 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j + 1)[0] = 255;
image.at<Vec3b>(i - 1, j + 1)[1] = 228;
image.at<Vec3b>(i - 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j - 1)[0] = 255;
image.at<Vec3b>(i - 1, j - 1)[1] = 228;
image.at<Vec3b>(i - 1, j - 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 27)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i, j - 1)[0] = 255;
image.at<Vec3b>(i, j - 1)[1] = 228;
image.at<Vec3b>(i, j - 1)[2] = 181;
image.at<Vec3b>(i + 1, j - 1)[0] = 255;
image.at<Vec3b>(i + 1, j - 1)[1] = 228;
image.at<Vec3b>(i + 1, j - 1)[2] = 181;
image.at<Vec3b>(i + 1, j + 1)[0] = 255;
image.at<Vec3b>(i + 1, j + 1)[1] = 228;
image.at<Vec3b>(i + 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j + 1)[0] = 255;
image.at<Vec3b>(i - 1, j + 1)[1] = 228;
image.at<Vec3b>(i - 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j - 1)[0] = 255;
image.at<Vec3b>(i - 1, j - 1)[1] = 228;
image.at<Vec3b>(i - 1, j - 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
if (r == 28)
{
i = random(5, 250);//行
j = random(5, 250);//列
image.at<Vec3b>(i, j + 1)[0] = 255;
image.at<Vec3b>(i, j + 1)[1] = 228;
image.at<Vec3b>(i, j + 1)[2] = 181;
image.at<Vec3b>(i + 1, j - 1)[0] = 255;
image.at<Vec3b>(i + 1, j - 1)[1] = 228;
image.at<Vec3b>(i + 1, j - 1)[2] = 181;
image.at<Vec3b>(i + 1, j + 1)[0] = 255;
image.at<Vec3b>(i + 1, j + 1)[1] = 228;
image.at<Vec3b>(i + 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j + 1)[0] = 255;
image.at<Vec3b>(i - 1, j + 1)[1] = 228;
image.at<Vec3b>(i - 1, j + 1)[2] = 181;
image.at<Vec3b>(i - 1, j - 1)[0] = 255;
image.at<Vec3b>(i - 1, j - 1)[1] = 228;
image.at<Vec3b>(i - 1, j - 1)[2] = 181;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 228;
image.at<Vec3b>(i, j)[2] = 181;
}
}
}
int main()
{
cv::String path = "C:\\testeed";
cv::String savedfilename;
std::vector<cv::String> filenames;
cv::Mat srcImg;
Mat gaussian;
Mat dst;
cv::glob(path, filenames);
for (int i = 0; i < filenames.size(); i++)
{
srcImg = cv::imread(filenames[i]);
Mat y(srcImg.rows, srcImg.cols, CV_8UC3, Scalar(0, 0, 0)); //ターゲット画像と同じサイズの全黒3チャンネルの画像を定義します。
noise(y, 125);//関数をコールする
GaussianBlur(y, gaussian, Size(3, 3), 0, 0);//GaussianBlurにします
addWeighted(srcImg, 1.0, gaussian, 1.0, 0.0, dst);//マスク画像足す原画像
savedfilename = "C:\\testeed\\" + to_string(i) + ".jpg";
std::cout << savedfilename << std::endl;
cv::imwrite(savedfilename, dst);
}
return 0;
}