How to undo or remove the drawn rectangle using Co

2019-08-22 15:35发布

问题:

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

回答1:

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


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

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


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


Do composite

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


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:

Navier-Stokes:

Telea: