【问题标题】:Segmenting Image In Fixed Blocks在固定块中分割图像
【发布时间】:2015-01-03 17:53:29
【问题描述】:

这与在 OpenCV 中裁剪图像无关。我知道该怎么做,例如: Image[200:400, 100:300] # Crop from x, y, w, h -> 100, 200, 300, 400。我要做的是创建多个段图像的宽度/高度明显不超过图像的宽度/高度。

准确地说,如果一个图像是 720x640 分辨率,我需要将这个图像分割成多个块,比如 100x100 固定块,那么如何在 OpenCV 中使用 Python 准确地实现这一点?

【问题讨论】:

  • 任何代码?还是尝试?
  • @Kasra,我不知道该怎么做!
  • Scikit-image 非常适合这类东西。图像表示为一个 numpy 数组,可以很容易地切成更小的固定块。
  • 使用 2 个 Rect 元素。第一个是整个图像的大小,第二个是 0,0,大小为 100,100。然后循环 x 和 y 并将第二个 Rect 位置设置为以 Rect 宽度/高度为步长的位置。您可以使用 Rect 交集运算符来减小边界处的 Rect 大小(其中 100,100 会太大)。可以给你 C++ 代码,但不能给你 python。

标签: python opencv


【解决方案1】:
import cv2

def segmentize (image_path, segment_width=200, segment_height=50):
    # Croping Formula ==> y:h, x:w
    idx, x_axis, x_width,  = 1, 0, segment_width
    y_axis, y_height = 0, segment_height
    img = cv2.imread(image_path)
    height, width, dept = img.shape
    while y_axis <= height:
        while x_axis <= width:
            crop = img[y_axis:y_height, x_axis:x_width]
            x_axis=x_width
            x_width+=segment_width
            cropped_image_path = "crop/crop%d.png" % idx
            cv2.imwrite(cropped_image_path, crop)
            idx+=1
        y_axis += segment_height
        y_height += segment_height
        x_axis, x_width = 0, segment_width

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2014-02-01
    • 2022-10-17
    • 2020-02-10
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多