Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change origin of image coordinate system to bottom left instead of default top left

Is there a simple way of changing the origin of image co-ordinate system of OpenCV to bottom left? Using numpy for example? I am using OpenCv 2.4.12 and Python 2.7.

Related: Numpy flipped coordinate system, but this talks about just display. I want something which I can use consistently in my algorithm.

Update:

def imread(*args, **kwargs):
    img = plt.imread(*args, **kwargs)
    img = np.flipud(img)
    return img      
#read reference image using cv2.imread
imref=cv2.imread('D:\\users\\gayathri\\all\\new\\CoilA\\Resized_Results\\coilA_1.png',-1)
cv2.circle(imref, (0,0),30,(0,0,255),2,8,0)
cv2.imshow('imref',imref)

#read the same image using imread function
im=imread('D:\\users\\gayathri\\all\\new\\CoilA\\Resized_Results\\coilA_1.png',-1)
img= im.copy()
cv2.circle(img, (0,0),30,(0,0,255),2,8,0)
cv2.imshow('img',img)

Image read using cv2.imread: original image

Image flipped using imread function: flipped

As seen the circle is drawn at the origin on upper left corner in both original and flipped image. But the image looks flipped which I do not desire.

like image 617
gaya Avatar asked Apr 24 '17 09:04

gaya


1 Answers

Reverse the height (or column) pixels will get the result below.

import numpy as np
import cv2
import matplotlib.pyplot as plt
%matplotlib inline 

img = cv2.imread('./imagesStackoverflow/flip_body.png') # read as color image
flip = img[::-1,:,:] # revise height in (height, width, channel)

plt.imshow(img[:,:,::-1]), plt.title('original'), plt.show()
plt.imshow(flip[:,:,::-1]), plt.title('flip vertical'), plt.show()
plt.imshow(img[:,:,::-1]), plt.title('original with inverted y-axis'), plt.gca().invert_yaxis(), plt.show()
plt.imshow(flip[:,:,::-1]), plt.title('flip vertical with inverted y-axis'), plt.gca().invert_yaxis(), plt.show()

Output images:

enter image description here

Above included the one you intended to do?

like image 163
thewaywewere Avatar answered Nov 13 '22 00:11

thewaywewere