如何应用像素位置反转

问题描述 投票:0回答:1

目标

我正在尝试编写一个程序,将图像的一个区域映射到同一图像中的另一个区域。例如,如果我在图像中有一个圆形区域,我想将每个像素映射到圆的圆周上,我会将像素的位置乘以某个整数以将像素发送到该区域之外。基本上,如果你想象一个气球,当你用空气填充气球时,气球的表面正在膨胀,类似地你可以想象我想要做什么。我试图将所选圆形区域表面上的每个像素发送到图像中的不同位置。

代码

我一开始很简单,我通过将矩形区域中的每个像素发送到不同的位置来表示。

from PIL import Image
import math
import numpy as np
im = Image.open("gala.png")

width, _ = im.size

#value for the width of image section to be sent
for t in np.arange(0,420):
    xmin=int(115+t)
    xmax=xmin+1
    for i in np.arange(xmin, xmax):
        def getpixels(yaxis):
            pixels=[]
            for j in range(yaxis, yaxis+50):# the value added to yaxis gives value for the height of the image section to be sent
                r, g, b = im.getpixel((i,j))
                pixels.append((r,g,b))
            return pixels

    def replacepixels(yaxis,pixels):
        index=0

        for j in range(yaxis,yaxis+50):
            im.putpixel((i,j),pixels[index])
            index=index+1


        def doalljob(yaxis1,yaxis2):
            firstpart=getpixels(yaxis1)
            secondpart=getpixels(yaxis2)
            replacepixels(yaxis2,firstpart)
        #tells where in y axis to send the pixels
        doalljob(50,220)

im.show()

然后我更进一步,然后将每个像素映射到该区域

from PIL import Image
import math
from PIL import Image, ImageDraw, ImageFilter
import numpy as np
import cv2
im = Image.open("gala1.png")

width, _ = im.size

#value for the width of image section to be sent
for t in range(210,420):
    xmin=115+int(t)
    xmax=xmin+1
    for i in range(xmin, xmax):
        def getpixels(yaxis):
            pixels=[]
            for j in range(yaxis, yaxis+50):# the value added to yaxis gives value for the height of the image section to be sent
                r, g, b = im.getpixel((i,j))
                pixels.append((r,g,b))
            return pixels

        def replacepixels(yaxis,pixels):
            index=0

            for j in range(yaxis,yaxis+50):
                im.putpixel((i,j),pixels[index])
                index=index+1


        def doalljob(yaxis1,yaxis2):
            firstpart=getpixels(yaxis1)
            secondpart=getpixels(yaxis2)
            replacepixels(yaxis2,firstpart)
        #tells where in y axis to send the pixels
        j=int(((t-210)/29)**(5/2))
        doalljob(50,j+130)
for tk in range(0,210):
    xmin=115+int(tk)
    xmax=xmin+1
    for i in range(xmin, xmax):
        def getpixels(yaxis):
            pixels=[]
            for j in range(yaxis, yaxis+50):# the value added to yaxis gives value for the height of the image section to be sent
                r, g, b = im.getpixel((i,j))
                pixels.append((r,g,b))
            return pixels

        def replacepixels(yaxis,pixels):
            index=0

            for j in range(yaxis,yaxis+50):
                im.putpixel((i,j),pixels[index])
                index=index+1


        def doalljob(yaxis1,yaxis2):
            firstpart=getpixels(yaxis1)
            secondpart=getpixels(yaxis2)
            replacepixels(yaxis2,firstpart)
        #tells where in y axis to send the pixels
        j=int(((210-tk)/29)**(5/2))
        doalljob(50,j+130)

im.show()

这是我为第一个代码获得的图像

这是我获得的第二个代码的图像

这是我所期望的 问题 主要问题是选择一个圆形区域来发送像素。我尝试过使用圆方程来选择圆形区域的方法,但结果一团糟。

谢谢!

python-3.x opencv image-processing
1个回答
0
投票

正如 @Tino D 指出的那样,很难理解你想要实现的目标,而且在循环内定义函数这一事实也没有帮助。但是,话虽这么说,我假设您想要图像上像素的增量位移。我建议这种类型的方法:

from PIL import Image
import numpy as np

def shift_pixels(im_array, x, y_start, y_end, shift):
    """Vertical shift"""
    height = y_end - y_start
    if shift < 0:
        for y in range(y_start, y_end):
            im_array[y+shift, x] = im_array[y, x]
    else:
        for y in range(y_end-1, y_start-1, -1):
            im_array[y+shift, x] = im_array[y, x]

def apply_shift(im, shift_function):
    """Apply a vertical shift to each vertical strip"""
    im_array = np.array(im)
    width, height, _ = im_array.shape
    for x in range(width):
        shift = shift_function(x)
        shift_pixels(im_array, x, 50, 100, shift) 
    return Image.fromarray(im_array)

def shift_function_example(x):
    """Example"""
    if 115 <= x < 325:
        return int(((x - 115) / 29)**(5/2))
    return 0

im = Image.open(r"C:\Users\s-degossondevarennes\Downloads\galaxy.jpg")

shifted_im = apply_shift(im, shift_function_example)

shifted_im.show()

这给出了

© www.soinside.com 2019 - 2024. All rights reserved.