在Python 3中使用4像素平均方法缩放图像

时间:2019-02-08 13:25:56

标签: python opencv computer-vision

我正在尝试缩放图像,我的问题如下: 我真的不知道该如何实现循环以读取原始图像中的所有像素,然后在我的新数组中平均放置4个像素(在这种情况下为原始图像的75%)...第一次在这里发布我,希望我的话题得到很好的建议。

import numpy as np
import cv2

    def averageScale(img, scaleSize, swSize):# Space and Radiometric Scale; swSize is the sliding window nxn where ill place the 4 pixels

    #loop parameters    
        w, h = img.shape
        lb_i = int((swSize/2))
        lb_j = lb_i
        ub_i = w - lb_i
        ub_j = h - lb_j

        #Image processing items
        newImage = np.zeros((scaleSize[1], scaleSize[0]), np.float)# H x W
        #print(newImage.shape)

        for i in range(lb_i, ub_i, 2):
            for j in range(lb_j, ub_j, 2):

                pixel = mean4Pixels(img, i-lb_i, j-lb_j, swSize)

                newImage[int(i/2)][int(j/2)] = pixel

        return newImage

    def mean4Pixels(img, index1, index2, swSize):

        slidingWindow = np.zeros((swfSize, swSize), np.float)# NxN window

        for i in range(0, swSize):
            for j in range(0, swSize):

                slidingWindow[i][j] = img[index1+i][index2+j]

        averageResult = sum(slidingWindow.sum(axis=0))/(swSize*2)

        return averageResult`

    img = np.uint8(cv2.imread("./images/imageScaling/coverMagazine.jpg"))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    w, h, d = img.shape

    scaleSize = np.array((w*(3/4),h*(3/4)), np.uint8)
    scaleSize = tuple(scaleSize)

    scaledImg = averageScale(img[:,:,0], scaleSize, 2)

0 个答案:

没有答案