Esempio n. 1
0
def laplace_pyramid_demo(image):  #必须宽高相等
    pyramid_image = gauss_pyramid_demo(image)
    level = len(pyramid_image)
    for i in range(level - 1, -1, -1):
        if (i - 1) < 0:
            expand = cv.pyrUp(pyramid_image[i], dstsize=image.shape[:2])
            lpls = cv.subtract(image, expand)
            cv.imshow("laplace" + str(i), lpls)
        else:
            expand = cv.pyrUp(pyramid_image[i],
                              dstsize=pyramid_image[i - 1].shape[:2])
            lpls = cv.subtract(pyramid_image[i - 1], expand)
            cv.imshow("laplace" + str(i), lpls)
Esempio n. 2
0
def lapalian_demo(image):
    image = cv.resize(image, (512, 512))  # 金字塔的源图像宽高必须是2^N
    print(image.shape)
    pyramid_images = pyramid_demo(image)  #做拉普拉斯金字塔必须用到高斯金字塔的结果
    level = len(pyramid_images)
    for i in range(level - 1, -1, -1):
        if (i - 1) < 0:
            expand = cv.pyrUp(pyramid_images[i], dstsize=image.shape[:2])
            lpls = cv.subtract(image, expand)
            cv.imshow("lapalian_down_" + str(i), lpls)
        else:
            expand = cv.pyrUp(pyramid_images[i],
                              dstsize=pyramid_images[i - 1].shape[:2])
            lpls = cv.subtract(pyramid_images[i - 1], expand)
            cv.imshow("lapalian_down_" + str(i), lpls)
Esempio n. 3
0
    def cartoon(self, image):
        '''
        Cartoonise Image!

        Datatypes: image:nparray format BGR
        
        '''
        numdown, numbilateral = 2, 7
        color = image
        for _ in range(numdown):
            color = cv2.pyrDown(color)
        for _ in range(numbilateral):
            color = cv2.bilateralFilter(color, d=9, sigmaColor=9, sigmaSpace=7)
        for _ in range(numdown):
            color = cv2.pyrUp(color)
        cartoon = cv2.bitwise_and(
            color,
            cv2.cvtColor(
                cv2.adaptiveThreshold(cv2.medianBlur(
                    cv2.cvtColor(image, cv2.COLOR_RGB2GRAY), 7),
                                      255,
                                      cv2.ADAPTIVE_THRESH_MEAN_C,
                                      cv2.THRESH_BINARY,
                                      blockSize=9,
                                      C=2), cv2.COLOR_GRAY2RGB))
        cartoon = cv2.cvtColor(cartoon, cv2.COLOR_BGR2RGB)
        return cartoon
Esempio n. 4
0
def toCarttonStyle(picturePath):
 # 设置输入输出路径和文件名称
    imgInput_FileName = picturePath
    imgOutput_FileName = 'res.jpg'
 
 # 属性设置
    num_down = 2 # 缩减像素采样的数目
    num_bilateral = 7 # 定义双边滤波的数目
 
 # 读取图片
    img_rgb = cv2.imread(imgInput_FileName)
 
 # 用高斯金字塔降低取样
    img_color = img_rgb
    for _ in range(num_down):
        img_color = cv2.pyrDown(img_color)
 
 # 重复使用小的双边滤波代替一个大的滤波
    for _ in range(num_bilateral):
        img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7)
 
 # 升采样图片到原始大小
    for _ in range(num_down):
        img_color = cv2.pyrUp(img_color)
 
 # 转换为灰度并且使其产生中等的模糊
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
    img_blur = cv2.medianBlur(img_gray, 7)
 
 # 检测到边缘并且增强其效果
    img_edge = cv2.adaptiveThreshold(img_blur, 255,
        cv2.ADAPTIVE_THRESH_MEAN_C,
        cv2.THRESH_BINARY,
        blockSize=9,
        C=2)
 
 # 算法处理后,照片的尺寸可能会不统一
 # 把照片的尺寸统一化
    height=img_rgb.shape[0]
    width = img_rgb.shape[1]
    img_color=cv2.resize(img_color,(width,height))
 
 # 转换回彩色图像
    img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
    img_cartoon = cv2.bitwise_and(img_color, img_edge)
 
 # 保存转换后的图片
    cv2.imwrite(imgOutput_FileName, img_cartoon)
    print('文件转换成漫画成功,保存在' + imgOutput_FileName)
Esempio n. 5
0
def cartoonizer(img, num_down=2, num_bi=5):
    #Params
    #num_down = 2 #DOWNSAMPLE STEPS
    #num_bi = 5 # BILATERAL FILTERING STEPS
    img_c = img
    for ix in range(num_down):
        img_c = cv2.pyrDown(img)  # Pyramid Down : Downsampling
    # print(img_c.shape)

    for iy in range(num_bi):
        img_c = cv2.bilateralFilter(img_c, d=9, sigmaColor=9,
                                    sigmaSpace=7)  #Filtering
    # print(img_c.shape)

    #UPSAMPLING
    for ix in range(num_down):
        img_c = cv2.pyrUp(img_c)  # Pyramid Down : Downsampling
    # print(img_c.shape)

    #BLUR and Threshold
    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)  # GRAY SCALE
    img_blur = cv2.medianBlur(img_gray, 7)  #MEDIAN BLUR
    img_edge = cv2.adaptiveThreshold(img_blur,
                                     255,
                                     cv2.ADAPTIVE_THRESH_MEAN_C,
                                     cv2.THRESH_BINARY,
                                     blockSize=9,
                                     C=2)

    img_c = cv2.resize(img_c, (800, 800))
    #RGB CONVERSION + BITWISE &
    img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
    # print(img_c.shape)
    # print(img_edge.shape)
    img_cartoon = cv2.bitwise_and(img_c, img_edge)

    stack = np.hstack([img, img_cartoon])
    return stack
Esempio n. 6
0
# generate gaussian pyramid for orange
orange_copy = orange.copy()
gp_orange = [orange_copy]

for i in range(6):
    orange_copy = cv2.pyrDown(orange_copy)
    gp_orange.append(orange_copy)
    #cv2.imshow(str(i), orange_copy)

# generate laplacian pyramid for apple
apple_copy = gp_apple[5]
lp_apple = [apple_copy]

for i in range(5, 0, -1):
    apple_extended = cv2.pyrUp(gp_apple[i])
    laplacian = cv2.subtract(gp_apple[i - 1], apple_extended)
    lp_apple.append(laplacian)
    #cv2.imshow(str(i), apple_copy)

# generate laplacian pyramid for orange
orange_copy = gp_orange[5]
lp_orange = [orange_copy]

for i in range(5, 0, -1):
    orange_extended = cv2.pyrUp(gp_orange[i])
    laplacian = cv2.subtract(gp_orange[i - 1], orange_extended)
    lp_orange.append(laplacian)
    #cv2.imshow(str(i), orange_copy)

# Now add left and right halves of images in each level
Esempio n. 7
0
    G = cv2.pyrDown(G)
    # cv2.imshow('gpa'+ str(i), G)
    gpA.append(G)

# generate Gaussian pyramid for B
G = B.copy()
gpB = [G]
for i in xrange(6):
    G = cv2.pyrDown(G)
    gpB.append(G)


# generate Laplacian Pyramid for A
lpA = [gpA[5]]
for i in xrange(5,0,-1):
    GE = cv2.pyrUp(gpA[i])
    GE_1 = cv2.resize(gpA[i-1], GE.shape[0:2])
    # cv2.imshow('ge'+ str(i), GE_1)
    L = cv2.subtract(GE_1,GE)
    # cv2.imshow('lpa'+ str(i), L)
    lpA.append(L)


# generate Laplacian Pyramid for B
lpB = [gpB[5]]
for i in xrange(5,0,-1):
    GE = cv2.pyrUp(gpB[i])
    GE_1 = cv2.resize(gpB[i-1], GE.shape[0:2])
    L = cv2.subtract(GE_1,GE)
    lpB.append(L)
Esempio n. 8
0
from cv2 import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

#pyrDown() 从一个高分辨率大尺寸的图像向上构建一个金子塔
# (尺寸变小,分辨率降低)。
img = cv.imread('star.jpg')
cv.imshow('img', img)
lower_reso = cv.pyrDown(img)
high_reso = cv.pyrUp(lower_reso)
cv.imshow('lower_case', lower_reso)
cv.imshow('high_reso', high_reso)
key = cv.waitKey(0)
if key == 27:
    cv.destroyAllWindows()
Esempio n. 9
0
from cv2 import cv2 as cv
import numpy as np

def cv_show(img, name):
    cv.imshow('tree', img)
    cv.waitKey(0)
    cv.destroyAllWindows()

img = cv.imread('tree.jpg')
up = cv.pyrDown(img)
down=cv.pyrUp(img)
cv_show(img,'down')
Esempio n. 10
0
import numpy as np
from cv2 import cv2

img = cv2.imread('lena.jpg')
layer = img.copy()

gp = [layer]

for i in range(6):
    layer = cv2.pyrDown(layer)
    gp.append(layer)
    #cv2.imshow(str(i), layer)

layer = gp[5]
cv2.imshow('upper level Gaussian Pyramid', layer)
lp = [layer]

for i in range(5, 0, -1):
    gaussian_extended = cv2.pyrUp(gp[i])
    laplacian = cv2.subtract(gp[i - 1], gaussian_extended)
    cv2.imshow(str(i), laplacian)

cv2.imshow('Original Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Esempio n. 11
0
#
# cv.imshow("original image", img)
# cv.imshow("first down", lr)
# cv.imshow("second down", lr1)
# cv.imshow("third down", lr2)
# cv.imshow("first up", hr)
# cv.imshow("second up", hr1)
# cv.imshow("third up", hr2)


# laplacian pyramids
layer = img.copy()
gaussian_pyr = [layer]

for i in range(6):
    layer = cv.pyrDown(layer)
    gaussian_pyr.append(layer)
    # cv.imshow("{}-down".format(i+1), layer)

layer = gaussian_pyr[5]
cv.imshow("Upper level Gaussian pyramid", layer)
lp = layer

for i in range(5, 0, -1):
    gaussian_expanded = cv.pyrUp(gaussian_pyr[i])
    laplacian_pyr = cv.subtract(gaussian_pyr[i-1], gaussian_expanded)
    cv.imshow("{}-pyramid".format(i), laplacian_pyr)


cv.waitKey(0)
cv.destroyAllWindows()
for i in range(6):
    apple_c = cv.pyrDown(apple_c)
    gp_apple.append(apple_c)

# gaussian pyramid orange
orange_c = orange.copy()
gp_orange = [orange_c]
for i in range(6):
    orange_c = cv.pyrDown(orange_c)
    gp_orange.append(orange_c)

# laplacian pyramid apple
apple_c = gp_apple[5]
lp_apple = [apple_c]
for i in range(5, 0, -1):
    gaussian_expanded = cv.pyrUp(gp_apple[i])
    laplacian = cv.subtract(gp_apple[i-1], gaussian_expanded)
    lp_apple.append(laplacian)

# laplacian pyramid orange
orange_c = gp_orange[5]
lp_orange = [orange_c]
for i in range(5, 0, -1):
    gaussian_expanded = cv.pyrUp(gp_orange[i])
    laplacian = cv.subtract(gp_orange[i-1], gaussian_expanded)
    lp_orange.append(laplacian)

apple_orange_pyramid = []
n = 0
for apple_lap, orange_lap in zip(lp_apple, lp_orange):
    cols, rows, sh = apple_lap.shape
Esempio n. 13
0
# coding: utf-8
import numpy as np
from cv2 import cv2
import matplotlib.pyplot as plt

img = cv2.imread(r'pictures\a.05x.jpg')
higher_reso = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
lower_reso = cv2.pyrDown(higher_reso)
higher_reso2 = cv2.pyrUp(lower_reso)

# Normally, we used to work with an image of constant size. But in some occassions, we need to work with images of different resolution of the same image. For example, while searching for something in an image, like face, we are not sure at what size the object will be present in the image. In that case, we will need to create a set of images with different resolution and search for object in all the images. These set of images with different resolution are called Image Pyramids (because when they are kept in a stack with biggest image at bottom and smallest image at top look like a pyramid).
# There are two kinds of Image Pyramids. 1) Gaussian Pyramid and 2) Laplacian Pyramids
# Higher level (Low resolution) in a Gaussian Pyramid is formed by removing consecutive rows and columns in Lower level (higher resolution) image. Then each pixel in higher level is formed by the contribution from 5 pixels in underlying level with gaussian weights. By doing so, a M \times N image becomes M/2 \times N/2 image. So area reduces to one-fourth of original area. It is called an Octave. The same pattern continues as we go upper in pyramid (ie, resolution decreases). Similarly while expanding, area becomes 4 times in each level. We can find Gaussian pyramids using cv2.pyrDown() and cv2.pyrUp() functions.
# Laplacian Pyramids are formed from the Gaussian Pyramids. There is no exclusive function for that. Laplacian pyramid images are like edge images only. Most of its elements are zeros. They are used in image compression. A level in Laplacian Pyramid is formed by the difference between that level in Gaussian Pyramid and expanded version of its upper level in Gaussian Pyramid.

plt.subplot(2, 2, 1), plt.imshow(higher_reso, cmap='gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 2, 2), plt.imshow(lower_reso, cmap='gray')
plt.title('lower_reso'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 2, 3), plt.imshow(higher_reso2, cmap='gray')
plt.title('higher_reso2'), plt.xticks([]), plt.yticks([])

plt.show()
Esempio n. 14
0
def cartoonifier(imagepath):

    #TO DO
    #step 1
    #Use bilateral filter for edge-aware smoothing.

    num_down = 1  # number of downsampling steps
    num_bilateral = 4  # number of bilateral filtering steps

    img = cv2.imread(imagepath)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # downsample image using Gaussian pyramid(see opencv 'pyrDown()' function)
    for i in range(num_down):
        img = cv2.pyrDown(img)

    # repeatedly apply small bilateral filter instead of
    # applying one large filter
    for i in range(num_bilateral):
        img = cv2.bilateralFilter(img, 15, 75, 75)

    # upsample image to original size (see opencv 'pyrUp()' function)
    for i in range(num_down):
        bilateralImg = cv2.pyrUp(img)

    #plt.imshow(bilateralImg)

    # ## Step#2
    # ### In this step we will blur the original image. This is considered as a pre-processing step before we move on towards the edge detection step. We will apply a median filter on the image, which replaces each pixel value with the median value of all the pixels in a small neighborhood.

    # In[5]:

    #TO DO
    #step 2
    # convert to grayscale and apply median blur
    img = cv2.imread(imagepath)
    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    medianBlurImg = cv2.medianBlur(grayImg, 5)

    #plt.imshow(medianBlurImg, cmap = 'gray')

    # ## Step#3
    #
    # ### In this step we will create an edge mask from the output produced in step#2 using adaptive thresholding

    # In[14]:

    #TO DO
    #step 3
    # detect and enhance edges(see opencv 'adaptiveThreshold()' function)
    edgeMask = cv2.adaptiveThreshold(
        medianBlurImg,
        255,
        cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
        cv2.THRESH_BINARY,
        11,
        2,
    )
    #plt.imshow(edgeMask, cmap = 'gray')

    # ## Final Step
    #
    # ### In this step we will combine the output produced in step#1 and step#3 using a bitwise and operator to produce our final output.(Note: You need to convert output from step#3 to color first)

    # In[40]:

    #TO DO
    #Final Step
    # convert back to color, bit-AND with color image
    finalMask = cv2.cvtColor(edgeMask, cv2.COLOR_GRAY2RGB)

    (x1, y1, z1) = bilateralImg.shape
    (x2, y2, z2) = finalMask.shape

    if x1 > x2:
        bilateralImg = np.delete(bilateralImg, x1 - 1, 0)
    elif x2 > x1:
        finalMask = np.delete(finalMask, x2 - 1, 0)

    if y1 > y2:
        bilateralImg = np.delete(bilateralImg, y1 - 1, 1)
    elif y2 > y1:
        finalMask = np.delete(finalMask, y2 - 1, 1)

    finalImg = cv2.bitwise_and(bilateralImg, finalMask, mask=None)

    return finalImg
Esempio n. 15
0
        continue
    cv2.imshow("Original",frame)
    
    img = cv2.resize(frame,(800,800))

    # DOWNSAMPLING + BILATERIAL FILTER
    img_c = img
    for ix in range(num_down):
        img_c = cv2.pyrDown(img)# Pyramid Down : Downsampling
    # print(img_c.shape)
    for iy in range(num_bi):
        img_c = cv2.bilateralFilter(img_c,d=9,sigmaColor=9,sigmaSpace=7) #Filtering
    # print(img_c.shape)
    #UPSAMPLING
    for ix in range(num_down):
        img_c = cv2.pyrUp(img_c)# Pyramid Down : Downsampling
    # print(img_c.shape)
    #BLUR and Threshold
    img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) # GRAY SCALE
    img_blur = cv2.medianBlur(img_gray,7) #MEDIAN BLUR
    img_edge = cv2.adaptiveThreshold(img_blur,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,blockSize=9,C=2)

    img_c = cv2.resize(img_c,(800,800))
    #RGB CONVERSION + BITWISE &
    img_edge = cv2.cvtColor(img_edge,cv2.COLOR_GRAY2RGB)
    # print(img_c.shape)
    # print(img_edge.shape)
    img_cartoon = cv2.bitwise_and(img_c,img_edge)

    stack = np.hstack([img,img_cartoon])
    cv2.imshow("Cartoon",stack)
Esempio n. 16
0
from cv2 import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

tmp_img = cv.imread('star.jpg')
cnt = 0
cv.imshow('win', tmp_img)
# 向下生成三级高斯金字塔
while (True):
    cnt += 1
    print(cnt)
    tmp_img = cv.pyrDown(tmp_img)
    cv.imshow('window', tmp_img)
    key = cv.waitKey(0)
    if key == 27 and cnt == 3:
        break

cnt = 0
#向上生成三级高斯金字塔
while (True):
    cnt += 1
    print(cnt)
    tmp_img = cv.pyrUp(tmp_img)
    cv.imshow('window', tmp_img)
    key = cv.waitKey(0)
    if key == 27 and cnt == 3:
        break
cv.destroyAllWindows()
Esempio n. 17
0
    def imagePyramidImg(self):
        imageFirst = Image.open(self.filename)
        imageLast = imageFirst.resize((450, 450), Image.ANTIALIAS)
        imageLast.save('img/dist/temp1.jpg')
        imageFirst2 = Image.open(self.filename2)
        imageLast2 = imageFirst2.resize((450, 450), Image.ANTIALIAS)
        imageLast2.save('img/dist/temp2.jpg')

        A = cv2.imread('img/dist/temp1.jpg')
        B = cv2.imread('img/dist/temp2.jpg')

        G = A.copy()
        gpA = [G]
        for i in range(6):
            G = cv2.pyrDown(G)
            gpA.append(G)

        G = B.copy()
        gpB = [G]
        for i in range(6):
            G = cv2.pyrDown(G)
            gpB.append(G)

        lpA = [gpA[5]]
        for i in range(6, 0, -1):

            GE = cv2.pyrUp(gpA[i])
            GE = cv2.resize(GE, gpA[i - 1].shape[-2::-1])
            L = cv2.subtract(gpA[i - 1], GE)
            lpA.append(L)

        lpB = [gpB[5]]
        for i in range(6, 0, -1):

            GE = cv2.pyrUp(gpB[i])
            GE = cv2.resize(GE, gpB[i - 1].shape[-2::-1])
            L = cv2.subtract(gpB[i - 1], GE)

            lpB.append(L)

        LS = []
        lpAc = []
        for i in range(len(lpA)):
            b = cv2.resize(lpA[i], lpB[i].shape[-2::-1])
            lpAc.append(b)

        j = 0
        for i in zip(lpAc, lpB):
            la, lb = i
            rows, cols, dpt = la.shape
            ls = np.hstack((la[:, 0:cols // 2], lb[:, cols // 2:]))
            j = j + 1
            LS.append(ls)

        ls_ = LS[0]
        for i in range(1, 6):
            ls_ = cv2.pyrUp(ls_)
            ls_ = cv2.resize(ls_, LS[i].shape[-2::-1])
            ls_ = cv2.add(ls_, LS[i])

        B = cv2.resize(B, A.shape[-2::-1])
        real = np.hstack((A[:, :cols // 2], B[:, cols // 2:]))

        cv2.imwrite('img/dist/pyramid.jpg', ls_)
# Load our input image
image = cv2.imread("./image.jpg")

# # Let's make our image 3/4 of it's original size
# image_scaled = cv2.resize(image, None, fx=0.75, fy=0.75)
# cv2.imshow("Scaling - Linear Interpolation", image_scaled)
# cv2.waitKey()

# # Let's double the size of our image
# img_scaled = cv2.resize(image, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
# cv2.imshow("Scaling - Cubic Interpolation", img_scaled)
# cv2.waitKey()

# # Let's skew the re-sizeing by setting exact dimensions
# img_scaled = cv2.resize(image, (900, 400), interpolation=cv2.INTER_AREA)
# cv2.imshow("Scaling - Skewed Size", img_scaled)
# cv2.waitKey()

# cv2.destroyAllWindows()

smaller = cv2.pyrDown(image)
larger = cv2.pyrUp(smaller)

cv2.imshow("Original", image)

cv2.imshow("Smaller ", smaller)
cv2.imshow("Larger", larger)

cv2.waitKey()
cv2.destroyAllWindows()