zoukankan      html  css  js  c++  java
  • Coursera:Image and Vedio Processing: From Mars to Hollywood作业(部分)

    • Write a computer program capable of reducing the number of intensity levels in an image from 256 to 2, in integer powers of 2. The desired number of intensity levels needs to be a variable input to your program.
    • Using any programming language you feel comfortable with (it is though recommended to use the provided free Matlab), load an image and then perform a simple spatial 3x3 average of image pixels. In other words, replace the value of every pixel by the average of the values in its 3x3 neighborhood. If the pixel is located at (0,0), this means averaging the values of the pixels at the positions (-1,1), (0,1), (1,1), (-1,0), (0,0), (1,0), (-1,-1), (0,-1), and (1,-1). Be careful with pixels at the image boundaries. Repeat the process for a 10x10 neighborhood and again for a 20x20 neighborhood. Observe what happens to the image (we will discuss this in more details in the very near future, about week 3).
    • Rotate the image by 45 and 90 degrees (Matlab provides simple command lines for doing this).
    • For every 3×3 block of the image (without overlapping), replace all corresponding 9 pixels by their average. This operation simulates reducing the image spatial resolution. Repeat this for 5×5 blocks and 7×7 blocks. If you are using Matlab, investigate simple command lines to do this important operation.

     task1.py

    1 def task1(img):
    2     jpg = cv2.imread(img, 0)
    3     while(True):
    4         level = int(input('choise level(1-8):'))
    5         cv2.imshow('level{}'.format(level), jpg-math.pow(2,level))
    6         cv2.waitKey()

     task2_1.py:

     1 import cv2
     2 import numpy as np
     3 
     4 filter = np.zeros((3, 3))
     5 
     6 def avg(matrix):
     7     return matrix.sum() // 9
     8 
     9 def filter_flush():
    10     filter[:, :] = 0
    11 
    12 def task(img):
    13     jpg = cv2.imread(img, 0)
    14     jc = jpg.copy()
    15     rl = jpg.shape[0]
    16     cl = jpg.shape[1]
    17 
    18     #x = [0, 0]
    19     filter_flush()
    20     filter[1:, 1:] = jpg[:1, :1]
    21     jc[0][0] = avg(filter)
    22     #x = [0, 1:cl-1]
    23     for ci in range(1, cl - 1):
    24         filter_flush()
    25         filter[1:, :] = jpg[:2, ci-1:ci+2]
    26         jc[0][ci] = avg(filter)
    27     #x = [0,cl-1]
    28     filter_flush()
    29     filter[1:, :2] = jpg[:2, cl-2:]
    30     jc[0][cl-1] = avg(filter)
    31     #x = [1:rl-1, :]
    32     for ri in range(1, rl-1):
    33         #x = [, 0]
    34         filter_flush()
    35         filter[:, 1:] = jpg[ri-1:ri+2, :2]
    36         jc[ri][0] = avg(filter)
    37         #x = [, 1:cl-1]
    38         for ci in range(1, cl-1):
    39             filter_flush()
    40             filter[:, :] = jpg[ri-1:ri+2, ci-1:ci+2]
    41             jc[ri][ci] = avg(filter)
    42         #x = [, cl-1]
    43         filter_flush()
    44         filter[:, :2] = jpg[ri-1:ri+2, cl-2:]
    45         jc[ri][cl-1] = avg(filter)
    46     #x = [rl-1, 0]
    47     filter_flush()
    48     filter[:2, 1:] = jpg[rl-2:, :2]
    49     jc[rl-1][0] = avg(filter)
    50     #x = [rl-1, :cl-1]
    51     for ci in range(1, cl-1):
    52         filter_flush()
    53         filter[1:, :] = jpg[rl-2:, ci-1:ci+2]
    54         jc[rl-1][ci] = avg(filter)
    55     #x = [rl-1, cl-1]
    56     filter_flush()
    57     filter[:2, :2] = jpg[rl-2:, cl-2:]
    58     jc[rl-1][cl-1] = avg(filter)
    59 
    60     cv2.imshow('origin', jpg)
    61     cv2.imshow('after average', jc)
    62     cv2.waitKey()

     task2_2.py

     1 import cv2
     2 import numpy as np
     3 filter = None
     4 
     5 def task2_2(img):
     6     global filter
     7     filter = np.zeros((10, 10))
     8     jpg = cv2.imread(img, 0)
     9     rl = jpg.shape[0]
    10     cl = jpg.shape[1]
    11     ground = np.pad(jpg, ((5,5), (5,5)), 'constant')
    12 
    13     #因为filter是10*10的,所以每个xi都要被卷积四次
    14     gv = np.zeros((rl+2, cl+2))
    15     for ri in range(rl+1):
    16         for ci in range(cl+1):
    17             filter_flush()
    18             filter[:] = ground[ri:ri+10, ci:ci+10]
    19             gv[ri][ci] = gv[ri][ci+1] = gv[ri+1][ci] = gv[ri+1][ci+1] = (gv[ri][ci] + filter.mean()) / 2
    20     jnew = gv[1:gv.shape[0]-1, 1:gv.shape[1]-1]
    21     cv2.imwrite('filter10.jpg', jnew)

    task2_3.py

     1 import cv2
     2 import numpy as np
     3 
     4 def task(img):
     5     filter = np.zeros((20, 20))
     6     jpg = cv2.imread(img, 0)
     7     rl = jpg.shape[0]
     8     cl = jpg.shape[1]
     9     ground = np.pad(jpg, ((10,10), (10,10)), 'constant')
    10 
    11     #因为filter是偶*偶的,所以每个xi都要被卷积四次
    12     gv = np.zeros((rl+2, cl+2))
    13     for ri in range(rl+1):
    14         for ci in range(cl+1):
    15             filter[:] = ground[ri:ri+20, ci:ci+20]
    16             gv[ri][ci] = gv[ri][ci+1] = gv[ri+1][ci] = gv[ri+1][ci+1] = (gv[ri][ci] + filter.mean()) / 2
    17     jnew = gv[1:gv.shape[0]-1, 1:gv.shape[1]-1]
    18     cv2.imwrite('filter20.jpg', jnew)

     task3_1.py

    1 import cv2
    2 import imutils
    3 
    4 def task(img):
    5     cv2.namedWindow('rotate image', cv2.WINDOW_NORMAL)
    6     image = cv2.imread(img)
    7     rot = imutils.rotate(image, angle = 45)
    8     cv2.imshow('rotate image', rot)
    9     cv2.waitKey()

    task3_2.py

    1 import cv2
    2 import imutils
    3 
    4 def task(img):
    5     cv2.namedWindow('rotate image', cv2.WINDOW_NORMAL)
    6     image = cv2.imread(img)
    7     rot = imutils.rotate(image, angle = 90)
    8     cv2.imshow('rotate image', rot)
    9     cv2.waitKey()

    (注:这一题除了使用cv2和imutils外,还可以使用Pillow库Image.rotate())

    task4.py

     1 import cv2
     2 import numpy as np
     3 
     4 def task(img):
     5     cv2.namedWindow('origin', cv2.WINDOW_NORMAL)
     6     cv2.namedWindow('filtered', cv2.WINDOW_NORMAL)
     7     image = cv2.imread(img)
     8     kernel = np.ones((3, 3), np.float32) / 9
     9     # kernel = np.ones((5, 5), np.float32) / 25
    10     # kernel = np.ones((7, 7), np.float32) / 49
    11     image_new = cv2.filter2D(image, -1, kernel)
    12     cv2.imshow('origin', image)
    13     cv2.imshow('filtered', image_new)
    14     cv2.waitKey()
    
    
    • Do a basic implementation of JPEG:
    1. Divide the image into non-overlapping 8x8 blocks.
    2. Compute the DCT (discrete cosine transform) of each block. This is implemented in popular packages such as Matlab.
    3. Quantize each block. You can do this using the tables in the video or simply divide each coefficient by N, round the result to the nearest integer, and multiply back by N. Try for different values of N.
    4. You can also try preserving the 8 largest coefficients (out of the total of 8x8=64), and simply rounding them to the closest integer.
    5. Visualize the results after inverting the quantization and the DCT.

    task1.py

     1 import cv2
     2 import numpy as np
     3 
     4 def task(img):
     5     image = cv2.imread(img, 0) #第二个参数指定为0是为了以灰度模式读取图像
     6     rows = image.shape[0]
     7     cols = image.shape[1]
     8     image_G = np.pad(image, ((0, 8), (0, 8)))
     9     blocks = {}
    10     count = 0
    11     for i in range(0, rows, 8):
    12         for j in range(0, cols, 8):
    13             blocks[count] = image_G[i:i+8, j:j+8]
    14             count += 1
    15     print(blocks)

    task2.py

     1 import cv2
     2 import numpy as np
     3 
     4 def task(img):
     5     image = cv2.imread(img, 0) #第二个参数指定为0是为了以灰度模式读取图像
     6     rows = image.shape[0]
     7     cols = image.shape[1]
     8     image_G = np.pad(image, ((0, 8), (0, 8)))
     9     image_DCT = image_G.copy()
    10     for i in range(0, rows, 8):
    11         for j in range(0, cols, 8):
    12             block = image_G[i:i+8, j:j+8]
    13             image_DCT[i:i+8, j:j+8] = cv2.dct(block.astype(np.float))
    14     image_DCT = image_DCT[:rows, :cols]
    15     print(image_DCT)
  • 相关阅读:
    vue typescript: Parameter ‘XXX’ implicitly has an ‘any’ type.解决方法
    ESLint学习(一)简介、安装、配置、命令行、规则
    使用vite搭建vue3项目(三) 配置国际化
    VUE3+Typescript 引用process提示错误
    centos7安装vsftpd,配置
    使用vite搭建vue3项目(一) 创建一个工程
    Symbols 是 JavaScript 最新推出的一种基本类型
    使用vite搭建vue3项目(二) 引入vuerouter
    在使用vite时使用import.meta.globEager 代替 require.context 自动导入文件
    VUE3.X版本error: Parsing error: Parsing error: Unexpected token 的解决办法
  • 原文地址:https://www.cnblogs.com/J14nWe1/p/14392408.html
Copyright © 2011-2022 走看看