Bitmap desc
=
new
Bitmap(source.Width, source.Height);
BitmapData sourcedata
=
source.LockBits(
new
Rectangle(
0
,
0
, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData descdata
=
desc.LockBits(
new
Rectangle(
0
,
0
, desc.Width, desc.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte
*
sourceptr
=
(
byte
*
)sourcedata.Scan0;
//
B,G,R
byte
*
descptr
=
(
byte
*
)descdata.Scan0;
int
step
=
source.Width
*
3
;
sourceptr
+=
step
*
2
;
descptr
+=
step
*
2
;
byte
[] pixel
=
new
byte
[
9
];
float
[,] mean
=
new
float
[
9
,
3
];
float
[,] var
=
new
float
[
9
,
3
];
int
n, m;
float
min;
for
(
int
x
=
2
; x
<
source.Height
-
2
; x
++
)
{
sourceptr
+=
6
;
descptr
+=
6
;
for
(
int
y
=
2
; y
<
source.Width
-
2
; y
++
)
{
for
(
int
color
=
0
; color
<
3
; color
++
)
{
pixel[
0
]
=
*
(sourceptr
-
(step
+
3
));
pixel[
1
]
=
*
(sourceptr
-
step);
pixel[
2
]
=
*
(sourceptr
-
(step
-
3
));
pixel[
3
]
=
*
(sourceptr
-
3
);
pixel[
4
]
=
*
(sourceptr);
pixel[
5
]
=
*
(sourceptr
+
3
);
pixel[
6
]
=
*
(sourceptr
+
(step
-
3
));
pixel[
7
]
=
*
(sourceptr
+
step);
pixel[
8
]
=
*
(sourceptr
+
(step
+
3
));
mean[
0
, color]
=
(
float
)(pixel[
0
]
+
pixel[
1
]
+
pixel[
2
]
+
pixel[
3
]
+
pixel[
4
]
+
pixel[
5
]
+
pixel[
6
]
+
pixel[
7
]
+
pixel[
8
])
/
9
;
var[
0
, color]
=
0
;
for
(n
=
0
; n
<
9
; n
++
)
var[
0
, color]
+=
pixel[n]
*
pixel[n]
-
mean[
0
, color]
*
mean[
0
, color];
pixel[
0
]
=
*
(sourceptr
-
(step
+
6
));
pixel[
1
]
=
*
(sourceptr
-
(step
+
3
));
pixel[
2
]
=
*
(sourceptr
-
6
);
pixel[
3
]
=
*
(sourceptr
-
3
);
pixel[
4
]
=
*
(sourceptr);
pixel[
5
]
=
*
(sourceptr
+
(step
-
6
));
pixel[
6
]
=
*
(sourceptr
+
(step
-
3
));
mean[
1
, color]
=
(
float
)(pixel[
0
]
+
pixel[
1
]
+
pixel[
2
]
+
pixel[
3
]
+
pixel[
4
]
+
pixel[
5
]
+
pixel[
6
])
/
7
;
var[
1
, color]
=
0
;
for
(n
=
0
; n
<
7
; n
++
)
var[
1
, color]
+=
pixel[n]
*
pixel[n]
-
mean[
1
, color]
*
mean[
1
, color];
pixel[
0
]
=
*
(sourceptr
-
(step
+
step
+
3
));
pixel[
1
]
=
*
(sourceptr
-
(step
+
step));
pixel[
2
]
=
*
(sourceptr
-
(step
+
step
-
3
));
pixel[
3
]
=
*
(sourceptr
-
(step
+
3
));
pixel[
4
]
=
*
(sourceptr
-
step);
pixel[
5
]
=
*
(sourceptr
-
(step
-
3
));
pixel[
6
]
=
*
(sourceptr);
mean[
2
, color]
=
(
float
)(pixel[
0
]
+
pixel[
1
]
+
pixel[
2
]
+
pixel[
3
]
+
pixel[
4
]
+
pixel[
5
]
+
pixel[
6
])
/
7
;
var[
2
, color]
=
0
;
for
(n
=
0
; n
<
7
; n
++
)
var[
2
, color]
+=
pixel[n]
*
pixel[n]
-
mean[
2
, color]
*
mean[
2
, color];
pixel[
0
]
=
*
(sourceptr
-
(step
-
3
));
pixel[
1
]
=
*
(sourceptr
-
(step
-
6
));
pixel[
2
]
=
*
(sourceptr);
pixel[
3
]
=
*
(sourceptr
+
3
);
pixel[
4
]
=
*
(sourceptr
+
6
);
pixel[
5
]
=
*
(sourceptr
+
(step
+
3
));
pixel[
6
]
=
*
(sourceptr
+
(step
+
6
));
mean[
3
, color]
=
(
float
)(pixel[
0
]
+
pixel[
1
]
+
pixel[
2
]
+
pixel[
3
]
+
pixel[
4
]
+
pixel[
5
]
+
pixel[
6
])
/
7
;
var[
3
, color]
=
0
;
for
(n
=
0
; n
<
7
; n
++
)
var[
3
, color]
+=
pixel[n]
*
pixel[n]
-
mean[
3
, color]
*
mean[
3
, color];
pixel[
0
]
=
*
(sourceptr);
pixel[
1
]
=
*
(sourceptr
+
(step
-
3
));
pixel[
2
]
=
*
(sourceptr
+
step);
pixel[
3
]
=
*
(sourceptr
+
(step
+
3
));
pixel[
4
]
=
*
(sourceptr
+
(step
+
step
-
3
));
pixel[
5
]
=
*
(sourceptr
+
step
+
step);
pixel[
6
]
=
*
(sourceptr
+
(step
+
step
+
3
));
mean[
4
, color]
=
(
float
)(pixel[
0
]
+
pixel[
1
]
+
pixel[
2
]
+
pixel[
3
]
+
pixel[
4
]
+
pixel[
5
]
+
pixel[
6
])
/
7
;
var[
4
, color]
=
0
;
for
(n
=
0
; n
<
7
; n
++
)
var[
4
, color]
+=
pixel[n]
*
pixel[n]
-
mean[
4
, color]
*
mean[
4
, color];
pixel[
0
]
=
*
(sourceptr
-
(step
+
step
+
6
));
pixel[
1
]
=
*
(sourceptr
-
(step
+
step
+
3
));
pixel[
2
]
=
*
(sourceptr
-
(step
+
6
));
pixel[
3
]
=
*
(sourceptr
-
(step
+
3
));
pixel[
4
]
=
*
(sourceptr
-
step);
pixel[
5
]
=
*
(sourceptr
-
3
);
pixel[
6
]
=
*
(sourceptr);
mean[
5
, color]
=
(
float
)(pixel[
0
]
+
pixel[
1
]
+
pixel[
2
]
+
pixel[
3
]
+
pixel[
4
]
+
pixel[
5
]
+
pixel[
6
])
/
7
;
var[
5
, color]
=
0
;
for
(n
=
0
; n
<
7
; n
++
)
var[
5
, color]
+=
pixel[n]
*
pixel[n]
-
mean[
5
, color]
*
mean[
5
, color];
pixel[
0
]
=
*
(sourceptr
-
(step
+
step
-
3
));
pixel[
1
]
=
*
(sourceptr
-
(step
+
step
-
6
));
pixel[
2
]
=
*
(sourceptr
-
step);
pixel[
3
]
=
*
(sourceptr
-
(step
-
3
));
pixel[
4
]
=
*
(sourceptr
-
(step
-
6
));
pixel[
5
]
=
*
(sourceptr);
pixel[
6
]
=
*
(sourceptr
+
3
);
mean[
6
, color]
=
(
float
)(pixel[
0
]
+
pixel[
1
]
+
pixel[
2
]
+
pixel[
3
]
+
pixel[
4
]
+
pixel[
5
]
+
pixel[
6
])
/
7
;
var[
6
, color]
=
0
;
for
(n
=
0
; n
<
7
; n
++
)
var[
6
, color]
+=
pixel[n]
*
pixel[n]
-
mean[
6
, color]
*
mean[
6
, color];
pixel[
0
]
=
*
(sourceptr);
pixel[
1
]
=
*
(sourceptr
+
3
);
pixel[
2
]
=
*
(sourceptr
+
step);
pixel[
3
]
=
*
(sourceptr
+
(step
+
3
));
pixel[
4
]
=
*
(sourceptr
+
(step
+
6
));
pixel[
5
]
=
*
(sourceptr
+
(step
+
step
+
3
));
pixel[
6
]
=
*
(sourceptr
+
(step
+
step
+
6
));
mean[
7
, color]
=
(
float
)(pixel[
0
]
+
pixel[
1
]
+
pixel[
2
]
+
pixel[
3
]
+
pixel[
4
]
+
pixel[
5
]
+
pixel[
6
])
/
7
;
var[
7
, color]
=
0
;
for
(n
=
0
; n
<
7
; n
++
)
var[
7
, color]
+=
pixel[n]
*
pixel[n]
-
mean[
7
, color]
*
mean[
7
, color];
pixel[
0
]
=
*
(sourceptr
-
3
);
pixel[
1
]
=
*
(sourceptr);
pixel[
2
]
=
*
(sourceptr
+
(step
-
6
));
pixel[
3
]
=
*
(sourceptr
+
(step
-
3
));
pixel[
4
]
=
*
(sourceptr
+
step);
pixel[
5
]
=
*
(sourceptr
+
(step
+
step
-
6
));
pixel[
6
]
=
*
(sourceptr
+
(step
+
step
-
3
));
mean[
8
, color]
=
(
float
)(pixel[
0
]
+
pixel[
1
]
+
pixel[
2
]
+
pixel[
3
]
+
pixel[
4
]
+
pixel[
5
]
+
pixel[
6
])
/
7
;
var[
8
, color]
=
0
;
for
(n
=
0
; n
<
7
; n
++
)
var[
8
, color]
+=
pixel[n]
*
pixel[n]
-
mean[
8
, color]
*
mean[
8
, color];
descptr
++
;
sourceptr
++
;
}
min
=
var[
0
,
0
]
+
var[
0
,
1
]
+
var[
0
,
2
];
m
=
0
;
for
(n
=
1
; n
<
9
; n
++
)
{
if
(min
>
var[n,
0
]
+
var[n,
1
]
+
var[n,
2
])
{
min
=
var[n,
0
]
+
var[n,
1
]
+
var[n,
2
];
m
=
n;
}
}
*
(descptr
-
3
)
=
(
byte
)(mean[m,
0
]);
*
(descptr
-
2
)
=
(
byte
)(mean[m,
1
]);
*
(descptr
-
1
)
=
(
byte
)(mean[m,
2
]);
}
sourceptr
+=
sourcedata.Stride
-
source.Width
*
3
+
6
;
descptr
+=
descdata.Stride
-
desc.Width
*
3
+
6
;
}
}
source.UnlockBits(sourcedata);
desc.UnlockBits(descdata);