python——图像处理3(均值偏移、改变亮度、图像修复、图像融合)

1.均值偏移(磨皮效果)
import cv2 as cv
import numpy as np


def bi_demo(image):#边缘保留滤波
    dst=cv.bilateralFilter(image,0,40,15)
    cv.imshow("bi_demo",dst)


def shift_demo(image):#均值迁移
    dst=cv.pyrMeanShiftFiltering(image,10,50)
    cv.imshow("bi_demo",dst)
    cv.imwrite('img/1/gg.jpg',dst)


src1 = cv.imread('img/1/a3.jpg', cv.IMREAD_COLOR)  # 读入彩色图片
print(src1.shape)
cv.namedWindow("input image",1)
cv.imshow('image1', src1)
shift_demo(src1)
k = cv.waitKey(0)

python——图像处理3(均值偏移、改变亮度、图像修复、图像融合)_第1张图片

2.基于hsv改变整体亮度
# encoding:utf-8
import cv2

def hsv(image, hue_shift_limit=(-2, 8), sat_shift_limit=(-5, 50),val_shift_limit=(0, 1.5)):
    img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    v = img[:, :, 2]
    print(v)
    v=v+10
    img[:, :, 2] = v
    image = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
    print(v)

    cv2.imwrite("img/3/0.jpg", image)

path1='img/3/gg1.jpg'
img1 = cv2.imread(path1, cv2.IMREAD_UNCHANGED)
hsv(img1)

3.基于灰度调节亮度(两张对比)
import cv2 as cv
import numpy as np
import cv2

def get_avg(list5):
    if len(list5)==0:
        avg=0;
    else:
        avg = sum(list5)/ len(list5)
    return avg

def light(img):
    gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
    x = gray.shape[0]
    y = gray.shape[1]
    list1 = []
    list2 = []
    for i in range(x):
        for j in range(y):
            if img[i,j][0]>20:
                if j>250 and j < 256:
                    list1.append(gray[i, j])
                elif j>256 and j<262:
                    list2.append(gray[i, j])

    avg1 = get_avg(list1)
    avg2 = get_avg(list2)
    return avg1,avg2

def change(img,dif):
    x = img.shape[0]
    y = img.shape[1]
    for i in range(x):
        for j in range(y):
            if img[i,j][0]>20:
                if j>=255:
                    img[i, j][0] += dif
                    img[i, j][1] += dif
                    img[i, j][2] += dif
    cv2.imwrite('img/2/a5.jpg',img)

img= cv.imread('img/2/a3.jpg')
avg1=light(img)[0]
avg2=light(img)[1]
dif=avg1-avg2
change(img,dif)
print(dif)



4.图像修复—inpaint
import cv2

img = cv2.imread('img/3/a1.jpg')
mask = cv2.imread('img/3/a2.jpg', 0)

cv2.imshow('img', img)
cv2.imshow('mask', mask)

dst = cv2.inpaint(img, mask, 20, cv2.INPAINT_TELEA)
cv2.imshow('1', dst)

dst2 = cv2.inpaint(img, mask, 20, cv2.INPAINT_NS)
cv2.imshow('2', dst2)

cv2.waitKey(0)
cv2.destroyAllWindows()

5.1 图像融合(整体)
#encoding:utf-8
import cv2  
import numpy as np  
import matplotlib.pyplot as plt
 
src1 = cv2.imread('img/a3.jpg')
src2 = cv2.imread('img/a5.jpg')
 
#图像融合
result = cv2.addWeighted(src1, 0.8, src2, 0.2, 0)
 
cv2.imshow("src1", src1)
cv2.imshow("src2", src2)
cv2.imshow("result", result)
# cv2.imwrite('img/a6.jpg',result)
cv2.waitKey(0)
cv2.destroyAllWindows()

5.2 图像融合(只融合v通道)
from PIL import *
import numpy as np;
import cv2;             #导入opencv模块
 
img1=cv2.imread("img/a3.jpg");#读取要处理的图片
img1_hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)
img1_h = img1_hsv[..., 0]
img1_s = img1_hsv[..., 1]
img1_v = img1_hsv[..., 2]

img2=cv2.imread("img/a5.jpg");#读取要处理的图片
img2_hsv = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)
img2_h = img2_hsv[..., 0]
img2_s = img2_hsv[..., 1]
img2_v = img2_hsv[..., 2]

result = cv2.addWeighted(img1_v, 0.9, img2_v, 0.1, 0)
cv2.imshow("src1", img1_v)
cv2.imshow("src2", img2_v)
cv2.imshow("result", result)
# cv2.imwrite('img/a6.jpg',result)
img3 = cv2.merge([img2_h, img2_s, result])
img4 = cv2.cvtColor(img3, cv2.COLOR_HSV2BGR)
cv2.imshow("examplesR1", img2)
cv2.imshow("examplesR2", img4)
cv2.waitKey(0)
cv2.destroyAllWindows()

你可能感兴趣的:(图像处理)