标签:src plt img image cv2 opencv 应用 简单 save
转换成HSV色彩空间
图像变换
图像过滤
提取直线、轮廓、区域
图像增强-白平衡等
import matplotlib.pyplot as plt
def plt_save(image, title=''):
plt.imshow(X=image, cmap='gray')
plt.title(title)
file_path = './output/' + title + '.png'
plt.savefig(file_path)
print(file_path)
pass
def plt_show(image, title=''):
plt.imshow(X=image, cmap='gray')
plt.title(title)
plt.show()
#main.py
import cv2
from image_filtering import show_filtering
from image_outline import show_outline
from image_transformation import show_transformation
from utils import plt_save
from image_color import show_hsv
from image_enhancement import show_enhancement
if __name__ == '__main__':
file_path = './images/000000507081.jpg'
origin = cv2.imread(file_path)
origin = origin[:, :, [2, 1, 0]]
# x, y = origin.shape[0:2]
# origin = cv2.resize(origin, (int(y / 3), int(x / 3)))
plt_save(image=origin, title='Origin')
# --------------------图像色彩--------------------
# 转换成HSV色彩空间
show_hsv(origin)
# --------------------图像变换--------------------
show_transformation(origin)
# --------------------图像过滤--------------------
show_filtering(origin)
# --------------------提取直线、轮廓、区域--------------------
show_outline(origin)
# -------------------- 图像增强-白平衡等--------------------
show_enhancement(origin)
print('done')
#image_transformation.py
import cv2
import numpy as np
from utils import plt_save
def show_transformation(src):
# 用形态学滤波器腐蚀和膨胀图像
element_3x3 = np.ones((3, 3), np.uint8)
# 腐蚀 3x3
eroded = cv2.erode(src=src, kernel=element_3x3)
plt_save(image=eroded, title='eroded')
# 膨胀 3x3 3次
dilated = cv2.dilate(src=src, kernel=element_3x3, iterations=3)
plt_save(image=dilated, title='dilated 3 times')
# 腐蚀 7x7
element_7x7 = np.ones((7, 7), np.uint8)
eroded_7x7 = cv2.erode(src=src, kernel=element_7x7, iterations=1)
plt_save(image=eroded_7x7, title='eroded 7x7')
# 腐蚀 3x3 3次
eroded_3 = cv2.erode(src=src, kernel=element_3x3, iterations=3)
plt_save(image=eroded_3, title='eroded 3 times')
# 用形态学滤波器开启和闭合图像
image_gray = cv2.cvtColor(src=src, code=cv2.COLOR_RGB2GRAY)
# plt_save(image=image_gray, title='image_gray')
# Close the image
element_5x5 = np.ones((5, 5), np.uint8)
closed = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_CLOSE, kernel=element_5x5)
plt_save(image=closed, title='closed')
# Open the image
opened = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_OPEN, kernel=element_5x5)
print(opened.shape)
plt_save(image=opened, title='opened')
closed = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_CLOSE, kernel=element_5x5)
closed_opened = cv2.morphologyEx(src=closed, op=cv2.MORPH_OPEN, kernel=element_5x5)
print(closed_opened.shape)
plt_save(image=closed_opened, title='Closed-Opened')
opened = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_OPEN, kernel=element_5x5)
opened_closed = cv2.morphologyEx(src=opened, op=cv2.MORPH_CLOSE, kernel=element_5x5)
plt_save(image=opened_closed, title='Opened-Closed')
# 在灰度图像中应用形态学运算
edge = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_GRADIENT, kernel=element_3x3)
plt_save(image=255 - edge, title='Gradient-Edge')
# Apply threshold to obtain a binary image
threshold = 80
_, thresh_binary = cv2.threshold(src=edge, thresh=threshold, maxval=255, type=cv2.THRESH_BINARY)
plt_save(image=thresh_binary, title='Gradient-Edge-Thresh-Binary-Edge')
# 7x7 Black Top-hat Image
black_hat = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_BLACKHAT, kernel=element_7x7)
plt_save(image=255 - black_hat, title='7x7-Black-Top-hat')
# Apply threshold to obtain a binary image
threshold = 25
_, thresh_binary = cv2.threshold(src=black_hat, thresh=threshold, maxval=255, type=cv2.THRESH_BINARY)
plt_save(image=255 - thresh_binary, title='7x7-Black Top-hat-Thresh-Binary-Edge')
# Apply the black top-hat transform using a 7x7 structuring element
closed = cv2.morphologyEx(src=thresh_binary, op=cv2.MORPH_CLOSE, kernel=element_7x7)
plt_save(image=255 - closed, title='7x7-Black-Top-hat-Closed')
pass
#image_filtering.py
import cv2
import numpy as np
from utils import plt_save
def show_filtering(src):
# 低通滤波
# Blur the image with a mean filter
blur = cv2.blur(src=src, ksize=(5, 5))
plt_save(image=blur, title='Mean filtered (5x5)')
# Blur the image with a mean filter 9x9
blur = cv2.blur(src=src, ksize=(9, 9))
plt_save(image=blur, title='Mean filtered (9x9)')
blur = cv2.GaussianBlur(src=src, ksize=(9, 9), sigmaX=1.5)
plt_save(image=blur, title='Gaussian filtered Image (9x9)')
gauss = cv2.getGaussianKernel(ksize=9, sigma=1.5, ktype=cv2.CV_32F)
print('GaussianKernel 1.5 = [', end='')
for item in gauss:
print(item, end='')
pass
print(']')
gauss = cv2.getGaussianKernel(ksize=9, sigma=-1, ktype=cv2.CV_32F)
print('GaussianKernel -1 = [', end='')
for item in gauss:
print(item, end='')
pass
print(']')
# 缩减 采样
blur = cv2.GaussianBlur(src=src, ksize=(11, 11), sigmaX=1.75)
resized1 = cv2.resize(src=blur, dsize=(0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_CUBIC)
plt_save(image=resized1, title='resize CUBIC 0.25')
# resizing with NN
resized2 = cv2.resize(src=resized1, dsize=(0, 0), fx=4, fy=4, interpolation=cv2.INTER_NEAREST)
plt_save(image=resized2, title='resize NEAREST x4')
# resizing with bilinear
resized3 = cv2.resize(src=resized1, dsize=(0, 0), fx=4, fy=4, interpolation=cv2.INTER_LINEAR)
plt_save(image=resized3, title='resize LINEAR x4')
# 中值滤波
median_blur = cv2.medianBlur(src=src, ksize=5)
plt_save(image=median_blur, title='Median filtered')
# 定向滤波器
image_gray = cv2.cvtColor(src=src, code=cv2.COLOR_BGR2GRAY)
# Compute Sobel X derivative
sobel_x = cv2.Sobel(src=image_gray, ddepth=cv2.CV_8U, dx=1, dy=0, ksize=3, scale=0.4, delta=128,
borderType=cv2.BORDER_DEFAULT)
plt_save(image=sobel_x, title='Sobel X')
# Compute Sobel Y derivative
sobel_y = cv2.Sobel(src=image_gray, ddepth=cv2.CV_8U, dx=0, dy=1, ksize=3, scale=0.4, delta=128,
borderType=cv2.BORDER_DEFAULT)
plt_save(image=sobel_y, title='Sobel Y')
# Compute norm of Sobel
sobel_x = cv2.Sobel(src=image_gray, ddepth=cv2.CV_16S, dx=1, dy=0)
sobel_y = cv2.Sobel(src=image_gray, ddepth=cv2.CV_16S, dx=0, dy=1)
sobel_1 = abs(sobel_x) + abs(sobel_y)
plt_save(image=sobel_1, title='abs Sobel X+Y')
sobel_2 = cv2.convertScaleAbs(sobel_x) + cv2.convertScaleAbs(sobel_y)
plt_save(image=sobel_2, title='cv2.convertScaleAbs Sobel X+Y')
# Compute Sobel X derivative (7x7)
sobel_x_7x7 = cv2.Sobel(src=image_gray, ddepth=cv2.CV_8U, dx=1, dy=0, ksize=7, scale=0.001, delta=128)
plt_save(image=sobel_x_7x7, title='Sobel X (7x7)')
uint8_sobel_1 = np.uint8(sobel_1)
plt_save(image=uint8_sobel_1, title='uint8 sobel_1')
uint8_sobel_2 = np.uint8(sobel_2)
plt_save(image=uint8_sobel_2, title='uint8 sobel_2')
int8_sobel_1 = np.int8(sobel_1)
plt_save(image=int8_sobel_1, title='int8 sobel_1')
int8_sobel_2 = np.int8(sobel_2)
plt_save(image=int8_sobel_2, title='int8 sobel_2')
# Apply threshold to Sobel norm (low threshold value)
_, thresh_binary = cv2.threshold(src=uint8_sobel_1, thresh=255, maxval=255, type=cv2.THRESH_BINARY)
plt_save(image=thresh_binary, title='Binary Sobel (low) cv2.threshold uint8_sobel_1')
_, thresh_binary = cv2.threshold(src=uint8_sobel_2, thresh=255, maxval=255, type=cv2.THRESH_BINARY)
plt_save(image=thresh_binary, title='Binary Sobel (low) cv2.threshold uint8_sobel_2')
# Apply threshold to Sobel norm (high threshold value)
_, thresh_binary = cv2.threshold(src=uint8_sobel_1, thresh=190, maxval=255, type=cv2.THRESH_BINARY)
plt_save(image=thresh_binary, title='Binary Sobel Image (high) cv2.threshold uint8_sobel_1')
_, thresh_binary = cv2.threshold(src=uint8_sobel_2, thresh=190, maxval=255, type=cv2.THRESH_BINARY)
plt_save(image=thresh_binary, title='Binary Sobel Image (high) cv2.threshold uint8_sobel_2')
add_weighted_sobel = cv2.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0)
plt_save(image=add_weighted_sobel, title='cv2.addWeighted abs')
# down-sample and up-sample the image
reduced = cv2.pyrDown(src=src)
rescaled = cv2.pyrUp(src=reduced)
plt_save(image=rescaled, title='Rescaled')
# down-sample and up-sample the image
reduced = cv2.pyrDown(src=image_gray)
rescaled = cv2.pyrUp(src=reduced)
w, h = src.shape[0:2]
rescaled = cv2.resize(rescaled, (h, w))
plt_save(image=rescaled, title='Rescaled')
subtract = cv2.subtract(src1=rescaled, src2=image_gray)
subtract = np.uint8(subtract)
plt_save(image=subtract, title='cv2.subtract')
gauss05 = cv2.GaussianBlur(src=image_gray, ksize=(0, 0), sigmaX=0.5)
gauss15 = cv2.GaussianBlur(src=image_gray, ksize=(0, 0), sigmaX=1.5)
subtract = cv2.subtract(src1=gauss15, src2=gauss05, dtype=cv2.CV_16S)
subtract = np.uint8(subtract)
plt_save(image=subtract, title='cv2.subtract gauss15 - gauss05')
gauss20 = cv2.GaussianBlur(src=image_gray, ksize=(0, 0), sigmaX=2.0)
gauss22 = cv2.GaussianBlur(src=image_gray, ksize=(0, 0), sigmaX=2.2)
subtract = cv2.subtract(src1=gauss22, src2=gauss20, dtype=cv2.CV_32F)
subtract = np.uint8(subtract)
plt_save(image=subtract, title='cv2.subtract gauss22 - gauss20')
#image_outline.py
import cv2
import numpy as np
from utils import plt_save
def show_outline(src):
contours = cv2.Canny(image=src, threshold1=125, threshold2=350)
plt_save(255 - contours, title='Canny Contours')
image_gray = cv2.cvtColor(src=src, code=cv2.COLOR_BGR2GRAY)
contours = cv2.Canny(image=image_gray, threshold1=125, threshold2=350)
plt_save(255 - contours, title='Canny Contours Gray')
# Hough tranform for line detection
theta = np.pi / 180
threshold = 50
lines = cv2.HoughLinesP(image=contours, rho=1, theta=theta, threshold=threshold)
if lines is not None:
src_clone = src.copy()
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(img=src_clone, pt1=(x1, y1), pt2=(x2, y2), color=(0, 255, 0), thickness=2)
pass
pass
plt_save(src_clone, title='Lines with HoughP, threshold: ' + str(threshold))
pass
# Detect circles
# blur = cv2.GaussianBlur(src=image_gray, ksize=(5, 5), sigmaX=1.5)
threshold = 200
min_votes = 100
circles = cv2.HoughCircles(image=image_gray, method=cv2.HOUGH_GRADIENT, dp=2, minDist=20,
param1=threshold, param2=min_votes, minRadius=15, maxRadius=50)
if circles is not None:
src_clone = src.copy()
for circle in circles:
for x1, y1, r in circle:
cv2.circle(img=src_clone, center=(x1, y1), radius=int(r), color=(0, 255, 0), thickness=2)
pass
plt_save(src_clone, title='Circles with HoughP, threshold: ' + str(threshold) + ', min_votes=' + str(min_votes))
pass
# Get the contours
src_clone = src.copy()
contours, _ = cv2.findContours(image=image_gray, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_NONE)
image_contours = cv2.drawContours(image=src_clone, contours=contours, contourIdx=-1, color=(255, 255, 255),
thickness=2)
plt_save(image_contours, title='Contours with RETR_LIST')
#image_enhancemet.py
import random
import sys
import cv2
import numpy as np
from utils import plt_save
def show_enhancement(src):
balance_img1 = white_balance(src)
plt_save(balance_img1, 'White Balance 1')
# 灰度世界算法
balance_img2 = grey_world(src)
plt_save(balance_img2, 'White Balance 2')
# 直方图均衡化
balance_img3 = his_equl_color(src)
plt_save(balance_img3, 'White Balance 3')
# 视网膜-大脑皮层(Retinex)增强算法
# 单尺度Retinex
ssr1 = single_scale_retinex(src, 300)
ssr1[ssr1 < 0] = 0
plt_save(ssr1, 'Single Scale Retinex 1')
ssr2 = s_s_r(src)
plt_save(ssr2, 'Single Scale Retinex 2')
# 多尺度Retinex
msr1 = multi_scale_retinex(src, [15, 80, 250])
msr1[msr1 < 0] = 0
plt_save(msr1, 'Multi Scale Retinex 1')
msr2 = m_s_r(src, sigma_list=[15, 80, 250])
plt_save(msr2, 'Multi Scale Retinex 2')
msrcr1 = m_s_r_c_r(src, sigma_list=[15, 80, 250])
plt_save(msrcr1, 'Multi Scale Retinex With Color Restoration 1')
# 自动白平衡 AWB
awb = automatic_white_balance(src)
plt_save(awb, 'Automatic White Balance')
# 自动色彩均衡 ACE
balance_img4 = automatic_color_equalization(src)
plt_save(balance_img4, 'Automatic Color Equalization')
def white_balance(src):
r, g, b = cv2.split(src)
r_avg = cv2.mean(r)[0]
g_avg = cv2.mean(g)[0]
b_avg = cv2.mean(b)[0]
# 求各个通道所占增益
k = (r_avg + g_avg + b_avg) / 3
kr = k / r_avg
kg = k / g_avg
kb = k / b_avg
r = cv2.addWeighted(src1=r, alpha=kr, src2=0, beta=0, gamma=0)
g = cv2.addWeighted(src1=g, alpha=kg, src2=0, beta=0, gamma=0)
b = cv2.addWeighted(src1=b, alpha=kb, src2=0, beta=0, gamma=0)
return cv2.merge([b, g, r])
# 灰度世界算法
def grey_world(nimg):
nimg = nimg.transpose(2, 0, 1).astype(np.uint32)
avg_b = np.average(nimg[0])
avg_g = np.average(nimg[1])
avg_r = np.average(nimg[2])
avg = (avg_b + avg_g + avg_r) / 3
nimg[0] = np.minimum(nimg[0] * (avg / avg_b), 255)
nimg[1] = np.minimum(nimg[1] * (avg / avg_g), 255)
nimg[2] = np.minimum(nimg[2] * (avg / avg_r), 255)
return nimg.transpose(1, 2, 0).astype(np.uint8)
# 直方图均衡化
def his_equl_color(img):
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
channels = cv2.split(ycrcb)
cv2.equalizeHist(channels[0], channels[0]) # equalizeHist(in,out)
cv2.merge(channels, ycrcb)
img_eq = cv2.cvtColor(ycrcb, cv2.COLOR_YCR_CB2BGR)
return img_eq
# 视网膜-大脑皮层(Retinex)增强算法
def single_scale_retinex(img, sigma):
temp = cv2.GaussianBlur(img, (0, 0), sigma)
gaussian = np.where(temp == 0, 0.01, temp)
retinex = np.log10(img + 0.01) - np.log10(gaussian)
return retinex
def multi_scale_retinex(img, sigma_list):
retinex = np.zeros_like(img * 1.0)
for sigma in sigma_list:
retinex = single_scale_retinex(img, sigma)
retinex = retinex / len(sigma_list)
return retinex
def color_restoration(img, alpha, beta):
img_sum = np.sum(img, axis=2, keepdims=True)
color_restoration_temp = beta * (np.log10(alpha * img) - np.log10(img_sum))
return color_restoration_temp
def multi_scale_retinex_with_color_restoration(img, sigma_list, g, b, alpha, beta):
img = np.float64(img) + 1.0
img_retinex = multi_scale_retinex(img, sigma_list)
img_color = color_restoration(img, alpha, beta)
img_msrcr = g * (img_retinex * img_color + b)
return img_msrcr
def simplest_color_balance(img, low_clip, high_clip):
total = img.shape[0] * img.shape[1]
low_val = 0.0
high_val = 0.0
for i in range(img.shape[2]):
unique, counts = np.unique(img[:, :, i], return_counts=True)
current = 0
for u, c in zip(unique, counts):
if float(current) / total < low_clip:
low_val = u
if float(current) / total < high_clip:
high_val = u
current += c
img[:, :, i] = np.maximum(np.minimum(img[:, :, i], high_val), low_val)
return img
def touint8(img):
for i in range(img.shape[2]):
img[:, :, i] = (img[:, :, i] - np.min(img[:, :, i])) / \
(np.max(img[:, :, i]) - np.min(img[:, :, i])) * 255
img = np.uint8(np.minimum(np.maximum(img, 0), 255))
return img
def s_s_r(img, sigma=300):
ssr = single_scale_retinex(img, sigma)
ssr = touint8(ssr)
return ssr
def m_s_r(img, sigma_list):
if sigma_list is None:
sigma_list = [15, 80, 250]
msr = multi_scale_retinex(img, sigma_list)
msr = touint8(msr)
return msr
def m_s_r_c_r(img, sigma_list, g=5, b=25, alpha=125, beta=46, low_clip=0.01, high_clip=0.99):
if sigma_list is None:
sigma_list = [15, 80, 250]
msrcr = multi_scale_retinex_with_color_restoration(img, sigma_list, g, b, alpha, beta)
msrcr = touint8(msrcr)
msrcr = simplest_color_balance(msrcr, low_clip, high_clip)
return msrcr
# 自动白平衡(AWB)
def automatic_white_balance(img):
result = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
avg_a = np.average(result[:, :, 1])
avg_b = np.average(result[:, :, 2])
for x in range(result.shape[0]):
for y in range(result.shape[1]):
l, a, b = result[x, y, :]
# fix for CV correction
l *= 100 / 255.0
result[x, y, 1] = a - ((avg_a - 128) * (l / 100.0) * 1.1)
result[x, y, 2] = b - ((avg_b - 128) * (l / 100.0) * 1.1)
result = cv2.cvtColor(result, cv2.COLOR_LAB2BGR)
return result
# 自动色彩均衡(ACE)
# 饱和函数
def calc_saturation(diff, slope, limit):
ret = diff * slope
if ret > limit:
ret = limit
elif ret < (-limit):
ret = -limit
return ret
def automatic_color_equalization(nimg, slope=10, limit=1000, samples=500):
nimg = nimg.transpose(2, 0, 1)
# Convert input to an ndarray with column-major memory order(仅仅是地址连续,内容和结构不变)
nimg = np.ascontiguousarray(nimg, dtype=np.uint8)
width = nimg.shape[2]
height = nimg.shape[1]
cary = []
# 随机产生索引
for i in range(0, samples):
_x = random.randint(0, width) % width
_y = random.randint(0, height) % height
dict_temp = {"x": _x, "y": _y}
cary.append(dict_temp)
pass
mat = np.zeros((3, height, width), float)
r_max = sys.float_info.min
r_min = sys.float_info.max
g_max = sys.float_info.min
g_min = sys.float_info.max
b_max = sys.float_info.min
b_min = sys.float_info.max
for i in range(height):
for j in range(width):
r = nimg[0, i, j]
g = nimg[1, i, j]
b = nimg[2, i, j]
r_rscore_sum = 0.0
g_rscore_sum = 0.0
b_rscore_sum = 0.0
denominator = 0.0
for _dict in cary:
_x = _dict["x"] # width
_y = _dict["y"] # height
# 计算欧氏距离
dist = np.sqrt(np.square(_x - j) + np.square(_y - i))
if dist < height / 5:
continue
_sr = nimg[0, _y, _x]
_sg = nimg[1, _y, _x]
_sb = nimg[2, _y, _x]
r_rscore_sum += calc_saturation(int(r) - int(_sr), slope, limit) / dist
g_rscore_sum += calc_saturation(int(g) - int(_sg), slope, limit) / dist
b_rscore_sum += calc_saturation(int(b) - int(_sb), slope, limit) / dist
denominator += limit / dist
r_rscore_sum = r_rscore_sum / denominator
g_rscore_sum = g_rscore_sum / denominator
b_rscore_sum = b_rscore_sum / denominator
mat[0, i, j] = r_rscore_sum
mat[1, i, j] = g_rscore_sum
mat[2, i, j] = b_rscore_sum
if r_max < r_rscore_sum:
r_max = r_rscore_sum
if r_min > r_rscore_sum:
r_min = r_rscore_sum
if g_max < g_rscore_sum:
g_max = g_rscore_sum
if g_min > g_rscore_sum:
g_min = g_rscore_sum
if b_max < b_rscore_sum:
b_max = b_rscore_sum
if b_min > b_rscore_sum:
b_min = b_rscore_sum
for i in range(height):
for j in range(width):
nimg[0, i, j] = (mat[0, i, j] - r_min) * 255 / (r_max - r_min)
nimg[1, i, j] = (mat[1, i, j] - g_min) * 255 / (g_max - g_min)
nimg[2, i, j] = (mat[2, i, j] - b_min) * 255 / (b_max - b_min)
return nimg.transpose([1, 2, 0]).astype(np.uint8)
标签:src,plt,img,image,cv2,opencv,应用,简单,save 来源: https://blog.csdn.net/weixin_44576543/article/details/120684998
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。