Ask Your Question

Trimap image use in OpenCV

asked 2017-01-21 03:18:29 -0500

RossFe92 gravatar image

Hello, I found The Oxford-IIIT Pet Dataset, which contains images of dogs and cats. For each image has a pixel level foreground-background segmentation (trimap) image. Searching the internet, I saw that trimap is an image with three colors (one for the background, one for the foreground and one for the not-classified region), but here the image is all black. It's a mistake or is correct so too? But above all I want to know if and how you can use it to get, given a normal image, a new image with the cat or dog on a black background. Thanks.

edit retag flag offensive close merge delete


"but here the image is all black." -- wait, you'll have to explain that.

is it a problem of importing / visualizing those images ?

could you add an example image, and the code you're using ?

berak gravatar imageberak ( 2017-01-21 03:28:43 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-01-21 03:36:29 -0500

berak gravatar image

updated 2017-01-21 04:21:05 -0500

assuming, those trimaps contain a single channel of [0,1,2] values, it's no wonder, that it looks "all black".

to visualize them, you could try to increase the contrast, e.g. like:

imshow("trimap", trimap*80);

to mask out the backgound, using those trimaps, you would build a mask, using the resp. label (let's assume, 0 for bg):

Mat mask = (trimap==0);
image.setTo(Scalar::all(0), mask); // paint black, where the mask is "on".
edit flag offensive delete link more


How then I can use it to get a picture with the cat or dog on a black background?

RossFe92 gravatar imageRossFe92 ( 2017-01-21 04:18:04 -0500 )edit

^^ please see edit

berak gravatar imageberak ( 2017-01-21 04:21:50 -0500 )edit

Thanks for your answer. I've tried but I've this error:

OpenCV Error: Assertion failed (mask.empty() || (mask.type() == CV_8U && size == mask.size)) in setTo, file /opt/OpenCV/opencv-3.0.0/modules/core/src/copy.cpp, line 473
terminate called after throwing an instance of 'cv::Exception'
  what():  /opt/OpenCV/opencv-3.0.0/modules/core/src/copy.cpp:473: error: (-215) mask.empty() || (mask.type() == CV_8U && size == mask.size) in function setTo
RossFe92 gravatar imageRossFe92 ( 2017-01-21 04:43:22 -0500 )edit

either your trimap is empty(), or it does not have the same size as image.

berak gravatar imageberak ( 2017-01-21 04:50:03 -0500 )edit

Unfortunately neither the image, the trimap and mask are empty and all three have the same size (600x400)...

RossFe92 gravatar imageRossFe92 ( 2017-01-21 04:56:58 -0500 )edit

trimaps/ Trimap annotations for every image in the dataset

    Pixel Annotations:

1: Foreground


3: Not classified

read readme in annotations

LBerger gravatar imageLBerger ( 2017-01-21 05:08:49 -0500 )edit

I had already read, how should I edit the berak's answer then?

RossFe92 gravatar imageRossFe92 ( 2017-01-21 05:16:31 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-01-21 03:18:29 -0500

Seen: 1,391 times

Last updated: Jan 21 '17