# Apply a four-point transform crop to an image

I am attempting to straighten and crop an image by using Edge Detection. The original image looks like this. By using edge detection and finding the max and min values for the points regarding the corners, I am able to get this.

However, I would like it to be perfectly straight, so that I can break it up into 10 (originally this was 20, but I realized 10 would suffice) perfect slices along the bars. Yes, I realize that I am currently only working with two points, but I would locate the coordinates of the other two points for the final version.

Is there a function that I can input all four corners into— have it crop the image according to those points, and have it transform the cropped image into a straightened rectangle of a given size?

Thank you very much in advance.

Here is the code I am currently working with:

i = Import["Images/1.JPG"]
corners = ImageCorners[i, 3, .01, 5]

minX = Min[corners[[;; , 1]]]
maxX = Max[corners[[;; , 1]]]
minY = Min[corners[[;; , 2]]]
maxY = Max[corners[[;; , 2]]]

z = ImageTrim[i, {{minX, minY}, {maxX, maxY}}]
imageData = ImagePartition[z, Scaled[{1/10, 1}]][[1, ;;]]

quantifyBar[x_] := Total[ImageMeasurements[x, "Mean"]]
classifyBar[x_] := If[x < 1.75, 1, 0]

For[x = 1, x < Length[imageData] + 1, x++,
Print[classifyBar[quantifyBar[imageData[[x]]]]]]

• Can you please edit your question to include the code you have already tried (shown on your linked image)? This will improve the quality of the question. – Pinti Apr 19 '17 at 7:35
• Have you looked at ImageTake? You can specify rows and columns that you want to take from the image. Also I don't understand what you mean by straightened rectangle. Do you want to detect the edges and cut along them? – Vahagn Tumanyan Apr 19 '17 at 10:47
• @Pinti Edited. Thank you. – Robert D Apr 19 '17 at 12:02
• @VahagnTumanyan I have yes, but Scaled's syntax looked more simple. My current attempt works the same, right? Sorry, after posting this question I ended up finding a better term for what I want— an affine transform on the photo, to create an orthogonal rectangle. – Robert D Apr 19 '17 at 12:06