Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have used convert this to draw a rectangle on image now i want to undo this the how? help?

convert Image1.jpg -fill black -draw "rectangle 135,55 155,60" Image2.jpg
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
216 views
Welcome To Ask or Share your Answers For Others

1 Answer

Once you draw a rectangle onto an image, it has replace the pixels in the image with the color of rectangle. You cannot undo that. You can replace the color of the rectangle with some interpretation of the image pixels nearby using morphology techniques in Imagemagick by making a mask from the same rectangle and using that to blend the morphology processed image or a median filtered image with the one with a rectangle. But a better method is some inpainting tool. But Imagemagick does not have the latter. See OpenCV or Skimage for that.

Here is how to mitigate it in Imagemagick using morphology (or median filtering).

Create test image

convert lena.png -fill none -stroke black -strokewidth 1 -draw "translate 128,128 rectangle -50,-50 50,50" -alpha off lena_rect.png


enter image description here

Use morphology close (or one could just use -statistics median 5x5)

convert lena_rect.png -morphology close:3 diamond:1 lena_rect_close.png


enter image description here

Create rectangle for mask (a bit thicker than in image)

convert -size 256x256 xc:white -fill none -stroke black -strokewidth 2 -draw "translate 128,128 rectangle -50,-50 50,50" -alpha off -negate rect.png


enter image description here

Do composite

convert lena_rect.png lena_rect_open.png rect.png -compose over -composite result.png


enter image description here

ADDITION:

For comparison, here are 3 inpainting methods from opencv and skimage.

#!/opt/local/bin/python3.7

import cv2
import numpy as np
import skimage.io
import skimage.restoration
import skimage.exposure

# method choice: biharmonic, Navier-Stokes, Telea
method = 'biharmonic'
#method = 'Navier-Stokes'
#method = 'Telea'


if method == 'biharmonic':
    print('biharmonic')

    img = skimage.io.imread('/Users/fred/desktop/lena_rect.png')

    msk = skimage.io.imread('/Users/fred/desktop/rect.png')
    msk = skimage.exposure.rescale_intensity(msk, in_range='image', out_range=(0,1))

    newimg = skimage.restoration.inpaint_biharmonic(img, msk, multichannel=True)

    skimage.io.imsave('/Users/fred/desktop/lena_rect_inpaint_biharmonic.png', newimg)

elif method == 'Navier-Stokes':
    print('Navier-Stokes')

    img = cv2.imread('/Users/fred/desktop/lena_rect.png')

    msk = cv2.imread('/Users/fred/desktop/rect.png',0)

    newimg = cv2.inpaint(img, msk, 3, cv2.INPAINT_NS)

    cv2.imwrite('/Users/fred/desktop/lena_rect_inpaint_navier_stokes_15.png', newimg)

elif method == 'Telea':
    print('Telea')

    img = cv2.imread('/Users/fred/desktop/lena_rect.png')

    msk = cv2.imread('/Users/fred/desktop/rect.png',0)

    newimg = cv2.inpaint(img, msk, 3, cv2.INPAINT_TELEA)

    cv2.imwrite('/Users/fred/desktop/lena_rect_inpaint_telea_3.png', newimg)


biharmonic:

enter image description here

Navier-Stokes:

enter image description here

Telea:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...