如何使用Python (或Perl)来检测 "黑暗 "的图像边框并对其进行裁剪?

问题描述 投票:0回答:1

我有一些小图像,这些图像是视频的屏幕截图。 可以在以下网址收集一个示例图像 http:/www.filedropper.comtest-image .

enter image description here

简而言之,我试图将图像裁剪到最接近 "主 "图像的部分,也就是在一个(几乎)均匀的 "黑色 "边框内。 你可以把它看作是去图像的中心,然后辐射出去,直到你碰到一个 "矩形('黑')边界"。

在我看来,最大的问题是确定图像周围 "裁剪矩形 "的位置和尺寸......但到目前为止,我还无法做到这一点。

我试过使用 ffmpeg 中的 "cropdetect "滤镜,但在 Perl 中没有任何有用的东西;......而且由于我是 Python 的新手,我仍然不知道是否有任何简单的模块可以做我需要的事情。 I 看了 "scikit-image"......但完全被它迷惑了,因为我没有足够的Python知识,更不用说对图像格式、颜色深度、操作技术等有足够的 "技术 "知识来让我使用 "scikit-image "了。

我很感激任何关于如何解决这个问题的建议,如果有一个简单的方法就更好了。 根据我对'scikit-image'的一点了解,似乎'Canny edge detect'或'prewitt_v''prewitt_h'过滤器可能与此有关...?

我使用Python 3.7.0 (和Active State Perl v5.20.2,如果有办法使用的话),都在Windows 8.1下运行。

非常感谢大家的建议。

python crop scikit-image edge-detection
1个回答
0
投票

我已经尝试解决这个问题......但它不是很 "强大"。 当图像被灰度化时,它使用了像素的亮度值:-。

# ----
#  this will find the 'black'-ish portions and crop the image to these points
#  ozboomer, 25-Apr-2020 3-May-2020
#
# ---------

import pprint
import colorsys

from PIL import Image, ImageFilter

# ---- Define local functions (I *still* don't understand why I have to put these here)

def calculate_luminances(r, g, b):
   """Return luminance values of supplied RGB and greyscale of RGB"""

   lum = (0.2126 * r) + (0.7152 * g) + (0.0722 * b)    # luminance

   H, S, V = colorsys.rgb_to_hsv(r, g, b)              # HSV for the pixel RGB
   R, G, B = colorsys.hsv_to_rgb(H, 0, V)              # ...and greyscale RGB

   glum = (0.2126 * R) + (0.7152 * G) + (0.0722 * B)   # greyscale luminance

   return(lum, glum)

# end calculate_luminances

def radial_edge(radial_vector, ok_range):
   """Return the point in the radial where the luminance marks an 'edge'  """

   print("radial_edge: test range=", ok_range)

   edge_idx = -1
   i = 0

   for glum_value in radial_vector:
      print("  radial_vector: i=", i, "glum_value=", "%.2f" % round(glum_value, 2))

      if int(glum_value) in ok_range:
         print("  IN RANGE!  Return i=", i)
         edge_idx = i
         break

      i = i + 1      
   # endfor         

   return(edge_idx)

# ---- End local function definitions

# ---- Define some constants, variables, etc

#image_file = "cap.bmp"
#image_file = "cap2.png"
#image_file = "cap3.png"
image_file = "Sample.jpg"
#image_file = "cap4.jpg"
output_file = "Cropped.png";

edge_threshold = range(0, 70)  # luminance in this range = 'an edge'

#
# The image layout:-
#
# [0,0]----------+----------[W,0]
#   |            ^            |
#   |            |            |
#   |            R3           |
#   |            |            |
#   +<--- R1 ---[C]--- R2 --->+
#   |            |            |
#   |            R4           |
#   |            |            |
#   |            v            |
# [0,H]----------+----------[W,H]
#

# -------------------------------------
#  Main Routine
#

# ---- Get the image file ready for processing

try:
   im = Image.open(image_file)  # RGB.. mode

except:
   print("Unable to load image,", image_file)
   exit(1)

# Dammit, Perl, etc code is SO much less verbose:-
# open($fh, "<", $filename) || die("\nERROR: Can't open file, '$filename'\n$!\n");

print("Image - Format, size, mode: ", im.format, im.size, im.mode)

W, H = im.size         # The (width x height) of the image
XC = int(W / 2.0)      # Approx. centre of image
YC = int(H / 2.0)

print("Image Centre: (XC,YC)=", XC, ",", YC)

# --- Define the ordinate ranges for each radial

R1_range = range(XC, -1, -1)  # Actual range: XC->0 by -1 ... along YC ordinate
R2_range = range(XC,  W,  1)  #             : XC->W by +1 ... along YC ordinate
R3_range = range(YC, -1, -1)  #             : YC->0 by -1 ... along XC ordinate
R4_range = range(YC,  H,  1)  #             : YC->H by +1 ... along XC ordinate 

# ---- Check each radial for its 'edge' point

radial_luminance = []
for radial_num in range (1,5):  # We'll do the 4 midlines
   radial_luminance.clear()

   if radial_num == 1:
      print("Radial: R1")
      for x in R1_range:
         R, G, B = im.getpixel((x, YC))
         [lum, glum] = calculate_luminances(R, G, B)
         print("  CoOrd=(", x, ",", YC, ") RGB=", 
               (R, G, B), "lum=", "%.2f" % round(lum, 2), 
               "glum=", "%.2f" % round(glum, 2))         
         radial_luminance.append(glum)
      # end: get another radial pixel         

      left_margin = XC - radial_edge(radial_luminance, edge_threshold)

   elif radial_num == 2: 
      print("Radial: R2")
      for x in R2_range:
         R, G, B = im.getpixel((x, YC))
         [lum, glum] = calculate_luminances(R, G, B)
         print("  CoOrd=(", x, ",", YC, ") RGB=", 
               (R, G, B), "lum=", "%.2f" % round(lum, 2), 
               "glum=", "%.2f" % round(glum, 2))         
         radial_luminance.append(glum)  
      # end: get another radial pixel         

      right_margin = XC + radial_edge(radial_luminance, edge_threshold)

   elif radial_num == 3: 
      print("Radial: R3")
      for y in R3_range:
         R, G, B = im.getpixel((XC, y))
         [lum, glum] = calculate_luminances(R, G, B)
         print("  CoOrd=(", XC, ",", y, ") RGB=", 
               (R, G, B), "lum=", "%.2f" % round(lum, 2), 
               "glum=", "%.2f" % round(glum, 2))         
         radial_luminance.append(glum)  
      # end: get another radial pixel         

      top_margin = YC - radial_edge(radial_luminance, edge_threshold)

   elif radial_num == 4: 
      print("Radial: R4")
      for y in R4_range:
         R, G, B = im.getpixel((XC, y))
         [lum, glum] = calculate_luminances(R, G, B)
         print("  CoOrd=(", XC, ",", y, ") RGB=", 
               (R, G, B), "lum=", "%.2f" % round(lum, 2), 
               "glum=", "%.2f" % round(glum, 2))         
         radial_luminance.append(glum)  
      # end: get another radial pixel         

      bottom_margin = YC + radial_edge(radial_luminance, edge_threshold)

   # end: which radial we're processing      

im.close()
crop_items = (left_margin, top_margin, right_margin, bottom_margin)
print("crop_items:", crop_items)

# ---- Crop the original image and save it

im = Image.open(image_file)
im2 = im.crop(crop_items)      
im2.save(output_file, 'png')

exit(0)

# [eof]

我希望 radial_edge() 函数需要进行修改,以检查周围的像素来确定是否有真正的边缘......。因为当前的 ok_range 可能需要为每张图片确定,所以没有必要尝试使用这样的脚本来自动裁剪。

还在寻找一种稳健可靠的方法来解决这个问题......

© www.soinside.com 2019 - 2024. All rights reserved.