2013-07-03 113 views
3

我想水平翻轉圖像。翻轉圖像Python

從這:

Original Pic

這樣:

Flipped Pic

,但我一直得到它反映了一半。

這樣的:

Result I get

我試圖扭轉x軸指數,我不明白爲什麼它被分成。

def flip(picture): 
    height = getHeight(picture) 
    width = getWidth(picture) 
    newPicture = makeEmptyPicture(width, height) 
    x2 = width-1 
    for x in range(0, width): 
     y2 = 0 
     for y in range(0, height): 
      pxl = getPixel(picture, x, y) 
      newPxl = getPixel(picture, x2, y2) 
      color = getColor(pxl) 
      setColor(newPxl, color) 
      y2 = y2+1 
     x2 = x2-1 
    return picture 

我的代碼的其餘部分:

def d():  
    f = pickAFile() 
    picture = makePicture(f)   
    newPicture = copy(picture)   
    writePictureTo(newPicture, r"D:\FOLDER\newPic4.jpg") 
    explore(newPicture) 

回答

3

在你flip()功能(如在任何功能),由其他的答案中提到,您返回picture這是作爲函數的參數傳遞的形象,但在d()被定義...

這是變量的scope的問題,所以我請你再看看我們的討論here

在這裏,你有兩個選擇(你在兩者之間取得了熔化):

  • 直接修改給定的參數
  • 創建newPicture,則picture修改它,最後它返回
關於2D選項

詳情:

的重要的是picture變量屬於d()函數(d()是它的範圍)。與此同時newPicture變量屬於flip()函數(flip()是其範圍)。所以newPicture的生命週期是flip()(即一旦你終止flip()函數的執行,在返回的時候它就會被銷燬)。除非你將它返回給d(),否則d()對這個newPicture不知道任何事情。

因此,簡而言之(假設我們正在談論的第二個選項):

1)創建一個函數,接受picture爲參數(翻轉())

2)內翻轉(),創建局部變量newPicture並且僅修改那一個,使得原始picture保持不變

3)返回向母公司scope重新更新newPicture。這裏d()調用flip(),所以它是父範圍。我們要創建一個三維變量(屬於d()範圍內),以保持在什麼被翻轉()返回的手:

def flip(picture) 
    # Create newPicture 
    # Modify newPicture (using the information from the "picture" parameter) 
    setColor(newPicture, ...) 
    ... 
    return newPicture 

def d(): 
    file = PickAFile() 
    original_pic = makePicture(file) 
    finalNewPicture = flip(original_pic)  # {1} 
    show(finalNewPicture) 

{1}:在這裏,我們可以指定由翻轉返回的值(即newPicture)到更高範圍變量finalNewPicture(處理程序)...

我希望它可以幫助您瞭解背後的邏輯。這就像俄羅斯娃娃:newPicture用於內的倒裝(),這是內部d()使用,...


編輯:

我也想給一個關於第一個選項的解釋...

1)創建一個函數,一個picture作爲參數(翻轉())

2)內部翻轉(),直接在更高修改作用域picture可變

3)不要返回任何東西從flip()

這將導致:

def flip(picture) 
    # Simply modify the variable "picture", given as a parameter 
    setColor(picture, ...) 
    ... 
    # Do not return anything 

def d(): 
    file = PickAFile() 
    original_pic = makePicture(file) 
    flip(original_pic)      # {1} 
    show(original_pic) 

{1}:這裏flip()直接在輸入圖片上進行更改,因此我們可以直接顯示原始修改圖片(original_pic)。不需要中間處理程序變量。


的選項1代碼:(因爲你已經有工作的選項2)

def flip(picture): 
    height = getHeight(picture) 
    width = getWidth(picture) 

    x2=width-1 
    for x in range(0, width/2): # Only process the half way 
    y2=0 
    for y in range(0, height): 
     # swap pix and pix2 
     pxl = getPixel(picture, x, y) 
     pxl2 = getPixel(picture, x2, y2) 
     color = getColor(pxl) 
     color2 = getColor(pxl2) 
     setColor(pxl2, color) 
     setColor(pxl, color2) 
     y2=y2+1 
    x2=x2-1 

def d():  
    f = pickAFile() 
    original_picture = makePicture(f)   
    flip2(original_picture)   
    show(original_picture) 

d() 

注:翻蓋可以按如下方式得到了廣泛的簡化:

def flip2(picture): 
    height = getHeight(picture) 
    width = getWidth(picture) 

    for x in range(0, width/2): # Only process the half way 
    for y in range(0, height): 
     # swap pix and pix2 
     pxl = getPixel(picture, x, y) 
     pxl2 = getPixel(picture, width-1-x, y) 
     color = getColor(pxl) 
     color2 = getColor(pxl2) 
     setColor(pxl2, color) 
     setColor(pxl, color2) 
5
def flip(picture): 
    height = getHeight(picture) 
    width = getWidth(picture) 
    newPicture = makeEmptyPicture(width, height) 
    for x in range(width): 
    for y in range(height): 
     color = getColor(getPixel(picture, x, y)) 
     setColor(getPixel(newPicture, width-1-x, y), color) 
    return newPicture 

這個怎麼樣?我剛剛刪除了對我來說似乎不必要的東西x2/y2。否則它應該以這種方式工作。我發現代碼中沒有真正的錯誤,只是在最後返回picture而不是newPicture。但是這也不應該導致鏡像。

+1

'寬度x'應該是'寬1 x' ...一個細節,但仍然;) –

+0

感謝@Golgauth指出(我固定我的答案)。 – Alfe

4

您正在更改原始圖片中的像素而不是新創建的newPicture。此外,您正在返回原始(現在修改)的圖片。

你應該有

newPxl = getPixel(newPicture, x2,y2) 

,你應該用

return newPicture 

也結束了,我想你的代碼翻轉圖像周圍的水平軸,而不是你的圖片顯示的是垂直的。

1

OpenCV提供了翻轉圖像的功能。

void flip(array src, array dst, int flipCode) 

翻轉圍繞垂直,水平或兩個軸的2D陣列。

參數:

  • src - 源陣列
  • dst - 目標陣列;將具有與src相同的大小和相同類型
  • flipCode - 指定如何翻轉數組:0表示翻轉x軸,正值(例如1)表示翻轉y軸,負值(例如 - 1)表示翻轉兩個軸。函數flip以三種不同方式之一翻轉數組(行列索引是基於0的)。

示例代碼:

cv.flip(original_image,flipped_image,1); 

編輯:您還可以指定被翻動的圖像:

flipped_image = cv2.flip(src=original_image, flipCode=1) 
+1

這樣做* python *通常等同於將翻轉的圖像簡單地分配給一個變量(函數將其作爲輸出生成)。例如。 'flipped_image = cv2.flip(src = original_image,flipCode = 1)'。 –