简体   繁体   English

固定块分割图像

[英]Segmenting Image In Fixed Blocks

Its not about cropping an image in OpenCV. 它与在OpenCV中裁剪图像无关。 I know how to do it, for example: Image[200:400, 100:300] # Crop from x, y, w, h -> 100, 200, 300, 400. What I am trying to do is create multiple segments of the Image, which do not exceed Image's Width/Height obviously. 我知道该怎么做,例如:Image [200:400,100:300]#从x,y,w,h-> 100、200、300、400裁剪。我想做的是创建多个细分图片的宽度,显然不超过图片的宽度/高度。

So precisely, if an Image is 720x640 resolution,and I need to split this image in multiple blocks, say it 100x100 Fixed blocks, so how to achieve this exactly in OpenCV using Python? 如此精确地讲,如果一个图像的分辨率为720x640,并且我需要将此图像拆分为多个块,例如100x100固定块,那么如何使用Python在OpenCV中完全实现此目的?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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