การแบ่งภาพไบนารี่ออกเป็น 'บล็อก' ของข้อมูลพิกเซล

ฉันใช้ Python และ PIL เป็นส่วนหนึ่งของงานของฉันในการฝังข้อมูลในภาพไบนารี และจำเป็นต้องวิเคราะห์กลุ่มพิกเซลเพื่อกำหนดพิกเซลที่เหมาะสมเพื่อจัดการเพื่อฝังข้อมูล รูปภาพจะต้องแบ่งออกเป็น 'บล็อก' ของข้อมูลพิกเซลเท่า ๆ กันที่พร้อมสำหรับการวิเคราะห์ แต่ฉันกำลังดิ้นรนเพื่อหาวิธีการที่เหมาะสมในการทำเช่นนี้ ฉันได้ลองใช้เทคนิคโดยใช้อาร์เรย์ Python และ numPy แล้ว แต่ไม่ประสบความสำเร็จ ข้อเสนอแนะใด ๆ ที่จะได้รับการชื่นชมอย่างมาก.

ขอบคุณ


person BootStrap    schedule 19.04.2011    source แหล่งที่มา
comment
คุณมีภาพ RGB หรือระดับสีเทา/bw หรือไม่?   -  person Anatolij    schedule 19.04.2011
comment
รูปภาพเป็นแบบไบนารี่ เนื่องจากพิกเซลสีดำคือ '0' พิกเซลสีขาวคือ '1' ฉันคิดว่าเป็นระดับสีเทาประเภทหนึ่ง   -  person BootStrap    schedule 19.04.2011
comment
ใช่สิ่งเดียวกัน ขออภัย ความคิดเห็นของฉันค่อนข้างคลุมเครือ   -  person Skurmedel    schedule 19.04.2011


คำตอบ (2)


คุณต้องใช้ numpy array การแบ่งส่วนเพื่อให้ได้กลุ่มพิกเซล รูปภาพเป็นเพียงอาร์เรย์ 2 มิติ ดังนั้นคุณจึงสามารถใช้ arr = numpy.array(Image.open(filename)) และหลังจากนั้นจึงแบ่งส่วนได้

#this code block from my fractal dimension finder
step = 2**a
for j in range(2**l):
    for i in range(2**l):
        block = arr[j * step:(j + 1) * step, i * step:(i + 1) * step]
person Anatolij    schedule 19.04.2011

คุณสามารถใช้เทคนิคก้าวเล็กๆ น้อยๆ ที่รู้จักกันเพื่อสร้างมุมมองรูปภาพของคุณที่สร้างจากบล็อก มันเร็วมากและไม่ใช้หน่วยความจำเพิ่มเติมใด ๆ (ตัวอย่างคือรายละเอียดเล็กน้อย):

import numpy as np

#img = np.array(Image.open(filename), dtype='uint8')

w, h = 5, 4 # width, height of image
bw, bh = 2, 3 # width, height of blocks

img = np.random.randint(2, size=(h, w)) # create a random binary image

# build a blocky view of the image data
sz = img.itemsize # size in bytes of the elements in img
shape = (h-bh+1, w-bw+1, bh, bw) # the shape of the new array: two indices for the blocks,
                                 # two indices for the content of each block
strides = (w*sz, sz, w*sz, sz) # information about how to map indices to image data
blocks = np.lib.stride_tricks.as_strided(img, shape=shape, strides=strides)

# now we can access the blocks
print img
[[1 1 0 0 0]
 [0 1 1 0 0]
 [0 0 1 0 1]
 [1 0 1 0 0]]

print blocks[0,0]
[[1 1]
 [0 1]
 [0 0]]

print blocks[1,2]
[[1 0]
 [1 0]
 [1 0]]
person pberkes    schedule 20.04.2011
comment
คุณจะป้องกันไม่ให้บล็อกทับซ้อนกันได้อย่างไร? - person Cerin; 09.11.2011