1

I am trying to implement Seam carving algorithm wherein we have to delete a seam from the image. Image is stored as a numpy M X N array. I have found the seam, which is nothing but an array of M integers whose value specifies column values to be deleted.

Eg: a 2 X 3 array

import numpy 
img_array = numpy.array([[1, 2, 3],[4, 5, 6]])

and

seam = numpy.array([1,2]) 

This means that we have to delete from the Img 1st element from the 1st row (1), and second element from the second row (5). After deletion, Img will be

print img_array

[[2,3]
 [4,6]]

Work done:

I am new to python and I have found solutions which concern about single dimensional array or deleting an entire row or column. But I could not find a way to delete elements from specific columns.

Community
  • 1
  • 1
Sameer
  • 101
  • 2
  • 11

2 Answers2

2

Will you always delete one element from each row? If you try to delete one element from one row, but not another, you will end up with a ragged array. That is why there isn't a general purpose way of removing single elements from a 2d array.

One option is to figure out which ones you want to delete, remove them from a flattened array, and then reshape it back to the correct shape. Then it is your responsibility to ensure that the correct number of elements are removed.

All of these 'delete' methods actually copy the 'keep' values to a new array. Nothing actually deletes elements from the original array. So you could just as easily (and just as fast) do your own copy to a new array.

Another option is to work with lists of lists. Those are more tolerant of becoming ragged.

Here's an example of using a boolean mask to remove selected elements from an array (making a copy of course):

In [100]: x=np.arange(1,7).reshape(2,3)
In [101]: x
Out[101]: 
array([[1, 2, 3],
       [4, 5, 6]])
In [102]: mask=np.ones_like(x,bool)
In [103]: mask
Out[103]: 
array([[ True,  True,  True],
       [ True,  True,  True]], dtype=bool)
In [104]: mask[0,0]=False
In [105]: mask[1,1]=False
In [106]: mask
Out[106]: 
array([[False,  True,  True],
       [ True, False,  True]], dtype=bool)
In [107]: x[mask]
Out[107]: array([2, 3, 4, 6])  # it's flat
In [108]: x[mask].reshape(2,2)
Out[108]: 
array([[2, 3],
       [4, 6]])

Notice that even though both x and mask are 2d, the indexing result is flattened. Such a mask could easily have produced an array that couldn't be reshape back to 2d.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
1

Each row in your matrix is a single dimensional array.

import numpy
ary=numpy.array([[1,2,3],[4,5,6]])
print ary[0]

Gives

array([1, 2, 3])

You could iterate over your matrix, using the values from you seam to remove an element from the current row. Append the result to a modified matrix you are building.

seam = numpy.array([1,2])
for i in range(2):
  tmp = numpy.delete(ary[i],seam[i]-1)
  if i == 0:
    modified_ary = tmp
  else:
    modified_ary = numpy.vstack((modified_ary,tmp))

print modified_ary

Gives

[[2 3]
 [4 6]]
Joshua Cook
  • 12,495
  • 2
  • 35
  • 31