Skip to main content
Question Protected by Jamal
Tweeted twitter.com/StackCodeReview/status/746242224225136640
deleted 5 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Objective: Crop the image so only the number stays in the image

Problem: Slow Performance

I have code that crops an image. The image pixels are 0 or 255. There are no values between.

The background is 0 (black) The Letter and the letter/Numbernumber is between 0  (not-inclusive) - 255 (white).

This code is being used to crop a Mnist Digit of the Mnist Dataset.

white-on-black handwritten digits

Image Source: http://multithreaded.stitchfix.com/blog/2015/12/09/intro-to-chainer/Image Source

The code below does it. However, however, it does with 2 fors And and it takes a long time!

  How can I optimize it?

def crop_image(data_to_crop):
    cropped_data = []

    for z in xrange(data_to_crop.shape[0]):

        img = data_to_crop[z]
        img = img.reshape(data_to_crop.shape[0], data_to_crop.shape[1])

        rx = -1
        upy = -1

        lx = -1
        by = -1

        for x in xrange(data_to_crop.shape[0]):
            for y in xrange(data_to_crop.shape[1]):

                px = img[x, y]

                if px > 0:

                    if rx == -1 or x > rx:
                        rx = x

                    if lx == -1 or x < lx:
                        lx = x

                    if upy == -1 or y > upy:
                        upy = y

                    if by == -1 or y < by:
                        by = y

        img = img[lx:rx, by:upy]

        cropped_data.append(img)

    return cropped_data

Objective: Crop the image so only the number stays in the image

Problem: Slow Performance

I have code that crops an image. The image pixels are 0 or 255. There are no values between.

The background is 0 (black) The Letter/Number is between 0(not-inclusive) - 255 (white)

This code is being used to crop a Mnist Digit of the Mnist Dataset.

white-on-black handwritten digits

Image Source: http://multithreaded.stitchfix.com/blog/2015/12/09/intro-to-chainer/

The code below does it. However, it does with 2 fors And it takes a long time!

  How can I optimize it?

def crop_image(data_to_crop):
    cropped_data = []

    for z in xrange(data_to_crop.shape[0]):

        img = data_to_crop[z]
        img = img.reshape(data_to_crop.shape[0], data_to_crop.shape[1])

        rx = -1
        upy = -1

        lx = -1
        by = -1

        for x in xrange(data_to_crop.shape[0]):
            for y in xrange(data_to_crop.shape[1]):

                px = img[x, y]

                if px > 0:

                    if rx == -1 or x > rx:
                        rx = x

                    if lx == -1 or x < lx:
                        lx = x

                    if upy == -1 or y > upy:
                        upy = y

                    if by == -1 or y < by:
                        by = y

        img = img[lx:rx, by:upy]

        cropped_data.append(img)

    return cropped_data

Objective: Crop the image so only the number stays in the image

Problem: Slow Performance

I have code that crops an image. The image pixels are 0 or 255. There are no values between.

The background is 0 (black) and the letter/number is between 0  (not-inclusive) - 255 (white).

This code is being used to crop a Mnist Digit of the Mnist Dataset.

white-on-black handwritten digits

Image Source

The code does it, however, it does with 2 fors and it takes a long time! How can I optimize it?

def crop_image(data_to_crop):
    cropped_data = []

    for z in xrange(data_to_crop.shape[0]):

        img = data_to_crop[z]
        img = img.reshape(data_to_crop.shape[0], data_to_crop.shape[1])

        rx = -1
        upy = -1

        lx = -1
        by = -1

        for x in xrange(data_to_crop.shape[0]):
            for y in xrange(data_to_crop.shape[1]):

                px = img[x, y]

                if px > 0:

                    if rx == -1 or x > rx:
                        rx = x

                    if lx == -1 or x < lx:
                        lx = x

                    if upy == -1 or y > upy:
                        upy = y

                    if by == -1 or y < by:
                        by = y

        img = img[lx:rx, by:upy]

        cropped_data.append(img)

    return cropped_data
added 112 characters in body; edited tags; edited title; added 5 characters in body
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Crop Binary Image - Remove Black Border - Python - Numpyblack border of image using NumPy

Cropping Image with NumpyObjective: Crop the image so only the number stays in the image

Problem: Slow Performance

I have a code that Cropscrops an image. The Imageimage pixels isare 0 or 255. There isare no values between.

enter image description herewhite-on-black handwritten digits

Image Source :http://multithreaded.stitchfix.com/blog/2015/12/09/intro-to-chainer/ Image Source: http://multithreaded.stitchfix.com/blog/2015/12/09/intro-to-chainer/

The code below does it. However, it does with 2 "for's"fors And it takes a long time!

How can iI optimize it?

def crop_image(data_to_crop): cropped_data = []

def crop_image(data_to_crop):
    cropped_data = []

    for z in xrange(data_to_crop.shape[0]):

        img = data_to_crop[z]
        img = img.reshape(data_to_crop.shape[0], data_to_crop.shape[1])

        rx = -1
        upy = -1

        lx = -1
        by = -1

        for x in xrange(data_to_crop.shape[0]):
            for y in xrange(data_to_crop.shape[1]):

                px = img[x, y]

                if px > 0:

                    if rx == -1 or x > rx:
                        rx = x

                    if lx == -1 or x < lx:
                        lx = x

                    if upy == -1 or y > upy:
                        upy = y

                    if by == -1 or y < by:
                        by = y

        img = img[lx:rx, by:upy]

        cropped_data.append(img)

    return cropped_data

Problem : Slow Performance

Objective : Crop the Image so only the number stays in the image

Crop Binary Image - Remove Black Border - Python - Numpy

Cropping Image with Numpy

I have a code that Crops an image. The Image pixels is 0 or 255. There is no values between.

enter image description here

Image Source :http://multithreaded.stitchfix.com/blog/2015/12/09/intro-to-chainer/

The code below does it. However, it does with 2 "for's" And it takes a long time!

How can i optimize it?

def crop_image(data_to_crop): cropped_data = []

for z in xrange(data_to_crop.shape[0]):

    img = data_to_crop[z]
    img = img.reshape(data_to_crop.shape[0], data_to_crop.shape[1])

    rx = -1
    upy = -1

    lx = -1
    by = -1

    for x in xrange(data_to_crop.shape[0]):
        for y in xrange(data_to_crop.shape[1]):

            px = img[x, y]

            if px > 0:

                if rx == -1 or x > rx:
                    rx = x

                if lx == -1 or x < lx:
                    lx = x

                if upy == -1 or y > upy:
                    upy = y

                if by == -1 or y < by:
                    by = y

    img = img[lx:rx, by:upy]

    cropped_data.append(img)

return cropped_data

Problem : Slow Performance

Objective : Crop the Image so only the number stays in the image

Crop black border of image using NumPy

Objective: Crop the image so only the number stays in the image

Problem: Slow Performance

I have code that crops an image. The image pixels are 0 or 255. There are no values between.

white-on-black handwritten digits

Image Source: http://multithreaded.stitchfix.com/blog/2015/12/09/intro-to-chainer/

The code below does it. However, it does with 2 fors And it takes a long time!

How can I optimize it?

def crop_image(data_to_crop):
    cropped_data = []

    for z in xrange(data_to_crop.shape[0]):

        img = data_to_crop[z]
        img = img.reshape(data_to_crop.shape[0], data_to_crop.shape[1])

        rx = -1
        upy = -1

        lx = -1
        by = -1

        for x in xrange(data_to_crop.shape[0]):
            for y in xrange(data_to_crop.shape[1]):

                px = img[x, y]

                if px > 0:

                    if rx == -1 or x > rx:
                        rx = x

                    if lx == -1 or x < lx:
                        lx = x

                    if upy == -1 or y > upy:
                        upy = y

                    if by == -1 or y < by:
                        by = y

        img = img[lx:rx, by:upy]

        cropped_data.append(img)

    return cropped_data
Source Link
user108668
user108668

Crop Binary Image - Remove Black Border - Python - Numpy

Cropping Image with Numpy

I have a code that Crops an image. The Image pixels is 0 or 255. There is no values between.

The background is 0 (black) The Letter/Number is between 0(not-inclusive) - 255 (white)

This code is being used to crop a Mnist Digit of the Mnist Dataset.

enter image description here

Image Source :http://multithreaded.stitchfix.com/blog/2015/12/09/intro-to-chainer/

The code below does it. However, it does with 2 "for's" And it takes a long time!

How can i optimize it?

def crop_image(data_to_crop): cropped_data = []

for z in xrange(data_to_crop.shape[0]):

    img = data_to_crop[z]
    img = img.reshape(data_to_crop.shape[0], data_to_crop.shape[1])

    rx = -1
    upy = -1

    lx = -1
    by = -1

    for x in xrange(data_to_crop.shape[0]):
        for y in xrange(data_to_crop.shape[1]):

            px = img[x, y]

            if px > 0:

                if rx == -1 or x > rx:
                    rx = x

                if lx == -1 or x < lx:
                    lx = x

                if upy == -1 or y > upy:
                    upy = y

                if by == -1 or y < by:
                    by = y

    img = img[lx:rx, by:upy]

    cropped_data.append(img)

return cropped_data

Problem : Slow Performance

Objective : Crop the Image so only the number stays in the image