简体   繁体   中英

Is there a more efficient way to find the contour of this object and fill the blank spaces in python?

I am trying to improve this image with the help of morphological operations. The result I get is good but it takes almost 40 seconds to get the resulting image and I was wondering if there is any other method to get a similar or even better result without taking too long.

Below I attach the images and the code that i used to enhance the original image. Thanks

Original spine image Final spine image

import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.morphology import (square, rectangle, diamond, disk, erosion, dilation, opening, closing)

im = imread('/content/drive/MyDrive/Proc_imag/Spine.png')
imf = closing(im,disk(40))
imf = opening(imf,rectangle(75,1))
imf[975:,:] = 0

plt.imshow(im,cmap='gray')
plt.title('Original')
plt.show()

plt.imshow(imf,cmap='gray')
plt.title('Final')
plt.show() 

Almost all of your time is being taken up by the closing function. This is mostly because that function's runtime scales terribly with kernel size and disk(40) is probably an 80x80 kernel under the hood. We can approximate the same thing by downscaling the image and running an equivalent-sized kernel on the smaller image.

import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.morphology import (square, rectangle, diamond, disk, erosion, dilation, opening, closing)
from skimage.transform import rescale
import time
import numpy as np

# load image
start = time.time();
im = imread('spine.png')
imf = np.copy(im);
end = time.time();
print("Read Time: " + str(end - start));

# resize down
scale = 4.0;
start = time.time();
imf = rescale(imf, 1.0 / scale, order=0, anti_aliasing=False, multichannel=False);
end = time.time();
print("Scale Time: " + str(end - start));

# disk, fill in holes
start = time.time();
imf = closing(imf,disk(40 // scale))
end = time.time();
print("Disk Time: " + str(end - start));

# resize up
start = time.time();
imf = rescale(imf, scale, order=0, anti_aliasing=False, multichannel=False);
end = time.time();
print("Upscale Time: " + str(end - start));

# rect, remove thin horizontal lines
start = time.time();
imf = opening(imf,rectangle(75,1))
end = time.time();
print("Rect Time: " + str(end - start));

# ???
imf[975:,:] = 0

# show original image
plt.imshow(im,cmap='gray')
plt.title('Original')
plt.show()

# show processed image
plt.imshow(imf,cmap='gray')
plt.title('Final')
plt.show() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM