using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
public delegate void ExampleCallback(Bitmap outBit, PictureBox p1);
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
int tc = 0;
//Bitmap lastB;
public Form1()
{
InitializeComponent();
}
class pictFilter
{
Random rnd = new Random();
Bitmap pictOut = null;
int h;//絵の高さ
int w;//絵の横幅
Bitmap pictIn;
ExampleCallback callback;
PictureBox lastPict;
cell[, ,] cells;
cell[, ,] cells2;
int cell_size;
int size;
int id=0;//再帰関数で使うための名札付け
public pictFilter()
{
h = 0;
w = 0;
}
public Bitmap Color_SandPlay_Filter(Bitmap b1, Bitmap b2, int c_size)
{
cell_size = c_size;
size = 255 / cell_size + 2;
cells = new cell[size, size, size];
cells2 = new cell[size, size, size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
for (int k = 0; k < size; k++)
{
cells[i, j, k] = new cell();
cells2[i, j, k] = new cell();
}
}
}
Color C1, C2;
Bitmap b3 = new Bitmap(b2.Width, b2.Height);
//List<cell> cell_list = new List<cell>(); //
//Color_SandPlay_Filterメソッド
for (int i = 0; i < b1.Width; i++)
{
for (int j = 0; j < b1.Height; j++)
{
C1 = b1.GetPixel(i, j);
cells[get_cell_point1(C1.R), get_cell_point1(C1.G), get_cell_point1(C1.B)].add(C1.R, C1.G, C1.B);
}
}
for (int i = 0; i < b2.Width; i++)
{
for (int j = 0; j < b2.Height; j++)
{
C2 = b2.GetPixel(i, j);
cells2[get_cell_point1(C2.R), get_cell_point1(C2.G), get_cell_point1(C2.B)].add(C2.R, C2.G, C2.B);
}
}
//Color_SandPlay_Filterメソッド
//砂山遊びの局地的な山頂を求める処理
List<cell> maximun_cell_list = new List<cell>(); //
List<cell> maximun_cell_list2 = new List<cell>(); //
List<cellPoint> cellPoints1 = new List<cellPoint>();
List<cellPoint> cellPoints2 = new List<cellPoint>();
for (int i = 1; i < size-1; i++)
{
for (int j = 1; j < size-1; j++)
{
for (int k = 1; k < size-1; k++)
{
if (check_local_maximun(ref cells, i, j, k) == true)
{
maximun_cell_list.Add(cells[i, j, k]);
cellPoints1.Add(new cellPoint(i, j, k));
}
if (check_local_maximun(ref cells2, i, j, k) == true)
{
maximun_cell_list2.Add(cells2[i, j, k]);
cellPoints2.Add(new cellPoint(i, j, k));
}
}
}
}
id = 1;
cellPoint cP;
cell mCell;
cell aveCell = new cell();
List<cell> IdCellAveList = new List<cell>();
for(int i=0;i<maximun_cell_list.Count;i++ ){
cP=cellPoints1[i];
mCell=maximun_cell_list[i] ;
aveCell = new cell();
aveCell.id = id;
search_Cells(ref cells, cP.rPoint, cP.gPoint, cP.bPoint,mCell.getCount()+1,ref aveCell);
IdCellAveList.Add(aveCell);
id++;
}
id = 1;
List<cell> IdCellAveList2 = new List<cell>();
for (int i = 0; i < maximun_cell_list2.Count; i++)
{
cP = cellPoints2[i];
mCell = maximun_cell_list2[i];
aveCell = new cell();
aveCell.id = id;
search_Cells(ref cells2, cP.rPoint, cP.gPoint, cP.bPoint, mCell.getCount(),ref aveCell);
IdCellAveList2.Add(aveCell);
id++;
}
public bool check_local_maximun(ref cell[,,] mycell,int rPoint,int gPoint,int bPoint)
{
if (rPoint == 0 || gPoint == 0 || bPoint == 0)
{
return false;
}
if (rPoint >= size-1 || gPoint >= size-1 || bPoint >= size-1)
{
return false;
}
int u = mycell[rPoint + 1 , gPoint , bPoint ].getCount() ;
int d = mycell[rPoint - 1 , gPoint , bPoint ].getCount() ;
int l = mycell[rPoint , gPoint+1 , bPoint ].getCount();
int r = mycell[rPoint , gPoint-1 , bPoint ].getCount();
int f = mycell[rPoint , gPoint , bPoint+1 ].getCount();
int b = mycell[rPoint , gPoint , bPoint-1 ].getCount();
int c = mycell[rPoint, gPoint, bPoint].getCount() ;
if (c > u && c > d && c > l && c > r && c > f && c > b && c>10)
{
return true;
}
else
{
return false;
}
}
public int get_cell_point1(int rgb)
{
return (rgb / cell_size) + 1;
}
public int get_cell_point(int rgb)
{
return (rgb / cell_size) +2;
}
public pictFilter(Bitmap b1, ExampleCallback callbackDelegate, PictureBox p1)
{
this.pictIn = b1;
h = 0;
w = 0;
callback = callbackDelegate;
lastPict = p1;
}
private int cutNum(int min, int max, int t)
{
if (t < min)
{
t = min;
}
if (t > max)
{
t = max;
}
return t;
}
}
public class cell
{
public int sumR = 0;
public int sumG = 0;
public int sumB = 0;
int count = 0;
public int id=0;//再起関数で使うための変数、ラベリング処理用
public int getCount()
{
return count;
}
public void add(int r, int g, int b)
{
count++;
sumR += r;
sumG += g;
sumB += b;
}
public void add(Color C)
{
count++;
sumR += C.R;
sumG += C.G;
sumB += C.B;
}
public void add(cell c)
{
count += c.getCount();
sumR += c.sumR;
sumG += c.sumG;
sumB += c.sumB;
}
public Color aveColor()
{
if (count > 0)
{
return Color.FromArgb(
(int)sumR/count,
(int)sumG/count,
(int)sumB/count) ;
}else{
return (Color.FromArgb(0,0,0) );
}
}
}
public class cellPoint
{
public int rPoint;
public int gPoint;
public int bPoint;
public class cellIDAveAndDev{
public int aveR=0;
public int aveG=0;
public int aveB=0;
public double devR=0;
public double devG=0;
public double devB=0;
public int id;
public cellIDAveAndDev(int myid)
{
id=myid ;
}
public void setAve(Color C){
aveR = C.R;
aveG = C.G;
aveB = C.B;
}
}