Imagemagick: 如何从图像中去除网格线和暗区域?

huangapple go评论95阅读模式
英文:

Imagemagick: How to remove grid lines and dark regions from an image?

问题

ImageMagick有一个-hough-lines操作符,可以检测直线。如何使用它来移除这些直线?

英文:

I have a microscopic image with grid of straight lines (not vertical or horizontal).

Imagemagick: 如何从图像中去除网格线和暗区域?

I know that ImageMagick have -hough-lines operator that can detect straight lines. How to use it to remove such lines?

答案1

得分: 3

以下是处理图像的一种Imagemagick方法。

(请注意,-hough-lines只准确到1度。因此,它可以找到线条,但它们不会对齐得足够好以用于去除。)

有两个主要步骤。首先找到旋转角度。然后旋转图像,然后使用形态学操作来移除水平线和垂直线。

输入:

Imagemagick: 如何从图像中去除网格线和暗区域?

第一步执行除法归一化以均匀背景白色,然后执行自动阈值,然后执行-deskew以获取旋转角度。我包括+write xxx.png以保存逐步图像。您可以删除这些+write xxx.png,但不能删除-write mpr:xxx。

angle=`magick gridded_ellipse.jpg -write mpr:img \
\( mpr:img -blur 0x99 \) +swap -compose divide -composite +write gridded_ellipse_div_normalize.png \
-auto-threshold triangle +write gridded_ellipse_threshold.png -deskew 40% +write gridded_ellipse_thresh_deskew.png -format "%[deskew:angle]" info:`

除法归一化:

Imagemagick: 如何从图像中去除网格线和暗区域?

除法归一化和阈值化:

Imagemagick: 如何从图像中去除网格线和暗区域?

除法归一化、阈值化和去斜:

Imagemagick: 如何从图像中去除网格线和暗区域?

第二步从先前的除法归一化图像开始旋转它。然后,它获取先前的去斜图像并应用水平形态学闭合,将其取反以使线条变成黑底白线,并将其添加到旋转后的图像上。然后它对去斜图像应用垂直形态学闭合,将其取反以使线条变成黑底白线,并将其添加到水平处理的先前结果上。这形成了最终结果。

magick gridded_ellipse_div_normalize.png -rotate $angle +write gridded_ellipse_norm_rot.png +write mpr:rot +delete \
\( gridded_ellipse_thresh_deskew.png +write mpr:deskew -morphology close rectangle:50x1 -negate +write gridded_ellipse_thresh_morph_horiz.png  \
mpr:rot -compose plus -composite +write gridded_ellipse_no_horiz.png -write mpr:no_horiz \) \
\( mpr:deskew -morphology close rectangle:1x50 -negate +write gridded_ellipse_thresh_morph_vert.png  \
mpr:no_horiz -compose plus -composite +write gridded_ellipse_result.png \) \
null:

除法归一化和旋转输入:

Imagemagick: 如何从图像中去除网格线和暗区域?

除法归一化、旋转和阈值化输入:

Imagemagick: 如何从图像中去除网格线和暗区域?

形态学闭合水平:

Imagemagick: 如何从图像中去除网格线和暗区域?

形态学闭合水平添加到旋转后的图像:

Imagemagick: 如何从图像中去除网格线和暗区域?

形态学闭合垂直:

Imagemagick: 如何从图像中去除网格线和暗区域?

形态学闭合垂直添加到先前的结果 - 最终图像:

Imagemagick: 如何从图像中去除网格线和暗区域?

英文:

Here is one way to process your image in Imagemagick.

(Note that -hough-lines is only accurate to 1 degree. So it can find the lines, but they will not be aligned well enough to use them for removal.)

There are two major steps. First find the angle of rotation. Second rotate the image and then use morphology to remove the horizontal and then the vertical lines.

Input:

Imagemagick: 如何从图像中去除网格线和暗区域?

First step does a division normalization to even out the background white, then does an automatic threshold, then does a -deskew to get the rotation angle. I have included +write xxx.png to save the images step-by-step. You can remove these +write xxx.png, but not the -write mpr:xxx.

angle=`magick gridded_ellipse.jpg -write mpr:img \
\( mpr:img -blur 0x99 \) +swap -compose divide -composite +write gridded_ellipse_div_normalize.png \
-auto-threshold triangle +write gridded_ellipse_threshold.png -deskew 40% +write gridded_ellipse_thresh_deskew.png -format "%[deskew:angle]" info:`

Division Normalized:

Imagemagick: 如何从图像中去除网格线和暗区域?

Division Normalized and Thresholded:

Imagemagick: 如何从图像中去除网格线和暗区域?

Division Normalized, Thresholded and Deskewed:

Imagemagick: 如何从图像中去除网格线和暗区域?

Second Step starts with the previous division normalized image and rotates it. Then it takes the previous deskewed image and apply a horizontal morphology close, negates it to make the lines white on black and adds that to the rotated image. Then it applies a vertical morphology close to the deskewed image, negates it to make the lines white on black and adds that to the previous result from the horizontal processing. This forms the final result.

magick gridded_ellipse_div_normalize.png -rotate $angle +write gridded_ellipse_norm_rot.png +write mpr:rot +delete \
\( gridded_ellipse_thresh_deskew.png +write mpr:deskew -morphology close rectangle:50x1 -negate +write gridded_ellipse_thresh_morph_horiz.png  \
mpr:rot -compose plus -composite +write gridded_ellipse_no_horiz.png -write mpr:no_horiz \) \
\( mpr:deskew -morphology close rectangle:1x50 -negate +write gridded_ellipse_thresh_morph_vert.png  \
mpr:no_horiz -compose plus -composite +write gridded_ellipse_result.png \) \
null:

Division Normalized and Rotated Input:

Imagemagick: 如何从图像中去除网格线和暗区域?

Division Normalized, Rotated and Thresholded Input:

Imagemagick: 如何从图像中去除网格线和暗区域?

Morphology Close Horizontal:

Imagemagick: 如何从图像中去除网格线和暗区域?

Morphology Close Horizontal Added To Rotated Image:

Imagemagick: 如何从图像中去除网格线和暗区域?

Morphology Close Vertical:

Imagemagick: 如何从图像中去除网格线和暗区域?

Morphology Close Vertical Added To Previous Result - Final Image:

Imagemagick: 如何从图像中去除网格线和暗区域?

huangapple
  • 本文由 发表于 2023年3月31日 21:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定