Image Perspective correction using Javascript and OpenCV
Hello everyone, in this article I am going to explain how we can crop an image with irregular rectangular window and correct perspective to get a regular rectangular image frame. Find the final result below :
How this cropping and perspective correction is done in this project, the below animation shows
It is important because, that is the first step. A quick summery of that post, I am taking an image in background, I am using d3 to create an svg element which is acting like an crop box, I have added some dragging function to adjust the corner, so that any image portion of any shape can be cropped.
After this step, I have setup basic UI git show the output side by side. I have added canvas and using opencv javascript library to paint the image portion into new canvas.
You will find the full code in below GitHub link so I am not adding much code in this article, will only go through some important snippets.
Getting all 4 corners of cropbox
Load img src into the canvas
Calculate the scale factor as canvas size might be different, so that we can calculate previous point positions according to canvas position.
Finally put the irregular cropped portion into the regular destination canvas which is rectangular
GitHub link for this project code : https://github.com/reactcodes/crop-perspective-correction-image
If you are facing any issues regarding this code kindly let me know by commenting below.
If you like the article Kindly follow my blog and subscribe to get daily updates, it will encourage me a lot. Thanks for visiting... :)
Well now lets discuss about the steps I followed to implement this.
I hope you have gone through my crop window making over an image post, if you have't please visit
https://reactcodes.blogspot.com/2019/11/creating-irregular-crop-window-on-image.html
It is important because, that is the first step. A quick summery of that post, I am taking an image in background, I am using d3 to create an svg element which is acting like an crop box, I have added some dragging function to adjust the corner, so that any image portion of any shape can be cropped.
After this step, I have setup basic UI git show the output side by side. I have added canvas and using opencv javascript library to paint the image portion into new canvas.
You will find the full code in below GitHub link so I am not adding much code in this article, will only go through some important snippets.
Getting all 4 corners of cropbox
const children = document.querySelectorAll('#window_g .handle')
Load img src into the canvas
utils.loadImageToCanvas(imageUsed, 'imageInit')
const scaleFactor = parseInt(imageWidth / svgCropWidth)
Finally put the irregular cropped portion into the regular destination canvas which is rectangular
let dst = new cv.Mat(); let dsize = new cv.Size(imageHeight, imageWidth); let srcTri = cv.matFromArray(4, 1, cv.CV_32FC2, pointsArray); let dstTri = cv.matFromArray(4, 1, cv.CV_32FC2, [0, 0, imageHeight, 0, imageHeight, imageWidth, 0, imageWidth]); let M = cv.getPerspectiveTransform(srcTri, dstTri); cv.warpPerspective(src, dst, M, dsize, cv.INTER_LINEAR, cv.BORDER_CONSTANT, new cv.Scalar()); document.getElementById('imageInit').style.display = "none" cv.imshow('imageResult', dst);
If you are facing any issues regarding this code kindly let me know by commenting below.
If you like the article Kindly follow my blog and subscribe to get daily updates, it will encourage me a lot. Thanks for visiting... :)


Hi, Will this work for video as well?
ReplyDeleteI've downloaded the project source from github and opened index.html in my browser. But on cropping i don't see cropped out(which is shown in the above gif). I dont see any difference in output.
ReplyDeleteTry to run it on a server such as lite-server
DeleteHi i saw your github project i see, after apply click the image remain same nothing come like your photo here,, i mean the right side image
ReplyDeleteI tried to run it on a server and the project worked well, give it a try! without server the issue is CORS issue which is generated by browser
DeleteI am trying to make your code work in my machine. I certainly dont understand how you manage to make the crop, I see you add +1200 to width of canvas but you substract -1200 to height ???? Why is that for??
ReplyDeleteAnd when you create the image in the imageInit you drawImage putting width to height and height to width ? Can you explain a little bit more?