python中是否有相當於imadjust的地方? equalizeHist不會給出類似的結果。Python中imadjust的等價物是什麼?
2
A
回答
1
import numpy as np
import bisect
def imadjust(src, tol=1, vin=[0,255], vout=(0,255)):
# src : input one-layer image (numpy array)
# tol : tolerance, from 0 to 100.
# vin : src image bounds
# vout : dst image bounds
# return : output img
dst = src.copy()
tol = max(0, min(100, tol))
if tol > 0:
# Compute in and out limits
# Histogram
hist = np.zeros(256, dtype=np.int)
for r in range(src.shape[0]):
for c in range(src.shape[1]):
hist[src[r,c]] += 1
# Cumulative histogram
cum = hist.copy()
for i in range(1, len(hist)):
cum[i] = cum[i - 1] + hist[i]
# Compute bounds
total = src.shape[0] * src.shape[1]
low_bound = total * tol/100
upp_bound = total * (100 - tol)/100
vin[0] = bisect.bisect_left(cum, low_bound)
vin[1] = bisect.bisect_left(cum, upp_bound)
# Stretching
scale = (vout[1] - vout[0])/(vin[1] - vin[0])
for r in range(dst.shape[0]):
for c in range(dst.shape[1]):
vs = max(src[r,c] - vin[0], 0)
vd = min(int(vs * scale + 0.5) + vout[0], vout[1])
dst[r,c] = vd
return dst
兩者,如果你不希望設置VIN及VOU。只需使用cv2.equalizeHist。
@jit
def hisEqul(img):
return cv2.equalizeHist(img)
@jit
def hisEqulColor(img):
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
channels = cv2.split(ycrcb)
cv2.equalizeHist(channels[0], channels[0])
cv2.merge(channels, ycrcb)
cv2.cvtColor(ycrcb, cv2.COLOR_YCR_CB2BGR, img)
return img
1
您可以找到imadjust這裏的C++版本: Is there any function equivalent to Matlab's imadjust in OpenCV with C++?
而且從@maslovw這個版本蟒蛇code是非常good.I只是優化了幾個圈也使它運行快一點。
import numpy as np
import bisect
from numba import jit
@jit
def imadjust(src, tol=1, vin=[0,255], vout=(0,255)):
# src : input one-layer image (numpy array)
# tol : tolerance, from 0 to 100.
# vin : src image bounds
# vout : dst image bounds
# return : output img
assert len(src.shape) == 2 ,'Input image should be 2-dims'
tol = max(0, min(100, tol))
if tol > 0:
# Compute in and out limits
# Histogram
hist = np.histogram(src,bins=list(range(256)),range=(0,255))[0]
# Cumulative histogram
cum = hist.copy()
for i in range(1, 256): cum[i] = cum[i - 1] + hist[i]
# Compute bounds
total = src.shape[0] * src.shape[1]
low_bound = total * tol/100
upp_bound = total * (100 - tol)/100
vin[0] = bisect.bisect_left(cum, low_bound)
vin[1] = bisect.bisect_left(cum, upp_bound)
# Stretching
scale = (vout[1] - vout[0])/(vin[1] - vin[0])
vs = src-vin[0]
vs[src<vin[0]]=0
vd = vs*scale+0.5 + vout[0]
vd[vd>vout[1]] = vout[1]
dst = vd
return dst
相關問題
- 1. python中'gem'的等價物是什麼?
- 2. python中print_r()的等價物是什麼?
- 3. 什麼是Python的os.walk的等價物?
- 4. 什麼是java.util.zip.Inflater的Python等價物?
- 5. 什麼是Python os.pathsep的等價物?
- 6. 什麼是Tomcat的Python等價物?
- 7. Python`itertools.chain`的Ruby等價物是什麼?
- 8. 什麼是Perlbrew的Python等價物?
- 9. 什麼是android中的dataWithContentsOfURL等價物?
- 10. RDFlib中rdf:ID的等價物是什麼?
- 11. Monotouch中的CGPDFDocumentGetCatalog等價物是什麼?
- 12. 什麼是C++中的instanceof等價物?
- 13. 什麼是VC7中的strtok_s等價物?
- 14. jquery中Ajax.updater的等價物是什麼?
- 15. 什麼是JSP中的sendmail等價物?
- 16. jQuery中Class.create()的等價物是什麼?
- 17. 什麼是MSTest中MbUnit.Framework.RowAttribute的等價物?
- 18. JQuery中innerHTML的等價物是什麼?
- 19. WinRT中SecureString的等價物是什麼?
- 20. java中fopen_s()的等價物是什麼?
- 21. 什麼是perl中$ _的php等價物?
- 22. Bindingsource中EOF的等價物是什麼?
- 23. 什麼是Java中的「ByRef」等價物?
- 24. java中cin.ignore()的等價物是什麼?
- 25. Swift中@autoreleasepool的等價物是什麼?
- 26. 什麼是C#中的vbNullChar等價物?
- 27. CoreFoundation中NSHomeDirectory()的等價物是什麼?
- 28. 什麼是Silverlight中的OnRender等價物?
- 29. 什麼是GraphicsMagick中的setCompression()等價物?
- 30. C#中bigint的等價物是什麼?
您可以輕鬆地從[this](http://stackoverflow.com/a/31650693/1586200)C++代碼實現Python代碼。 –