Image-Contrast-Enhancement
Image-Contrast-Enhancement copied to clipboard
python implementation do not work
opencv-python==3.4.2.17 numpy==1.19.1
function output image is black image
def CELA(img): HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV_FULL) HSV_channels = cv2.split(HSV) V = HSV_channels[2]
ksize = 5
gauker1 = cv2.getGaussianKernel(ksize, 15)
gauker2 = cv2.getGaussianKernel(ksize, 80)
gauker3 = cv2.getGaussianKernel(ksize, 250)
gauV1 = cv2.filter2D(V, cv2.CV_8U, gauker1, anchor=(-1, -1), delta=0, borderType=cv2.BORDER_CONSTANT)
gauV2 = cv2.filter2D(V, cv2.CV_8U, gauker2, anchor=(-1, -1), delta=0, borderType=cv2.BORDER_CONSTANT)
gauV3 = cv2.filter2D(V, cv2.CV_8U, gauker3, anchor=(-1, -1), delta=0, borderType=cv2.BORDER_CONSTANT)
lut = np.zeros(256, np.uint8)
for i in range(256):
lut[i] = 17.0 * (1. - math.sqrt(i/127.)) + 3. if i <= 127 else 3. / 128. *(i - 127.) + 3.
lut[i] = (-lut[i] + 20.)/17.
beta1 = cv2.LUT(gauV1, lut)
beta2 = cv2.LUT(gauV2, lut)
beta3 = cv2.LUT(gauV3, lut)
gauV1 = np.float64(gauV1/255.)
gauV2 = np.float64(gauV2/255.)
gauV3 = np.float64(gauV3/255.)
V = np.float64(V/255.)
V = cv2.log(V)
gauV1 = cv2.log(gauV1)
gauV2 = cv2.log(gauV2)
gauV3 = cv2.log(gauV3)
r = (3. * V - beta1 * gauV1 - beta2 * gauV2 - beta3 * gauV3) / 3.0
R = cv2.exp(r)
R_min, R_max = np.min(R), np.max(R)
V_w = (R - R_min)/ (R_max - R_min)
V_w = np.uint8(V_w*255)
histsize = 256
hist = cv2.calcHist([V_w], [0], None, [histsize], [0, 255], accumulate=False)
pdf = hist / img.size
pdf_min, pdf_max = np.min(pdf), np.max(pdf)
for i in range(histsize):
pdf[i] = pdf_max * (pdf[i] - pdf_min) / (pdf_max - pdf_min)
cdf = [0] * 256
accum = 0
for i in range(255):
accum += pdf[i]
cdf[i] = accum
cdf[255] = 1. - accum
V_w_max = np.max(V_w)
for i in range(255):
lut[i] = V_w_max * math.pow((i *1./V_w_max), 1. - cdf[i])
V_out = cv2.LUT(V_w, lut)
V_out = np.uint8(V_out)
HSV_channels[2] = V_out
HSV = cv2.merge(HSV_channels)
return cv2.cvtColor(HSV, cv2.COLOR_HSV2BGR_FULL)
Hi, after testing your python codes, there are several things I want to tell you. (1) Your code : lut = np.zeros(256, np.uint8) My code : std::vector<double> lut(256, 0); So I think your code should be changed to ==> lut = np.zeros(256, np.float64)
(2) Your code : R_min, R_max = np.min(R), np.max(R) My code : double R_min, R_max; cv::minMaxLoc(R, &R_min, &R_max); I compared the R_min and R_max in your code and my code, they are not the same. So I changed your code to ==> R_min, R_max, _, _ = cv2.minMaxLoc(R)
After (1)(2), your code works. However, the result from your code is darker than mine.
(3) I think the problem is here: hist = cv2.calcHist([V_w], [0], None, [histsize], [0, 256], accumulate=False)
The values in "hist" in yours are quite different from mine. The values in "hist" in my code are: hist[0] = 634 hist[1] = 2977 hist[2] = 9580 hist[3] = 16491 hist[4] = 15547 hist[5] = 19788 hist[6] = 20093 hist[7] = 18034 hist[8] = 12600 hist[9] = 13882 hist[10] = 14399 hist[11] = 13701 hist[12] = 12829 hist[13] = 8249 hist[14] = 10812 hist[15] = 9448 hist[16] = 9863 hist[17] = 7454 hist[18] = 5484 hist[19] = 6540 hist[20] = 6272 hist[21] = 5275 hist[22] = 3706 hist[23] = 5568 hist[24] = 6036 hist[25] = 4589 hist[26] = 3747 hist[27] = 6538 hist[28] = 6481 hist[29] = 4746 hist[30] = 3585 hist[31] = 6326 hist[32] = 6670 hist[33] = 4145 hist[34] = 2886 hist[35] = 5819 hist[36] = 5726 hist[37] = 5321 hist[38] = 868 hist[39] = 4197 hist[40] = 4287 hist[41] = 3975 hist[42] = 3741 hist[43] = 381 hist[44] = 3216 hist[45] = 2947 hist[46] = 2878 hist[47] = 2675 hist[48] = 735 hist[49] = 2231 hist[50] = 2333 hist[51] = 2303 hist[52] = 2225 hist[53] = 1672 hist[54] = 1394 hist[55] = 1643 hist[56] = 1675 hist[57] = 1723 hist[58] = 1583 hist[59] = 1642 hist[60] = 1560 hist[61] = 1608 hist[62] = 1732 hist[63] = 1476 hist[64] = 1637 hist[65] = 1489 hist[66] = 1689 hist[67] = 1583 hist[68] = 1574 hist[69] = 1667 hist[70] = 1565 hist[71] = 1610 hist[72] = 1544 hist[73] = 1803 hist[74] = 1507 hist[75] = 1654 hist[76] = 1447 hist[77] = 1573 hist[78] = 1556 hist[79] = 1465 hist[80] = 1607 hist[81] = 1461 hist[82] = 1587 hist[83] = 1494 hist[84] = 1665 hist[85] = 1392 hist[86] = 1485 hist[87] = 1227 hist[88] = 1469 hist[89] = 1503 hist[90] = 1409 hist[91] = 1342 hist[92] = 1404 hist[93] = 1492 hist[94] = 1463 hist[95] = 1503 hist[96] = 1456 hist[97] = 1498 hist[98] = 1618 hist[99] = 1783 hist[100] = 1706 hist[101] = 1680 hist[102] = 1915 hist[103] = 1770 hist[104] = 1763 hist[105] = 1705 hist[106] = 2020 hist[107] = 2266 hist[108] = 3253 hist[109] = 4155 hist[110] = 4461 hist[111] = 3619 hist[112] = 3934 hist[113] = 1855 hist[114] = 1285 hist[115] = 998 hist[116] = 879 hist[117] = 651 hist[118] = 573 hist[119] = 455 hist[120] = 443 hist[121] = 359 hist[122] = 348 hist[123] = 323 hist[124] = 285 hist[125] = 230 hist[126] = 222 hist[127] = 231 hist[128] = 186 hist[129] = 155 hist[130] = 148 hist[131] = 147 hist[132] = 125 hist[133] = 120 hist[134] = 99 hist[135] = 121 hist[136] = 99 hist[137] = 92 hist[138] = 97 hist[139] = 78 hist[140] = 88 hist[141] = 75 hist[142] = 71 hist[143] = 60 hist[144] = 61 hist[145] = 57 hist[146] = 62 hist[147] = 43 hist[148] = 43 hist[149] = 52 hist[150] = 43 hist[151] = 40 hist[152] = 36 hist[153] = 41 hist[154] = 33 hist[155] = 36 hist[156] = 26 hist[157] = 21 hist[158] = 28 hist[159] = 23 hist[160] = 31 hist[161] = 18 hist[162] = 18 hist[163] = 19 hist[164] = 15 hist[165] = 21 hist[166] = 14 hist[167] = 13 hist[168] = 6 hist[169] = 5 hist[170] = 11 hist[171] = 12 hist[172] = 12 hist[173] = 5 hist[174] = 12 hist[175] = 8 hist[176] = 9 hist[177] = 11 hist[178] = 7 hist[179] = 7 hist[180] = 11 hist[181] = 8 hist[182] = 9 hist[183] = 10 hist[184] = 9 hist[185] = 8 hist[186] = 2 hist[187] = 5 hist[188] = 7 hist[189] = 4 hist[190] = 5 hist[191] = 7 hist[192] = 7 hist[193] = 7 hist[194] = 5 hist[195] = 4 hist[196] = 2 hist[197] = 6 hist[198] = 2 hist[199] = 6 hist[200] = 0 hist[201] = 4 hist[202] = 0 hist[203] = 3 hist[204] = 3 hist[205] = 2 hist[206] = 3 hist[207] = 2 hist[208] = 3 hist[209] = 5 hist[210] = 5 hist[211] = 1 hist[212] = 2 hist[213] = 1 hist[214] = 1 hist[215] = 1 hist[216] = 1 hist[217] = 2 hist[218] = 1 hist[219] = 2 hist[220] = 0 hist[221] = 2 hist[222] = 3 hist[223] = 2 hist[224] = 1 hist[225] = 0 hist[226] = 2 hist[227] = 0 hist[228] = 0 hist[229] = 1 hist[230] = 0 hist[231] = 0 hist[232] = 2 hist[233] = 1 hist[234] = 0 hist[235] = 2 hist[236] = 2 hist[237] = 2 hist[238] = 1 hist[239] = 1 hist[240] = 1 hist[241] = 0 hist[242] = 0 hist[243] = 1 hist[244] = 0 hist[245] = 2 hist[246] = 0 hist[247] = 0 hist[248] = 0 hist[249] = 0 hist[250] = 0 hist[251] = 0 hist[252] = 1 hist[253] = 0 hist[254] = 0 hist[255] = 1
I will not show yours here, but the values with lower indices in "hist" in yours are quite larger than mine, so the result in yours is darker than mine.
So, the problem must be here or the codes before the caculation of "hist". But I still can't find out where the exact problem is.
Maybe you can modify your code and do more tests.
I upload a result from my code here, you can compare your result with mine.
If you find the problem, please tell me! I hope my answer will help you.