Image IO

opencv-python中的ImageIO

from __future__ import print_function

import
cv2 as cv alpha = 0.5 try: raw_input # Python 2 except NameError: raw_input = input # Python 3 print(''' Simple Linear Blender ----------------------- * Enter alpha [0.0-1.0]: ''') input_alpha = float(raw_input().strip()) if 0 <= alpha <= 1: alpha = input_alpha # [load] src1 = cv.imread('D:/cloud.jpg') src2 = cv.imread('D:/lena.jpg') print(src1.shape) # [load] if src1 is None: print("Error loading src1") exit(-1) elif src2 is None: print("Error loading src2") exit(-1) # [blend_images] beta = (1.0 - alpha) src2 = cv.resize(src2, (src1.shape[1],src1.shape[0])) print(src2.shape) dst = cv.addWeighted(src1, alpha, src2, beta, 0.0) # [blend_images] # [display] cv.namedWindow("dst", cv.WINDOW_AUTOSIZE) cv.imshow('dst', dst) cv.waitKey() # [display] cv.destroyAllWindows()

 图像锐化操作(手动及内置函数)

from __future__ import print_function
import sys
import time

import numpy as np
import cv2 as cv

## [basic_method]
def is_grayscale(my_image):
    return len(my_image.shape) < 3


def saturated(sum_value):
    if sum_value > 255:
        sum_value = 255
    if sum_value < 0:
        sum_value = 0

    return sum_value


def sharpen(my_image):
    if is_grayscale(my_image):
        height, width = my_image.shape
    else:
        my_image = cv.cvtColor(my_image, cv.CV_8U)
        height, width, n_channels = my_image.shape

    result = np.zeros(my_image.shape, my_image.dtype)
    ## [basic_method_loop]
    for j in range(1, height - 1):
        for i in range(1, width - 1):
            if is_grayscale(my_image):
                sum_value = 5 * my_image[j, i] - my_image[j + 1, i] - my_image[j - 1, i] \
                            - my_image[j, i + 1] - my_image[j, i - 1]
                result[j, i] = saturated(sum_value)
            else:
                for k in range(0, n_channels):
                    sum_value = 5 * my_image[j, i, k] - my_image[j + 1, i, k]  \
                                - my_image[j - 1, i, k] - my_image[j, i + 1, k]\
                                - my_image[j, i - 1, k]
                    result[j, i, k] = saturated(sum_value)
    ## [basic_method_loop]
    return result
## [basic_method]

def main(argv):
    filename = 'D:/lena.jpg'

    img_codec = cv.IMREAD_COLOR
    if argv:
        filename = sys.argv[1]
        if len(argv) >= 2 and sys.argv[2] == "G":
            img_codec = cv.IMREAD_GRAYSCALE

    src = cv.imread(filename, img_codec)

    if src is None:
        print("Can't open image [" + filename + "]")
        print("Usage:")
        print("mat_mask_operations.py [image_path -- default lena.jpg] [G -- grayscale]")
        return -1

    cv.namedWindow("Input", cv.WINDOW_AUTOSIZE)
    cv.namedWindow("Output", cv.WINDOW_AUTOSIZE)
    cv.namedWindow("Built-in", cv.WINDOW_AUTOSIZE)

    cv.imshow("Input", src)
    t = round(time.time())

    dst0 = sharpen(src)

    t = (time.time() - t)
    print("Hand written function time passed in seconds: %s" % t)

    cv.imshow("Output", dst0)
    cv.waitKey()

    t = time.time()
    ## [kern]
    kernel = np.array([[0, -1, 0],
                       [-1, 5, -1],
                       [0, -1, 0]], np.float32)  # kernel should be floating point type
    ## [kern]
    ## [filter2D]
    dst1 = cv.filter2D(src, -1, kernel)
    # ddepth = -1, means destination image has depth same as input image
    ## [filter2D]

    t = (time.time() - t)
    print("Built-in filter2D time passed in seconds:     %s" % t)

    cv.imshow("Built-in", dst1)

    cv.waitKey()
    cv.destroyAllWindows()
    return 0


if __name__ == "__main__":
    main(sys.argv[1:])

原图:

 手动锐化时间:

Hand written function time passed in seconds: 6.753117084503174

内置函数:

Built-in filter2D time passed in seconds:     0.01554727554321289

 Sobel 算子

from __future__ import division
import cv2 as cv
import numpy as np

def visualize_images():
    ## [imshow 1]
    img = cv.imread('D:/lena.jpg')
    cv.namedWindow('image', cv.WINDOW_AUTOSIZE)
    cv.imshow('image', img)
    cv.waitKey(3)
    ## [imshow 1]

    ## [imshow 2]
    img = cv.imread('D:/lena.jpg')
    grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    sobelx = cv.Sobel(grey, cv.CV_32F, 1, 0)
    # find minimum and maximum intensities
    minVal = np.amin(sobelx)
    maxVal = np.amax(sobelx)
    draw = cv.convertScaleAbs(sobelx, alpha=255.0/(maxVal - minVal), beta=-minVal * 255.0/(maxVal - minVal))
    cv.namedWindow('image', cv.WINDOW_AUTOSIZE)
    cv.imshow('image', draw)
    cv.waitKey()
    ## [imshow 2]

if __name__ == '__main__':
    visualize_images()

 由于求导之后,导数不一定在0到255之间,所以需要对像素进行线性变换。

 

posted @ 2021-02-22 20:27  为红颜  阅读(276)  评论(0编辑  收藏  举报