Discussion:
[SciPy-user] scipy.signal.convolve2d() clips off part of my image
Mike Kreiner
2005-01-25 21:32:04 UTC
Permalink
I posted this message earlier to comp.lang.python, but figured this
would be a better place to go.

I'm using the convolve2d(image, mask) function from scipy to blur an
image. The image I'm using is 512 by 384. When I display the image that
convolve2d returns, the the left-most square of pixels (388 by 388)
turn out fine, blurred and everything, however the right side of the
image is all black.

I've uploaded the practice image to: http://tinypic.com/1g3iox
The output image is: http://tinypic.com/1g3iv9
import scipy
img = scipy.imread("c:\\practice.jpg",flatten=True)
img.shape
(384, 512)
mask =
(1.0/115)*scipy.array([[2,4,5,4,2],[4,9,12,9,4],[5,12,15,12,5],[4,9,12,9,4],[2,4,5,4,2]])
blurredImg = scipy.signal.convolve2d(img, mask)
scipy.imsave("c:\\blurred.jpg",blurredImg)
Also, I noticed that the shape attribute is (384, 512), even though
windows and my image editor say the image is 512 by 384. Could this
have something to do with the reason convolve2d() only works right on
the left-most 388 by 388 pixels? Thanks for any help.

-Mike Kreiner
Travis Oliphant
2005-01-28 20:50:57 UTC
Permalink
Post by Mike Kreiner
I posted this message earlier to comp.lang.python, but figured this
would be a better place to go.
I'm using the convolve2d(image, mask) function from scipy to blur an
image. The image I'm using is 512 by 384. When I display the image that
convolve2d returns, the the left-most square of pixels (388 by 388)
turn out fine, blurred and everything, however the right side of the
image is all black.
Could be a bug. I'll look into it. I'm rewriting the convolve2d
function right now, to be much faster.
Post by Mike Kreiner
I've uploaded the practice image to: http://tinypic.com/1g3iox
The output image is: http://tinypic.com/1g3iv9
import scipy
img = scipy.imread("c:\\practice.jpg",flatten=True)
img.shape
(384, 512)
mask =
(1.0/115)*scipy.array([[2,4,5,4,2],[4,9,12,9,4],[5,12,15,12,5],[4,9,12,9,4],[2,4,5,4,2]])
blurredImg = scipy.signal.convolve2d(img, mask)
scipy.imsave("c:\\blurred.jpg",blurredImg)
Also, I noticed that the shape attribute is (384, 512), even though
windows and my image editor say the image is 512 by 384. Could this
have something to do with the reason convolve2d() only works right on
the left-most 388 by 388 pixels? Thanks for any help.
No, the two are unrelated. Scipy's convention is to report the shape of
an array as (rows, columns). A lot of images are reported in terms of
width x height. But width is the number of columns, and height is the
number of rows.

So a 512 x 384 (width x height) image would have a shape of 384 rows and
512 columns --- img.shape = (384, 512)

Thanks for the report.

-Travis
Travis Oliphant
2005-01-28 21:56:23 UTC
Permalink
Post by Mike Kreiner
I posted this message earlier to comp.lang.python, but figured this
would be a better place to go.
I'm using the convolve2d(image, mask) function from scipy to blur an
image. The image I'm using is 512 by 384. When I display the image that
convolve2d returns, the the left-most square of pixels (388 by 388)
turn out fine, blurred and everything, however the right side of the
image is all black.
Mike,

I will look into what is going on, as it looks like the "full" linear
convolution may have a bug.
Try using the "same" flag (this gives an output that is the same size as
the input). This seems to work.

convolve2d(image, mask, mode="same")

-Travis
Travis Oliphant
2005-01-28 22:01:38 UTC
Permalink
Post by Mike Kreiner
I posted this message earlier to comp.lang.python, but figured this
would be a better place to go.
I'm using the convolve2d(image, mask) function from scipy to blur an
image. The image I'm using is 512 by 384. When I display the image that
convolve2d returns, the the left-most square of pixels (388 by 388)
turn out fine, blurred and everything, however the right side of the
image is all black.
A tiny bug in the code that you hit when using the "full" flag (which
returns the full linear convolution) with unequal dimensions was causing
this. This is fixed in CVS now. But, you can use the "same" flag
(which returns the same size output image as the input image -- and is
usually what you want anyway in situations like this) the problem
doesn't arise.

Perhaps, changing the default to "same" would be a good idea.

Thoughts?

-Travis

Loading...