0

I have managed to make two different code, one of them from another post here on stack overflow, which both scales an image up without the used of any openCV functions like resize, which both uses backward mapping, but I cannot seem to convert the codes into forward mapping, which would result in a scaled image with a black "pattern" of pixels. If any doubt about what forward / backward mapping is a link explanation can be found here

I use the same image for both codes. I have looked at the theory for forward mapping but as of yet have not been able to implement it, I keep getting errors which I have been trying to debug but no luck so far. sometimes I do get a completely black image but with the right size.

Image U use

The code below is the working code of scaling and not the code were I have tried forward mapping if need I can also post that.

Code:

import cv2 as cv
import numpy as np

img = cv.imread('Scale.jpg')

def upScaling(image, scale):
    (h, w, dim) = img.shape
    scaleH = h * scale
    scaleW = w * scale
    #making sure that the scale factor is an integer
    sH = int(scaleH)
    sW = int(scaleW)
    # Get the ratio of the rows
    row_ratio = h / scaleH
    # Getting the ratio of the cols
    column_ratio = w / scaleW
    # i get a list position of the rows
    row_position = np.floor(np.arange(sH) * row_ratio).astype(int)
    #getting a list of position of the columns
    column_position = np.floor(np.arange(sW) * column_ratio).astype(int)
    # Initialize The scaled image called imageScaled
    scaledUp = np.zeros((sH, sW, 3), np.uint8)
    for i in range(sH):
        for j in range(sW):

        scaledUp[i, j] = img[row_position[i], column_position[j]]

    return scaledUp
ScalingUp = upScaling(img, 1.3)
cv.imshow('Scaling up', ScalingUp)

Code #2:

    import cv2

    import numpy as np

    img = cv2.imread('Scale.jpg', 0)

    cv2.imshow('unscaled', img)

    h,w = img.shape[:2]

    print(h)

    print(w)

    def resizePixels(pixels,w1,h1,w2,h2):
        retval = []
        x_ratio = (int)((w1<<16)/w2) +1
        print(x_ratio)
        y_ratio = (int)((h1<<16)/h2) +1
        print(y_ratio)
        for i in range(h2):
            for j in range(w2):
                x2 = ((j*x_ratio)>>16)
                y2 = ((i*y_ratio)>>16)
            #add pixel values from original image to an array
            retval.append(pixels[y2,x2])
        return retval;

    ret = resizePixels(img,w,h,500,500)
    #reshape the array to get the resize image
    dst = np.reshape(ret,(500,500))

    cv2.imshow('Resize',dst)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Hopefully, it is possible to convert my current code into a version that uses forward mapping, if not I have no problem making a new code.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Can you only show the relevant code to your question? So much code could be overwhelming for a reader. Maybe remove any unnecessary comments? – Mohit Motwani Nov 15 '18 at 10:17
  • you can get help for interpolation from here https://stackoverflow.com/questions/53302184/why-is-my-bilinear-interpolation-vastly-different-from-the-in-built-matlab-funct – user8190410 Nov 15 '18 at 17:14

0 Answers0