Page 1 of 2

How to unwarp an image

Posted: 2013-12-04T14:51:31-07:00
by impoweruser
Using a combination of pixel editing in my development enviornment + the use IM convert warp distortion, does anyone have any tips or ideas about how to unwarp a randomly warped image?

here are 3 examples (hand drawn sorry, but you get the idea), I am looking to have the line straight on horizontal axis:

http://postimg.org/image/4dc6p2tmn/

Fred? are you out there?

Re: How to unwarp an image

Posted: 2013-12-04T15:29:05-07:00
by fmw42
It likely cannot be done without knowing the distortion and then may not be possible if IM does not understand that distortion, though if you know the inverse distortion you could program it with -fx.

Are you talking about one curved line per image that is black on white background or are you wanting something that unwarps a proper color picture?

Re: How to unwarp an image

Posted: 2013-12-04T15:57:51-07:00
by snibgo
Some possibilities exist.

1. Given a roughly horizontal squigly line, create a vertical-only displacement map that would make the line straight.

2. Given a number of roughly horizontal squigly lines, create a vertical-only displacement map that would make all the lines straight. This is more complex.

3. Something else.

(1) is fairly easy. Make the area beneath the black line black. Resize to a single line, then back up to full size. That gives a greyscale relative displacement map, although it is from-to and needs to be to-from.

Re: How to unwarp an image

Posted: 2013-12-04T16:05:49-07:00
by fmw42
here are 3 examples (hand drawn sorry, but you get the idea), I am looking to have the line straight on horizontal axis:
No I do not really get the idea. That is why I asked if only one squiggly line or the whole image. What do you mean by randomly distorted? If you are talking about random vertical displacements of every column of the image, that may be solvable by the method outlined by snibgo. But if every pixel is randomly displaced in arbitrary directions, then I do not see how you can do that. Even for a more simple equation type warp, you would probably need to know what the equations was.

Can you give us a better or real example?

Re: How to unwarp an image

Posted: 2013-12-04T19:21:01-07:00
by impoweruser
sorry if its not clear, i'll be working on a single squgly line that can have a random offset. In other words it can start high and end low or vice versa. It could also be high then low then high (like a warp). Ive tried warping in the opposite direction, but what I cant ewrap my head around is how to determine the feature points (maybe im off base here) and how to proceed from there. Ill try to get some better examples uploaded.

Re: How to unwarp an image

Posted: 2013-12-04T19:45:47-07:00
by fmw42
If the line is pure black and the rest of the image is dark, you can get the coordinates of each black pixel using IM txt: format

convert image txt: | grep "black"

see http://www.imagemagick.org/Usage/files/#txt

You could then either fit a straight line to the points or just draw a straight line from the first to the last point.

Re: How to unwarp an image

Posted: 2013-12-04T22:05:31-07:00
by snibgo
This is the sort of thing I mean. The two convert commands could be easily combined into one. The errors are mostly (or entirely?) because the source line has, for given values of x, multiple values of y.

For non-users of Windows: the "FOR" command sets WW, HH and HHdiv2 to the source width, height and semi-height rounded to the nearest pixel.

If a photograph is taken of a squigly edge, it could be traced in a separate layer in Gimp. The second convert below would then straighten the edge.

Code: Select all

set SRC=clutLine2.png

FOR /F "usebackq" %%L IN (`%IM%identify -format "WW=%%w\nHH=%%h\nHHdiv2=%%[fx:int(h/2+0.5)]" %SRC%`) DO set /A %%L

%IM%convert ^
  %SRC% ^
  -verbose ^
  -morphology Distance "1x2+0+0:0,1" ^
  -write m1.png ^
  -resize "%WW%x1^!" -resize "%WW%x%HH%^!" ^
  -depth 16 ^
  c.png

%IM%convert ^
  %SRC% ^
  c.png ^
  -interpolate bicubic ^
  -background khaki -virtual-pixel background ^
  -compose displace ^
  -set option:compose:args 0x%HHdiv2% ^
  -composite ^
  strLine.png
clutLine2.png:
Image

strLine.png:
Image

Re: How to unwarp an image

Posted: 2013-12-05T03:03:12-07:00
by snibgo
A tip worth remembering: when shrinking to a single row to get average values for each column, "-filter box" gives a more accurate result than the default. Here is an improved version of my script, which also uses a single convert. It modifies the input line by reducing vertical black lines to a singe pixel. It creates an intermediate result c2.png that can be used as c.png in my previous post. If c2.png isn't required, remove "-depth 16 -write c2.png ^".

Code: Select all

set SRC=clutLine2.png

FOR /F "usebackq" %%L ^
IN (`%IM%identify -format "WW=%%w\nHH=%%h\nHHdiv2=%%[fx:int(h/2+0.5)]" %SRC%`) ^
DO set /A %%L

%IM%convert ^
  %SRC% ^
  -virtual-pixel white ^
  ( +clone -morphology Dilate rectangle:1x2 -negate ) ^
  -compose Lighten -composite ^
  -write mpr:DLBW ^
  -morphology Distance "1x2+0+0:0,1" ^
  -threshold 50%% ^
  -filter box -resize "%WW%x1^!" -resize "%WW%x%HH%^!" ^
  -depth 16 -write c2.png ^
  mpr:DLBW ^
  +swap ^
  -interpolate bicubic ^
  -background khaki -virtual-pixel background ^
  -compose displace ^
  -set option:compose:args 0x%HHdiv2% ^
  -composite ^
  strLine2.png
The border of the source image is visible as the boundary between white and khaki.
Image
The result looks perfect. I've solved the problem I was trying to solve. But this may not help the OP.

Re: How to unwarp an image

Posted: 2013-12-05T10:39:33-07:00
by impoweruser
Nice!, guys thanks so much for the replies and effort. My initial post indicated a 1 pixel line and this was because i was simply following the bottom edge of the image im trying to unwarp and I thought this might be the best approach. Ive attached two actual samples. Ill try what snibgo as recommened and see if it works with my scenario. At any rate, here are the two actual samples and again I sincerely appreciate your help with this:

http://postimg.org/image/wqbi7dqiz/

http://postimg.org/image/81fgygmgx/

Youll notice they are more of blobs than anything. But the goal is the same and that is to basically flatten the image by following the bottom edge (hope I am describing this correct)

Re: How to unwarp an image

Posted: 2013-12-05T11:13:00-07:00
by fmw42
what do you mean by flatten? Do you want it to end up one curve or one straight line or a rectangle? Please clarify

try this morphological skeletonizing, pruning and thinning:

Image
convert sample1.png -negate -morphology Thinning:-1 Skeleton -morphology Thinning:-1 'LineEnds:1>' -morphology thinning '3x1:0,1,0' -negate result1.png
Image

Is that what you want?

Re: How to unwarp an image

Posted: 2013-12-05T11:28:22-07:00
by impoweruser
fmw42 wrote:what do you mean by flatten? Do you want it to end up one curve or one straight line or a rectangle? Please clarify
I want the bottom of the image to be straight (flat/horizontally), regardless of the curves on the top of the image. So I guess straight line or perhaps even a rectangle would be the answer.

I tried the first example of snibgo, and it works remarkably well with a few excpetions:

it seems to flatten/collapse/make straight (whatever we want to call it) the TOP of the randomly curved blob image instead of the bottom and it also ended up cropping a portion of it ~ it only shows about half the height of the original image.

Another way to describe what I am trying to do would be to imagine the randomly curved blog image and then to have it all pulled down (displaced evenly) to the bottom of the image as if there were gravity pulling it down. (LOL, gosh reading that it sounds like Im crazy... ill try to mock up a before an after :)

Re: How to unwarp an image

Posted: 2013-12-05T11:34:09-07:00
by fmw42
If you shift the bottom down to be flat, what do you expect the top to look like? Should it be curved to preserve the height of the black?

Do you just want a rectangle of the same width but average height of the black?

Re: How to unwarp an image

Posted: 2013-12-05T12:00:51-07:00
by impoweruser
fmw42 wrote:If you shift the bottom down to be flat, what do you expect the top to look like? Should it be curved to preserve the height of the black?

Do you just want a rectangle of the same width but average height of the black?
It should preserve the height of the black. After second thought, a rectangle would not do this, so no rectangle and no averaging of heghts.

Im tinkering with sbingos solution but not having much luck :( Again the only pitfall of his is that it straightens the top, not the bottom and it seems to crop a portion of the image as well (since its centering the straight line in the exact middle of the image), but it really seems this is the closest ive gotten.

Re: How to unwarp an image

Posted: 2013-12-05T12:17:28-07:00
by fmw42
try the following. I negate the image, then get the height of the white on a column by column basis and use that height image to generate a new image that draws black from the top of a pure white image as much as the specified height, then flips the image over so that the flat is at the bottom. The value of 35 is the scale factor use to match the height of the original image.

Image

convert sample1.png \
\( -clone 0 -fill white -colorize 100 \) \
\( -clone 0 -negate -scale 96x1! \) \
-delete 0 -fx "j<35*v?black:white" -flip sample1_proc.png

Image

Re: How to unwarp an image

Posted: 2013-12-05T13:11:52-07:00
by fmw42
Actually this is simpler, the colorize step is not needed:


convert sample1.png \
\( -clone 0 -negate -scale 96x1! \) \
-fx "j<35*v?black:white" -flip result.png