1
$\begingroup$

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]]]]]]
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Pinti Apr 19 '17 at 7:35
  • $\begingroup$ 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? $\endgroup$ – Vahagn Tumanyan Apr 19 '17 at 10:47
  • $\begingroup$ @Pinti Edited. Thank you. $\endgroup$ – Robert D Apr 19 '17 at 12:02
  • $\begingroup$ @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. $\endgroup$ – Robert D Apr 19 '17 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.