Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python镜像图像_Python_Opencv_Image Processing - Fatal编程技术网

使用Python镜像图像

使用Python镜像图像,python,opencv,image-processing,Python,Opencv,Image Processing,我需要水平翻转图片,不使用反转功能,,我以为我做对了,但返回的图像只是图片的右下角,没有翻转 我的密码是 def Flip(image1, image2): img = graphics.Image(graphics.Point(0, 0), image1) X = img.getWidth() Y = img.getHeight() for y in range(Y): for x in range(X): A = img

我需要水平翻转图片,不使用反转功能,,我以为我做对了,但返回的图像只是图片的右下角,没有翻转

我的密码是

def Flip(image1, image2):
    img = graphics.Image(graphics.Point(0, 0), image1)
    X = img.getWidth()
    Y = img.getHeight()
    for y in range(Y):
        for x in range(X):
            A = img.getPixel(x,y)
            r = A[0]
            g = A[1]
            b = A[2]
            color = graphics.color_rgb(r,g,b)
            img.setPixel(X-x,y,color)
    img = graphics.Image(graphics.Point(0,0), image2)
    win = graphics.GraphWin(image2, img.getWidth(), img.getHeight())
    img.draw(win)

我哪里出错了?

这里有一些我认为可以改进的地方:

def Flip(image1, image2):
    img = graphics.Image(graphics.Point(0, 0), image1)
    X = img.getWidth()
    Y = img.getHeight()
    for y in range(Y):
        for x in range(X):
            A = img.getPixel(x,y)
            r = A[0]
            g = A[1]
            b = A[2]
            color = graphics.color_rgb(r,g,b)
这项任务可能更像蟒蛇:

img
现在图像翻转了一半。这是因为您正在同一图像源上编写内容,在到达中间位置之前,会随时丢失旧内容。(请注意,
X-X
会将图像大小增加1个像素。如果图像宽度为100,则在第一次迭代中,
X-X=100-0=100
由于它从0开始,因此图像会变宽1个像素。)然后,开始复制回。此外,您不使用该内容,因为:

    img = graphics.Image(graphics.Point(0,0), image2)
问题是:您只是过度编写了
img
的内容,而没有赋予它任何用途。后来:

    win = graphics.GraphWin(image2, img.getWidth(), img.getHeight())
    img.draw(win)
这似乎与函数的目的无关(翻转图像)。我要做的是:

import graphics
import sys

def Flip(image_filename):
    img_src = graphics.Image(graphics.Point(0, 0), image_filename)
    img_dst = img_src.clone()
    X, Y = img_src.getWidth(), img_src.getHeight()
    for x in range(X):
        for y in range(Y):
            r, g, b = img_src.getPixel(x, y)
            color = graphics.color_rgb(r, g, b)
            img_dst.setPixel(X-x-1, y, color)

    return img_dst

if __name__ == '__main__':
    input = sys.argv[1] or 'my_image.ppm'
    output = 'mirror-%s' % input
    img = Flip (input)
    img.save(output)
请注意,函数
Flip
仅负责翻转图像,在函数之外,您可以根据需要对图像执行任何操作,如“main”程序中所示

如果您只想使用一个图像,这是可能的,而且效率更高。为此,可以使用相同的原则在变量之间交换值:

def Flip(image_filename):
    img = graphics.Image(graphics.Point(0, 0), image_filename)
    X, Y = img.getWidth(), img.getHeight()
    for x in range(X/2):
        for y in range(Y):
            r_1, g_1, b_1 = img.getPixel(x, y)
            color_1 = graphics.color_rgb(r_1, g_1, b_1)

            r_2, g_2, b_2 = img.getPixel(X-x-1, y)
            color_2 = graphics.color_rgb(r_2, g_2, b_2)

            img.setPixel(X-x-1, y, color_1)
            img.setPixel(x, y, color_2)

    return img

您需要翻转一张图片,但您的函数需要两个输入?奇怪?第一个输入是正在输入的文件,第二个输入是文件的文件名output@cah这些都是令人困惑的参数(和局部变量)名称。您也在重复使用变量
img
,这对两件事没有帮助。@cah也似乎是您第一次尝试将
image1
翻转到位,除了因为您没有交换像素,所以在读取数据之前会将一半的数据重击。然后你扔掉结果,打开
image2
,出于某种原因把它画进去……我试过了,现在把图像翻过来了,但它的大小仍然不对。这是我最初的问题,它只显示了图像的四分之一,但现在图像的四分之一被翻转,所以这部分是好的,但我不知道为什么它不是正确的大小,我看到了问题。图像被覆盖,丢失了一半图像的信息。我添加了解释和修复。我没有要导入的sys,是否有其他方法来执行克隆功能?或者用另一种方法来编写代码来解决问题?
import sys
部分只是为了让将来遇到类似问题的任何读者都能完整地理解这个示例,而且它还使测试用例更简单。你应该可以用任何你需要的东西来代替它。请参阅我添加的另一个示例。
def Flip(image_filename):
    img = graphics.Image(graphics.Point(0, 0), image_filename)
    X, Y = img.getWidth(), img.getHeight()
    for x in range(X/2):
        for y in range(Y):
            r_1, g_1, b_1 = img.getPixel(x, y)
            color_1 = graphics.color_rgb(r_1, g_1, b_1)

            r_2, g_2, b_2 = img.getPixel(X-x-1, y)
            color_2 = graphics.color_rgb(r_2, g_2, b_2)

            img.setPixel(X-x-1, y, color_1)
            img.setPixel(x, y, color_2)

    return img