像素注释与scipy.ndimage.geometric\u变换

2024-05-15 11:52:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在numpy图像上实现置换贴图。这是我的实现。你知道吗

def displaceImage(img,displacementMap,distance=10,angle=45):
    """
    NOTE: If displacementMap is smaller than img, then everthing out of bounds
    is not displaced!
    """
    angle=math.radians(angle)
    img=numpyArray(img)
    displacementMap=numpyArray(displacementMap)
    if len(displacementMap.shape)>2:
        # convert displacement map to grayscale
        displacementMap=colorSpaces.grayscale(displacementMap)
    def dissp(point):
        """
        Called for each point in the array.  Returns a same shaped tuple where it should go.
        """
        if point[0]<displacementMap.shape[0] and point[1]<displacementMap.shape[1]:
            delta=displacementMap[point[0],point[1]]*distance
            toX=point[0]+delta*math.sin(angle)
            toY=point[1]-delta*math.cos(angle)
            if len(point)>2: # array is (w,h,rgb)
                point=(toX,toY,point[2])
            else: # array is (w,h) -- aka grayscale
                point=(toX,toY)
        return point
    img=scipy.ndimage.geometric_transform(img,dissp,mode="nearest")
    return img

下面我尝试用一个b&w柏林噪声来移动到图像的左上象限。从中央的树叶/天空区域可以看到,它似乎在做人们所期望的事情。当然,问题是到处都是散乱的像素。思想?你知道吗

resultant image


Tags: 图像toximgifisdefmatharray