我正在制作图像滤镜,但要使其正常工作,我什至需要消除图像中的闪电。 Original Image | Output Image Due to a lighting issue. | Output when lightning is same all over the image.
我已经试过这段代码:
import cv2
import numpy as np
img = cv2.imread('new_sample/sample6.jpg', 1)
original = img.copy()
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8, 8))
cl = clahe.apply(l)
limg = cv2.merge((cl, a, b))
final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
kernel_sharpen_3 = np.array([[-1, -1, -1, -1, -1]
[-1, 2, 2, 2, -1],
[-1, 2, 8, 2, -1],
[-1, 2, 2, 2, -1],
[-1, -1, -1, -1, -1]]) / 8
img = cv2.filter2D(final, -1, kernel_sharpen_3)
ret, thresh1 = cv2.threshold(img, 110, 255, cv2.THRESH_BINARY)
threshold = cv2.fastNlMeansDenoising(thresh1, 11, 31, 9)
blur = cv2.GaussianBlur(threshold, (5, 5), 0)
stack_horizontal = np.concatenate((original, final), axis=1)
stack_horizontal2 = np.concatenate((threshold, blur), axis=1)
stack_vertical = np.concatenate((stack_horizontal, stack_horizontal2),
axis=0)
cv2.imshow('Images', cv2.resize(stack_vertical, (700, 1000)))
cv2.imwrite("magic.jpg", blur)
cv2.waitKey()
cv2.destroyAllWindows()
最佳答案
这是Python / OpenCV中的一个想法。进行自适应阈值处理,然后用原稿的平均颜色为白色着色。
输入:
import cv2
import numpy as np
# read image
img = cv2.imread("purple_text.jpg")
h, w, c = img.shape
# get average color of img
color = cv2.mean(img)[0:3]
# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# do adaptive threshold on gray image
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 13)
thresh3 = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
# change white to color
result1 = thresh3.copy()
result1[thresh==255] = color
# optionally colorize text darker and more blue
result2 = result1.copy()
result2[thresh==0] = (color[0],0.65*color[1],0.65*color[2])
# write results to disk
cv2.imwrite("purple_text_processed1.jpg", result1)
cv2.imwrite("purple_text_processed2.jpg", result2)
# display it
cv2.imshow("IMAGE", img)
cv2.imshow("THRESHOLD", thresh)
cv2.imshow("RESULT1", result1)
cv2.imshow("RESULT2", result2)
cv2.waitKey(0)
背景色:
背景和文本颜色:
添加:
这是一种更好的变体,它可以获取背景以及文本的平均颜色,并根据自适应阈值对其中一个或两个进行着色。
import cv2
import numpy as np
# read image
img = cv2.imread("purple_text.jpg")
h, w, c = img.shape
# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# do adaptive threshold on gray image to make a mask
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 13)
# get color of image where thresh is 255
bgcolor = cv2.mean(img, thresh)[0:3]
# get color where thresh is 0
textcolor = cv2.mean(img, 255-thresh)[0:3]
# put colorize background and text with bgcolor and color, resp.
result = img.copy()
result[thresh==255] = bgcolor
result[thresh==0] = textcolor
# write results to disk
cv2.imwrite("purple_text_processed3.jpg", result)
# optionally darken text COLOR_BGR2GRAY
resultx = img.copy()
resultx[thresh==255] = bgcolor
resultx[thresh==0] = (0.75*textcolor[0], 0.75*textcolor[1], 0.75*textcolor[2])
# write results to disk
cv2.imwrite("purple_text_processed3x.jpg", resultx)
# display it
#cv2.imshow("IMAGE", img)
cv2.imshow("THRESHOLD", thresh)
cv2.imshow("RESULT", result)
cv2.imshow("RESULTX", resultx)
cv2.waitKey(0)
彩色文本和背景:
彩色和深色文本和背景:
关于python - 如何使彩色图像中的照明均匀?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58624628/