how to remove vertical and horizontal line in this image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
aspenlin
Posts: 4
Joined: 2019-06-04T14:15:20-07:00
Authentication code: 1152

how to remove vertical and horizontal line in this image

Post by aspenlin »

Suppose I have an image like this one:
https://www.dropbox.com/s/7e8xmjt5u04us ... 4.jpg?dl=0
How do I remove the vertical and horizontal lines in the image?

I found a useful answer here but don't know what adjustment to make to deal with my image:
https://stackoverflow.com/questions/539 ... m-an-image

Furthermore, is there any way to make this wavy image more straight? Is there a way to make the text in this image clearer? I might be asking for too much :) but it will be good to know if they are there.

The result of 'convert --version' for the imagemagick-7.0.8-42 I'm using on MacOS is:
Version: ImageMagick 6.9.10-43 Q16 x86_64

Thanks very much for your help!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to remove vertical and horizontal line in this image

Post by fmw42 »

I do not think what you want to do is going to be very easy or effective.
aspenlin
Posts: 4
Joined: 2019-06-04T14:15:20-07:00
Authentication code: 1152

Re: how to remove vertical and horizontal line in this image

Post by aspenlin »

Thanks for your reply.
May I ask why the lines here are hard to remove? Is it because they are skewed? The result in the link is so cool.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to remove vertical and horizontal line in this image

Post by fmw42 »

Yes, because they are curved. That link had perfectly horizontal and vertical lines.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to remove vertical and horizontal line in this image

Post by snibgo »

aspenlin wrote:Furthermore, is there any way to make this wavy image more straight?
The image has four long wavy horizontal lines. You can straighten any one of them by moving entire columns up or down, but that will make some lines worse.

Suppose we want to straighten both the top and bottom lines. The image can be shrunk vertically, with most shrinking where the lines are furthest apart. We can shrink vertically with a displacement map. The following assumes the midline of the image is already straight. Windows BAT syntax:

Code: Select all

%IMG7%magick ^
  labtest4.jpg ^
  +write mpr:ORIG ^
  +depth ^
  ( +clone -blur 0x5 ) ^
  -compose DivideSrc -composite ^
  +write d.png ^
  -contrast-stretch 10%%,80%% ^
  -threshold 50%% ^
  -crop 1760x+5+0 +repage ^
  -morphology Convolve Blur:0x10 ^
  +write c0.png ^
  -threshold 20%% ^
  +write c.png ^
  -fill Black ^
  -draw "color 0,0 floodfill" ^
  -draw "color 0,%%[fx:h-1] floodfill" ^
  -fill Blue ^
  -draw "color 0,0 floodfill" ^
  -draw "color 0,%%[fx:h-1] floodfill" ^
  -fill White +opaque Blue ^
  -fill Black -opaque Blue ^
  +write f.png ^
  -set option:MYSIZE %%[fx:w]x%%[fx:h] ^
  -trim +repage ^
  +write t.png ^
  -scale "x1^!" ^
  -define compose:clamp=off ^
  -evaluate Subtract %%[min] ^
  -scale "%%[MYSIZE]^!" ^
  ( +clone ^
    -sparse-color bilinear 0,0,Black,0,%%[fx:h-1],White ^
    -evaluate Subtract 50%% ^
    +write t2.png ^
  ) ^
  -compose Multiply -composite ^
  -evaluate Add 50%% ^
  -channel R -evaluate Set 50%% +channel ^
  +write g.png ^
  mpr:ORIG ^
  +swap ^
  -compose Displace -composite ^
  labtest_out.jpg
Image
For simplicity, I have trimmed the left and right edges. Some more work would correct that.

Each "+write X.png" is for debugging only, and can be removed.

The result makes the top and bottom lines parallel, though neither is exactly straight. More work could make them exactly straight.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to remove vertical and horizontal line in this image

Post by snibgo »

Here is a more accurate result that makes the top and bottom printed lines straight and horizontal, following the method explained in Straightening two lines.

First, we create an image that is black in the middle, and blue at the top line and above, and blue at the bottom line and below. The lines don't extend the full width, so we need to trim the left and right edges, then extend to the original size with "viewport". We also "DivideSrc" to compensate for uneven lighting.

Code: Select all

%IMG7%magick ^
  labtest4.jpg ^
  -set option:MYSIZE %%[fx:w]x%%[fx:h] ^
  -crop 1760x+5+0 +repage ^
  -set option:distort:viewport 1890x1410-5+0 ^
  -distort SRT 1,0 ^
  +repage ^
  ( +clone -blur 0x5 ) ^
  -compose DivideSrc -composite ^
  -contrast-stretch 10%%,80%% ^
  -threshold 50%% ^
  -morphology Convolve Blur:0x10 ^
  -threshold 20%% ^
  -fill Black ^
  -draw "color 0,0 floodfill" ^
  -draw "color 0,%%[fx:h-1] floodfill" ^
  -fill Blue ^
  -draw "color 0,0 floodfill" ^
  -draw "color 0,%%[fx:h-1] floodfill" ^
  -fill Black +opaque Blue ^
  -strip^
  lb4_bl1.png
Image

Now call two Windows BAT scripts to create a relative displacement map (see the web page for the scripts):

Code: Select all

call %PICTBAT%blue2cluts ^
  lb4_bl1.png lb4_blc_XX.miff

call %PICTBAT%interpClut ^
  lb4_blc_1.miff lb4_blc_2.miff ^
  %b2cHH% ^
  lb4_lt1.miff ^
  %b2cXTRATOP% %b2cXTRABOT%
Now displace with the map:

Code: Select all

%IMG7%magick ^
  labtest4.jpg ^
  lb4_lt1.miff ^
  -virtual-pixel None ^
  -compose Displace ^
    -set option:compose:args 0x100%% ^
    -composite ^
  lb4_d1.jpg
Image

The top and bottom printed lines are now straight and horizontal. The "vertical" lines are not vertical. A perspective transformation would make them so.
snibgo's IM pages: im.snibgo.com
aspenlin
Posts: 4
Joined: 2019-06-04T14:15:20-07:00
Authentication code: 1152

Re: how to remove vertical and horizontal line in this image

Post by aspenlin »

Oh wow, the result is impressive!
Can I ask what kind of perspective transformation will make the vertical line vertical? Sorry I'm new to image processing.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to remove vertical and horizontal line in this image

Post by snibgo »

I pick the two outer-most "vertical" lines, and find the coordinates of the ends with a GUI editor. The transformation pins the top ends, and slides the bottom ends inwards to make those lines vertical.

Insert this before saving the output:

Code: Select all

  -distort perspective ^
153,408,153,408,^
1674,408,1674,408,^
133,1278,153,1278,^
1702,1280,1674,1280 ^
Image
snibgo's IM pages: im.snibgo.com
Post Reply