Shear an image in python using Bx and By for forward and backward mapping












1















I have this problem i would like to have solved. I need to shear an image using forward mapping and then shear it back using backward mapping. The code works if I delete the backMapping but not with it added. Here is my code, any help is appreciated!



import cv2
import numpy as np

img = cv2. imread("Lena2.jpg")

rows, cols, c = img.shape

Bx = 0.2
By = 0.3

def forMap (img,Bx,By):
rows = img.shape[0]
cols = img.shape[1]
imgForward = np.ndarray(shape = (int(cols + rows*By), int(rows + cols*Bx),3))

for row in range(rows):
for col in range(cols):
np.matmul(imgForward,np.array([[rows],[cols]]))
imgForward[int (row+col*By), int(col+row*Bx)] = img[row,col]/255

return imgForward

def backMap (img, Bx, By):
n = int(1/(1-Bx*By))
rows = img.shape[0]
cols = img.shape[1]
imgBackwards = np.ndarray(shape = img.shape);

for row in range(rows):
for col in range(cols):
backCol = int (n*(col+row*Bx))
backRow = int (n*(col+row*By))
np.matmul(imgBackwards,np.array([[rows],[cols]]))
imgBackwards[int(backRow+backCol*By), int(backCol + backRow*Bx)] = img[row,col]

forMap(img, Bx, By)
BackMapping = (backMap(img, Bx, By))

cv2.imshow("original image", img)
cv2.imshow("Forward Mapping", forMap)
cv2.imshow("Backward mapping", backMap)
cv2.waitKey(0)









share|improve this question























  • “It doesn’t work” is not a question. Please describe the problem. Do you get an error, wrong output, crash, ...?

    – Cris Luengo
    Nov 21 '18 at 16:59
















1















I have this problem i would like to have solved. I need to shear an image using forward mapping and then shear it back using backward mapping. The code works if I delete the backMapping but not with it added. Here is my code, any help is appreciated!



import cv2
import numpy as np

img = cv2. imread("Lena2.jpg")

rows, cols, c = img.shape

Bx = 0.2
By = 0.3

def forMap (img,Bx,By):
rows = img.shape[0]
cols = img.shape[1]
imgForward = np.ndarray(shape = (int(cols + rows*By), int(rows + cols*Bx),3))

for row in range(rows):
for col in range(cols):
np.matmul(imgForward,np.array([[rows],[cols]]))
imgForward[int (row+col*By), int(col+row*Bx)] = img[row,col]/255

return imgForward

def backMap (img, Bx, By):
n = int(1/(1-Bx*By))
rows = img.shape[0]
cols = img.shape[1]
imgBackwards = np.ndarray(shape = img.shape);

for row in range(rows):
for col in range(cols):
backCol = int (n*(col+row*Bx))
backRow = int (n*(col+row*By))
np.matmul(imgBackwards,np.array([[rows],[cols]]))
imgBackwards[int(backRow+backCol*By), int(backCol + backRow*Bx)] = img[row,col]

forMap(img, Bx, By)
BackMapping = (backMap(img, Bx, By))

cv2.imshow("original image", img)
cv2.imshow("Forward Mapping", forMap)
cv2.imshow("Backward mapping", backMap)
cv2.waitKey(0)









share|improve this question























  • “It doesn’t work” is not a question. Please describe the problem. Do you get an error, wrong output, crash, ...?

    – Cris Luengo
    Nov 21 '18 at 16:59














1












1








1








I have this problem i would like to have solved. I need to shear an image using forward mapping and then shear it back using backward mapping. The code works if I delete the backMapping but not with it added. Here is my code, any help is appreciated!



import cv2
import numpy as np

img = cv2. imread("Lena2.jpg")

rows, cols, c = img.shape

Bx = 0.2
By = 0.3

def forMap (img,Bx,By):
rows = img.shape[0]
cols = img.shape[1]
imgForward = np.ndarray(shape = (int(cols + rows*By), int(rows + cols*Bx),3))

for row in range(rows):
for col in range(cols):
np.matmul(imgForward,np.array([[rows],[cols]]))
imgForward[int (row+col*By), int(col+row*Bx)] = img[row,col]/255

return imgForward

def backMap (img, Bx, By):
n = int(1/(1-Bx*By))
rows = img.shape[0]
cols = img.shape[1]
imgBackwards = np.ndarray(shape = img.shape);

for row in range(rows):
for col in range(cols):
backCol = int (n*(col+row*Bx))
backRow = int (n*(col+row*By))
np.matmul(imgBackwards,np.array([[rows],[cols]]))
imgBackwards[int(backRow+backCol*By), int(backCol + backRow*Bx)] = img[row,col]

forMap(img, Bx, By)
BackMapping = (backMap(img, Bx, By))

cv2.imshow("original image", img)
cv2.imshow("Forward Mapping", forMap)
cv2.imshow("Backward mapping", backMap)
cv2.waitKey(0)









share|improve this question














I have this problem i would like to have solved. I need to shear an image using forward mapping and then shear it back using backward mapping. The code works if I delete the backMapping but not with it added. Here is my code, any help is appreciated!



import cv2
import numpy as np

img = cv2. imread("Lena2.jpg")

rows, cols, c = img.shape

Bx = 0.2
By = 0.3

def forMap (img,Bx,By):
rows = img.shape[0]
cols = img.shape[1]
imgForward = np.ndarray(shape = (int(cols + rows*By), int(rows + cols*Bx),3))

for row in range(rows):
for col in range(cols):
np.matmul(imgForward,np.array([[rows],[cols]]))
imgForward[int (row+col*By), int(col+row*Bx)] = img[row,col]/255

return imgForward

def backMap (img, Bx, By):
n = int(1/(1-Bx*By))
rows = img.shape[0]
cols = img.shape[1]
imgBackwards = np.ndarray(shape = img.shape);

for row in range(rows):
for col in range(cols):
backCol = int (n*(col+row*Bx))
backRow = int (n*(col+row*By))
np.matmul(imgBackwards,np.array([[rows],[cols]]))
imgBackwards[int(backRow+backCol*By), int(backCol + backRow*Bx)] = img[row,col]

forMap(img, Bx, By)
BackMapping = (backMap(img, Bx, By))

cv2.imshow("original image", img)
cv2.imshow("Forward Mapping", forMap)
cv2.imshow("Backward mapping", backMap)
cv2.waitKey(0)






python image-processing pycharm mapping shearsort






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 14:52









Gustav JakobsenGustav Jakobsen

82




82













  • “It doesn’t work” is not a question. Please describe the problem. Do you get an error, wrong output, crash, ...?

    – Cris Luengo
    Nov 21 '18 at 16:59



















  • “It doesn’t work” is not a question. Please describe the problem. Do you get an error, wrong output, crash, ...?

    – Cris Luengo
    Nov 21 '18 at 16:59

















“It doesn’t work” is not a question. Please describe the problem. Do you get an error, wrong output, crash, ...?

– Cris Luengo
Nov 21 '18 at 16:59





“It doesn’t work” is not a question. Please describe the problem. Do you get an error, wrong output, crash, ...?

– Cris Luengo
Nov 21 '18 at 16:59












1 Answer
1






active

oldest

votes


















0














Forward Mapping:



order of shape should be (num of rows, num of cols, channnels), so it becomes
imgForward = np.ndarray(shape=(int(rows + cols*Bx),int(cols + rows*By),3))



No need of this line np.matmul(imgForward,np.array([[rows],[cols]]))



Then, you have to copy all the 3 channels at new position



imgForward[int(row+col*Bx), int(col+row*By),:] = img[row,col,:]


Backward Mapping



Only you need to change int(row+col*Bx), int(col+row*By) with int(row-col*Bx), int(col-row*By)



So your code becomes



import cv2
import numpy as np

img = cv2. imread('one.jpg')
rows, cols, c = img.shape

Bx = 0.2
By = 0.3

def forMap (img,Bx,By):
rows = img.shape[0]
cols = img.shape[1]
imgForward = np.zeros((int(rows + cols*Bx),int(cols + rows*By),3), dtype=np.ubyte)
for row in range(rows):
for col in range(cols):
#np.matmul(imgForward,np.array([[rows],[cols]]))
imgForward[int(row+col*Bx), int(col+row*By),:] = img[row,col,:]

return imgForward

def backMap (img, Bx, By):
rows = img.shape[0]
cols = img.shape[1]
imgBackwards = np.zeros(shape=img.shape, dtype=np.ubyte);

for row in range(rows):
for col in range(cols):
backCol = int (col-row*By)
backRow = int (row-col*Bx)
#np.matmul(imgBackwards,np.array([[rows],[cols],3]))
imgBackwards[backRow, backCol, :] = img[row,col,:]
return imgBackwards

fimg = forMap(img, Bx, By)
bimg = backMap(fimg, Bx, By)

cv2.imshow("original image", img)
cv2.imshow("Forward Mapping", fimg)
cv2.imshow("Backward mapping", bimg)
cv2.waitKey(0)
cv2.destroyAllWindows()





share|improve this answer
























  • You are a lifesaver @user8190410 !!!! That was exactly how it should work!

    – Gustav Jakobsen
    Nov 21 '18 at 18:23











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414696%2fshear-an-image-in-python-using-bx-and-by-for-forward-and-backward-mapping%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Forward Mapping:



order of shape should be (num of rows, num of cols, channnels), so it becomes
imgForward = np.ndarray(shape=(int(rows + cols*Bx),int(cols + rows*By),3))



No need of this line np.matmul(imgForward,np.array([[rows],[cols]]))



Then, you have to copy all the 3 channels at new position



imgForward[int(row+col*Bx), int(col+row*By),:] = img[row,col,:]


Backward Mapping



Only you need to change int(row+col*Bx), int(col+row*By) with int(row-col*Bx), int(col-row*By)



So your code becomes



import cv2
import numpy as np

img = cv2. imread('one.jpg')
rows, cols, c = img.shape

Bx = 0.2
By = 0.3

def forMap (img,Bx,By):
rows = img.shape[0]
cols = img.shape[1]
imgForward = np.zeros((int(rows + cols*Bx),int(cols + rows*By),3), dtype=np.ubyte)
for row in range(rows):
for col in range(cols):
#np.matmul(imgForward,np.array([[rows],[cols]]))
imgForward[int(row+col*Bx), int(col+row*By),:] = img[row,col,:]

return imgForward

def backMap (img, Bx, By):
rows = img.shape[0]
cols = img.shape[1]
imgBackwards = np.zeros(shape=img.shape, dtype=np.ubyte);

for row in range(rows):
for col in range(cols):
backCol = int (col-row*By)
backRow = int (row-col*Bx)
#np.matmul(imgBackwards,np.array([[rows],[cols],3]))
imgBackwards[backRow, backCol, :] = img[row,col,:]
return imgBackwards

fimg = forMap(img, Bx, By)
bimg = backMap(fimg, Bx, By)

cv2.imshow("original image", img)
cv2.imshow("Forward Mapping", fimg)
cv2.imshow("Backward mapping", bimg)
cv2.waitKey(0)
cv2.destroyAllWindows()





share|improve this answer
























  • You are a lifesaver @user8190410 !!!! That was exactly how it should work!

    – Gustav Jakobsen
    Nov 21 '18 at 18:23
















0














Forward Mapping:



order of shape should be (num of rows, num of cols, channnels), so it becomes
imgForward = np.ndarray(shape=(int(rows + cols*Bx),int(cols + rows*By),3))



No need of this line np.matmul(imgForward,np.array([[rows],[cols]]))



Then, you have to copy all the 3 channels at new position



imgForward[int(row+col*Bx), int(col+row*By),:] = img[row,col,:]


Backward Mapping



Only you need to change int(row+col*Bx), int(col+row*By) with int(row-col*Bx), int(col-row*By)



So your code becomes



import cv2
import numpy as np

img = cv2. imread('one.jpg')
rows, cols, c = img.shape

Bx = 0.2
By = 0.3

def forMap (img,Bx,By):
rows = img.shape[0]
cols = img.shape[1]
imgForward = np.zeros((int(rows + cols*Bx),int(cols + rows*By),3), dtype=np.ubyte)
for row in range(rows):
for col in range(cols):
#np.matmul(imgForward,np.array([[rows],[cols]]))
imgForward[int(row+col*Bx), int(col+row*By),:] = img[row,col,:]

return imgForward

def backMap (img, Bx, By):
rows = img.shape[0]
cols = img.shape[1]
imgBackwards = np.zeros(shape=img.shape, dtype=np.ubyte);

for row in range(rows):
for col in range(cols):
backCol = int (col-row*By)
backRow = int (row-col*Bx)
#np.matmul(imgBackwards,np.array([[rows],[cols],3]))
imgBackwards[backRow, backCol, :] = img[row,col,:]
return imgBackwards

fimg = forMap(img, Bx, By)
bimg = backMap(fimg, Bx, By)

cv2.imshow("original image", img)
cv2.imshow("Forward Mapping", fimg)
cv2.imshow("Backward mapping", bimg)
cv2.waitKey(0)
cv2.destroyAllWindows()





share|improve this answer
























  • You are a lifesaver @user8190410 !!!! That was exactly how it should work!

    – Gustav Jakobsen
    Nov 21 '18 at 18:23














0












0








0







Forward Mapping:



order of shape should be (num of rows, num of cols, channnels), so it becomes
imgForward = np.ndarray(shape=(int(rows + cols*Bx),int(cols + rows*By),3))



No need of this line np.matmul(imgForward,np.array([[rows],[cols]]))



Then, you have to copy all the 3 channels at new position



imgForward[int(row+col*Bx), int(col+row*By),:] = img[row,col,:]


Backward Mapping



Only you need to change int(row+col*Bx), int(col+row*By) with int(row-col*Bx), int(col-row*By)



So your code becomes



import cv2
import numpy as np

img = cv2. imread('one.jpg')
rows, cols, c = img.shape

Bx = 0.2
By = 0.3

def forMap (img,Bx,By):
rows = img.shape[0]
cols = img.shape[1]
imgForward = np.zeros((int(rows + cols*Bx),int(cols + rows*By),3), dtype=np.ubyte)
for row in range(rows):
for col in range(cols):
#np.matmul(imgForward,np.array([[rows],[cols]]))
imgForward[int(row+col*Bx), int(col+row*By),:] = img[row,col,:]

return imgForward

def backMap (img, Bx, By):
rows = img.shape[0]
cols = img.shape[1]
imgBackwards = np.zeros(shape=img.shape, dtype=np.ubyte);

for row in range(rows):
for col in range(cols):
backCol = int (col-row*By)
backRow = int (row-col*Bx)
#np.matmul(imgBackwards,np.array([[rows],[cols],3]))
imgBackwards[backRow, backCol, :] = img[row,col,:]
return imgBackwards

fimg = forMap(img, Bx, By)
bimg = backMap(fimg, Bx, By)

cv2.imshow("original image", img)
cv2.imshow("Forward Mapping", fimg)
cv2.imshow("Backward mapping", bimg)
cv2.waitKey(0)
cv2.destroyAllWindows()





share|improve this answer













Forward Mapping:



order of shape should be (num of rows, num of cols, channnels), so it becomes
imgForward = np.ndarray(shape=(int(rows + cols*Bx),int(cols + rows*By),3))



No need of this line np.matmul(imgForward,np.array([[rows],[cols]]))



Then, you have to copy all the 3 channels at new position



imgForward[int(row+col*Bx), int(col+row*By),:] = img[row,col,:]


Backward Mapping



Only you need to change int(row+col*Bx), int(col+row*By) with int(row-col*Bx), int(col-row*By)



So your code becomes



import cv2
import numpy as np

img = cv2. imread('one.jpg')
rows, cols, c = img.shape

Bx = 0.2
By = 0.3

def forMap (img,Bx,By):
rows = img.shape[0]
cols = img.shape[1]
imgForward = np.zeros((int(rows + cols*Bx),int(cols + rows*By),3), dtype=np.ubyte)
for row in range(rows):
for col in range(cols):
#np.matmul(imgForward,np.array([[rows],[cols]]))
imgForward[int(row+col*Bx), int(col+row*By),:] = img[row,col,:]

return imgForward

def backMap (img, Bx, By):
rows = img.shape[0]
cols = img.shape[1]
imgBackwards = np.zeros(shape=img.shape, dtype=np.ubyte);

for row in range(rows):
for col in range(cols):
backCol = int (col-row*By)
backRow = int (row-col*Bx)
#np.matmul(imgBackwards,np.array([[rows],[cols],3]))
imgBackwards[backRow, backCol, :] = img[row,col,:]
return imgBackwards

fimg = forMap(img, Bx, By)
bimg = backMap(fimg, Bx, By)

cv2.imshow("original image", img)
cv2.imshow("Forward Mapping", fimg)
cv2.imshow("Backward mapping", bimg)
cv2.waitKey(0)
cv2.destroyAllWindows()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 17:09









user8190410user8190410

548139




548139













  • You are a lifesaver @user8190410 !!!! That was exactly how it should work!

    – Gustav Jakobsen
    Nov 21 '18 at 18:23



















  • You are a lifesaver @user8190410 !!!! That was exactly how it should work!

    – Gustav Jakobsen
    Nov 21 '18 at 18:23

















You are a lifesaver @user8190410 !!!! That was exactly how it should work!

– Gustav Jakobsen
Nov 21 '18 at 18:23





You are a lifesaver @user8190410 !!!! That was exactly how it should work!

– Gustav Jakobsen
Nov 21 '18 at 18:23




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414696%2fshear-an-image-in-python-using-bx-and-by-for-forward-and-backward-mapping%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?