2013-02-08 8 views
3

저는 파이썬에서 프로그래밍 클래스를 사용하고 있습니다. 미러 포인트를 정의한 다음 중첩 된 for 루프를 사용하여 한면에서 다른면으로 픽셀을 복사하여 이미지를 미러링하는 중입니다.미러 이미지 파이썬에서 대각선으로

def mirrorVertical(source): 
mirrorPoint = getWidth(source)/2 
width = getWidth(source) 
for y in range(0,getHeight(source)): 
    for x in range(0,mirrorPoint): 
    leftPixel = getPixel(source,x,y) 
    rightPixel = getPixel(source,width - x - 1,y) 
    color = getColor(leftPixel) 
    setColor(rightPixel,color) 

나는 현재 왼쪽 상단이 하단 우측에 반영됩니다 있도록 대각선 이미지를 반영하기 위해 우리를 요청하는 할당 문제에 일하고 있어요 예를 들어, 수직 이미지를 미러링 다음 코드를 사용합니다. 지금까지 발견 한 모든 예제와 답변은 정사각형 이미지에만 적용되며 모든 이미지에이를 적용 할 수 있어야합니다. 가능한 한 대각선 미러 포인트를 정의해야합니다. 나는 y = mx + b 스타일 방정식을 사용하여 미러 포인트를 정의하려고 시도했지만 파이썬에게 그 라인을 만드는 방법을 알 수는 없습니다. 정사각형 이미지와 관련이없는 도움을 주시면 감사하겠습니다!

참고 : 저는 여기에서 아주 새로운 이미지이기 때문에 아직 이미지를 게시 할 수 없지만 대각선 미러 포인트는 왼쪽 하단에서 오른쪽 상단까지 실행됩니다. 왼쪽 상단 삼각형의 이미지가 오른쪽 하단에 반영됩니다.

답변

0

대각선이 아닌 45 도선을 기준으로 미러링한다고 가정합니다.

너비는 원본 높이이고 너비는 원본 너비입니다. 새 이미지를 만들어야합니다.

좌표계의 원점이 왼쪽 하단 인 경우 원본의 점 (x, y)을 새 이미지의 (y, x) 위치에 복사하십시오.

height = getHeight(source) 
width = getWidth(source) 
for i in range(height - 1): 
    for j in range(int(width * float(height - i)/height)): 
     # Swap pixel i,j with j,i 

이 대각선을 따라 미러링 작동합니다

3

당신은이 같은 정사각형이 아닌 배열의 오른쪽과 왼쪽을 교체 할 수 있습니다), 또 다른 코너의 경우에, 당신은 좀 더 생각해야합니다. 당신은 어떤 임의의 위치를 ​​따라 비추고 싶다는 암시를하는 것처럼 보입니다. 이 경우 미러 라인의 반대쪽에 일치하는 픽셀이없는 픽셀을 채우는 방법을 결정해야합니다.

당신은 당신이 아마 루프를 명시 적으로 수행해야 할 필요가 있으므로 할당 작업을하고 있지만 언급 한 데이터를 숫자가 적은 배열에 넣으면 조합을 통해 원하는 것을 쉽게 (그리고보다 효율적으로) 달성 할 수 있습니다 numpy 함수 fliplr, flipudtranspose 중 하나입니다.

코드 예제 (왼쪽/오른쪽으로 미러링)에서 왼쪽 픽셀을 오른쪽으로 복사하지만 그 반대의 경우가 아니므로 실제로 이미지를 미러링하지 않습니다.

2

그냥 대각선으로 미러링하는 방법 중 하나를 공유 할 수는 오른쪽 상단에 하단 왼쪽 :

학생은 그가 왼쪽 아래에 오른쪽 상단 "에서 반영 할 필요가있는 경우, 적응을 담당하고있다 "반대 대각선을 사용하십시오.

# !! Works only with squared pictures... !! 
def diagMirrorPicture(picture): 
    height = getHeight(picture) 
    width = getWidth(picture) 

    if (height != width): 
     printNow("Error: The input image is not squared. Found [" + \ 
            str(width) + " x " + str(height) + "] !") 
     return None 

    newPicture = makeEmptyPicture(width, height) 

    mirrorPt = 0 
    for x in range(0, width, 1): 
     for y in range(mirrorPt, height, 1): 
      sourcePixel = getPixel(picture, x, y) 
      color = getColor(sourcePixel) 

      # Copy bottom-left as is 
      targetPixel = getPixel(newPicture, x, y) 
      setColor(targetPixel, color) 

      # Mirror bottom-left to top right 
      targetPixel = getPixel(newPicture, y, x) 
      #         ^^^^ (simply invert x and y) 
      setColor(targetPixel, color) 

     # Here we shift the mirror point 
     mirrorPt += 1 

    return newPicture 


file = pickAFile() 
picture = makePicture(file) 

picture = diagMirrorPicture(picture) 

if (picture): 
    writePictureTo(picture, "/home/mirror-diag2.png") 
    show(picture) 

참고 :이 mirrorPt 점 종축 통과 함께 독립적으로 화소 라인마다 수직 미러를 작동하는 것처럼 동작이.


출력 (안토니 타 피스에 의해 그림) :


................. enter image description here ... .............. enter image description here ..................



다음은 실험과 그냥 재미입니다

...




# Draw point, with check if the point is in the image area 
def drawPoint(pic, col, x, y): 
    if (x >= 0) and (x < getWidth(pic)) and (y >= 0) and (y < getHeight(pic)): 
    px = getPixel(pic, x, y) 
    setColor(px, col) 

# Draw line segment given two points 
# From Bresenham's line algorithm : 
# http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm 
def drawLine(pic, col, x0, y0, x1, y1): 

    dx = abs(x1-x0) 
    dy = abs(y1-y0) 
    sx = sy = 0 

    #sx = 1 if x0 < x1 else -1 
    #sy = 1 if y0 < y1 else -1 

    if (x0 < x1): 
    sx = 1 
    else: 
    sx = -1 
    if (y0 < y1): 
    sy = 1 
    else: 
    sy = -1 

    err = dx - dy 

    while (True): 

    drawPoint(pic, col, x0, y0) 

    if (x0 == x1) and (y0 == y1): 
     break 

    e2 = 2 * err 
    if (e2 > -dy): 
     err = err - dy 
     x0 = x0 + sx 

    if (x0 == x1) and (y0 == y1): 
     drawPoint(pic, col, x0, y0) 
     break 

    if (e2 < dx): 
     err = err + dx 
     y0 = y0 + sy 


# Works only with squared cropped areas : 
# i.e. in [(x0, y0), (x1, y1)], abs(x1-x0) must be equal to abs(y1-y0) 
# 
# USAGE : 
# * To get bottom reflected to top use x0 > x1 
# * To get top reflected to bottom use x0 < x1 

def diagCropAndMirrorPicture(pic, startPt, endPt): 
    w = getWidth(pic) 
    h = getHeight(pic) 

    if (startPt[0] < 0) or (startPt[0] >= w) or \ 
     (startPt[1] < 0) or (startPt[1] >= h) or \ 
     (endPt[0] < 0) or (endPt[0] >= w) or \ 
     (endPt[1] < 0) or (endPt[1] >= h): 
      printNow("Error: The input points must be in the image range !") 
      return None 

    new_w = abs(startPt[0] - endPt[0]) 
    new_h = abs(startPt[1] - endPt[1]) 

    if (new_w != new_h): 
      printNow("Error: The input points do not form a square !") 
      return None 

    printNow("Given: (" + str(startPt[0]) + ", " + str(endPt[0]) + ") and (" \ 
         + str(startPt[1]) + ", " + str(endPt[1]) + ")") 

    newPicture = makeEmptyPicture(new_w, new_h) 

    if (startPt[0] < endPt[0]): 
     offsetX = startPt[0] 
     switchX = False 
     switchTB = True 
    else: 
     offsetX = endPt[0] 
     switchX = True 
     switchTB = False 

    if (startPt[1] < endPt[1]): 
     offsetY = startPt[1] 
     switchY = False 
    else: 
     offsetY = endPt[1] 
     switchY = True 

    # (switchX XOR switchY) 
    changeDiag = (switchX != switchY) 

    mirror_pt = 0 
    for x in range(0, new_w, 1): 

     for y in range(mirror_pt, new_h, 1): 
     #for y in range(0, new_h, 1): 

      oldX = x 
      oldY = y 


      if (switchTB): 
       sourcePixel = getPixel(picture, offsetX+new_w-1- oldX, offsetY+new_h-1- oldY) 
      else: 
       sourcePixel = getPixel(picture, offsetX+oldX, offsetY+oldY) 
      color = getColor(sourcePixel) 

      if (changeDiag): 
       newX = new_w-1 - x 
       newY = new_h-1 - y 
       #printNow("Change Diag !") 
      else: 
       newX = x 
       newY = y 

      # Copied half 
      if (switchTB): 
       targetPixel = getPixel(newPicture, new_w-1- x, new_h-1- y) 
      else: 
       targetPixel = getPixel(newPicture, x, y) 
      setColor(targetPixel, color) 

      # Mirror half (simply invert x and y) 
      if (switchTB): 
       targetPixel = getPixel(newPicture, new_h-1- newY, new_w-1- newX) 
      else: 
       targetPixel = getPixel(newPicture, newY, newX) 
      setColor(targetPixel, color) 


     # Here we shift the mirror point 
     if (not changeDiag): 
      mirror_pt += 1 


    return newPicture 


file = pickAFile() 
pic = makePicture(file) 
picture = makePicture(file) 

# Draw working area 
drawLine(pic, white, 30, 60, 150, 180) 
drawLine(pic, white, 30, 180, 150, 60) 
drawLine(pic, black, 30, 60, 30, 180) 
drawLine(pic, black, 30, 60, 150, 60) 
drawLine(pic, black, 150, 60, 150, 180) 
drawLine(pic, black, 30, 180, 150, 180) 
show(pic) 
writePictureTo(pic, "D:\\pic.png") 

# Build cropped and mirrored areas 
pic1 = diagCropAndMirrorPicture(picture, (150, 60), (30, 180)) 
pic2 = diagCropAndMirrorPicture(picture, (30, 180), (150, 60)) 
pic3 = diagCropAndMirrorPicture(picture, (150, 180), (30, 60)) 
pic4 = diagCropAndMirrorPicture(picture, (30, 60), (150, 180)) 

# Show cropped and mirrored areas 
if (pic1): 
    writePictureTo(pic1, "D:\\pic1.png") 
    show(pic1) 

if (pic2): 
    writePictureTo(pic2, "D:\\pic2.png") 
    show(pic2) 

if (pic3): 
    writePictureTo(pic3, "D:\\pic3.png") 
    show(pic3) 

if (pic4): 
    writePictureTo(pic4, "D:\\pic4.png") 
    show(pic4) 
........ ....................... ....................... enter image description here .......................... ............................

....... enter image description here


..... ..... enter image description here .......... enter image description here .......... enter image description here .......



+1

당신 이봐! ;) 당신이 좋아하는 것을 기쁘게 생각합니다. 나는 당신에게 이메일을 보내기 위해 정확하게 새롭게 게시 된 답변 링크를 그룹화했다. 그러나 당신은 이미 그것을 발견했다 ... –

+1

좋아, 그럼 내가 너에게 불어로 쓸거야! –

+0

해야합니다. 정확히 "제곱 된"그림을 입력으로 사용 하시겠습니까? 당신이 얻은 오류는 무엇입니까? 내 대답에 제곱되지 않은 그림을 거부하는 수표를 추가했습니다. 건배. –