Shiqing Shen
Shiqing Shen

Reputation: 85

Image weirdly changed after cv::resize()

The related code is here(C++, opencv):

Rect rec = boundingRect(...);
image_grey.copyTo(gesture_grey, mask);
imshow("image_grey", gesture_grey(rec));
resize(gesture_grey(rec), gesture_grey, Size(256, 256));
imshow("gesture_grey", gesture_grey);

The result of imshow(): enter image description here

Why are the two images so different before and after resizing? How to fix it?

Upvotes: 1

Views: 69

Answers (1)

zmechanic
zmechanic

Reputation: 1990

The problem is in copyTo method. It doesn't clear the content of 'gesture_grey' image container.

When you use gesture_grey(rec) you are effectively executing copy constructor, which gives you a new image container, so it's all cool. But when you call copyTo you are copying over to an existing target 'gesture_grey'.

To fix it, you need to re-initialize 'gesture_grey' in order to clear it, before calling copyTo. Otherwise, what you see is a combination of the previous content of 'gesture_grey' + new content you copy from image_grey.

Upvotes: 1

Related Questions