Traceback TypeError: Expected Ptr<cv::UMat> for argument 'img'. when try to delete columns and draw lines

Question:

I want to delete some columns in my original image, and then draw a line (or rectangle or….). If I first draw the line and then drop the columns:

img = cv2.imread('image.jpg',0)

cv2.line(img,pt1=(0,0),pt2=(711,711),color=(102, 255, 255),thickness=10)
img= np.delete(img,list(range(400,500)),1)

plt.imshow(img,"gray")

there is no problem. But when I first drop the columns and then draw a line (or rectangle or whatever..)

img = cv2.imread('image.jpg',0)


img= np.delete(img,list(range(400,500)),1)
cv2.line(img,pt1=(0,0),pt2=(711,711),color=(102, 255, 255),thickness=5)
plt.imshow(img,"gray")

Funny thing, this doesn’t seem to be a problem with rows, and even making a copy the problem persist. Is not a problem of dimension and also try the ugly solution transposing

img= np.delete(img.T, list(range(400,500)),0),T
cv2.line(img,pt1=(0,0),pt2=(711,711),color=(102, 255, 255),thickness=5)
plt.imshow(img,"gray")

but again doesn’t work.

Asked By: user48969

||

Answers:

It is really strange situation.

print(type(img)) before and after np.delete() shows <class 'numpy.ndarray'> so I don’t know what makes problem.

But if I use .copy() then it works correctly.

img = np.delete(img, list(range(400,500)), 1).copy()
#img = np.delete(img.T, list(range(400,500)), 0).T.copy()
cv2.line(img, pt1=(0,0), pt2=(711,711), color=(102, 255, 255), thickness=10)

Maybe np.delete() is some "lazy" function which doesn’t create new numpy directly but when it is needed but it doesn’t do this when it is needed in cv2.line()

Maybe you should send this issue to authors of numpy or cv2

Answered By: furas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.