前言
Python代码
def LCC_ColorImg(src):
rows = np.shape(src)[0]
cols = np.shape(src)[1]
I = np.arange(rows * cols).reshape((rows, cols))
inv_I = np.arange(rows * cols).reshape((rows, cols))
Mast = np.empty([rows, cols, 1], dtype=np.uint8)
for i in range(0, rows):
data1 = Mast[i]
for j in range(0, cols):
I[i][j] = (src[i, j][0] + src[i, j][1] + src[i, j][2]) / 3.0
inv_I[i][j] = 255
data1[j] = inv_I[i][j] - I[i][j]
Mast = cv2.GaussianBlur(Mast, (41, 41), 0)
dst = np.empty([rows, cols, 3], dtype=np.uint8)
for i in range(0, rows):
data2 = Mast[i]
for j in range(0, cols):
for k in range(0, 3):
Exp = math.pow(2, (128 - data2[j]) / 128.0)
value = int(255 * math.pow(src[i, j][k] / 255.0, Exp))
dst[i, j][k] = value
return dst
参考的C++代码
Mat LCC(const Mat &src){
int rows = src.rows;
int cols = src.cols;
int **I;
I = new int *[rows];
for(int i = 0; i < rows; i++){
I[i] = new int [cols];
}
int **inv_I;
inv_I = new int *[rows];
for(int i = 0; i < rows; i++){
inv_I[i] = new int [cols];
}
Mat Mast(rows, cols, CV_8UC1);
for(int i = 0; i < rows; i++){
uchar *data = Mast.ptr<uchar>(i);
for(int j = 0; j < cols; j++){
I[i][j] = (src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[1]) / 3.0;
inv_I[i][j] = 255;
*data = inv_I[i][j] - I[i][j];
data++;
}
}
GaussianBlur(Mast, Mast, Size(41, 41), BORDER_DEFAULT);
Mat dst(rows, cols, CV_8UC3);
for(int i = 0; i < rows; i++){
uchar *data = Mast.ptr<uchar>(i);
for(int j = 0; j < cols; j++){
for(int k = 0; k < 3; k++){
float Exp = pow(2, (128 - data[j]) / 128.0);
int value = int(255 * pow(src.at<Vec3b>(i, j)[k] / 255.0, Exp));
dst.at<Vec3b>(i, j)[k] = value;
}
}
}
return dst;
}
|