Ask Your Question
0

Python's Numpy.transpose by axes equivalent for cv::Mat?

asked 2016-08-29 16:06:31 -0600

zealotfb gravatar image

Hi there

I'm trying to translate the below Python code that uses numpy arrays into C++ and use cv::Mat instead of numpy:

def get_face_mask(im, landmarks): im = numpy.zeros(im.shape[:2], dtype=numpy.float64)

for group in OVERLAY_POINTS:
    draw_convex_hull(im,
                     landmarks[group],
                     color=1)

im = numpy.array([im, im, im]).transpose((1, 2, 0))

im = (cv2.GaussianBlur(im, (FEATHER_AMOUNT, FEATHER_AMOUNT), 0) > 0) * 1.0
im = cv2.GaussianBlur(im, (FEATHER_AMOUNT, FEATHER_AMOUNT), 0)

return im

(Full source code can be seen here)

I'm finding it a bit difficult to understand what the below lines does:

im = numpy.array([im, im, im]).transpose((1, 2, 0))

I don't seem to be able to find the equivalent of this in OpenCV C++ version.

Any help would be much appreciated.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-08-29 19:11:08 -0600

Tetragramm gravatar image

It rearranges the channels, so if before it was BGR (the default format), then after it would be GRB, which is weird. The statement has the identical im in all three channels, so why rearrange it?

Try just replacing it with im = [im, im, im] and see if that works.

If this is just a simplified version and you do need an OpenCV replacement, use mixChannels. I'm not sure what the python bindings are, but that's the function. The documentation there has a small C++ example.

Slightly slower, but with a python example is splitting and then merging, which is in the tutorial HERE.

edit flag offensive delete link more

Comments

I have tried it with im = [im, im, im] as you suggested but it didn't work.

I have also tried to use mixChannels as you have suggested but I don't seem to be able to manage to make it work.

Looking at the description on the tutorial given here, I understand that this code feathers the edge of the mask outwards by 11 pixels. But I don't seem to be able to get the same effect in C++.

Any further suggestions?

zealotfb gravatar imagezealotfb ( 2016-08-30 10:14:34 -0600 )edit

Oh, you want to replace the whole thing? I thought you just meant the transpose function

Use morphology operations. It should be a one liner, or two. The one difference is that it doesn't turn it into three channels the way the code you posted here does.

So do the morphology operation, so that im, as a single channel looks like what you want, then do im = [im, im, im] to make it three channels.

Tetragramm gravatar imageTetragramm ( 2016-08-30 11:27:22 -0600 )edit

That has done the trick! Thanks a lot mate.

zealotfb gravatar imagezealotfb ( 2016-08-30 12:39:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-29 16:06:31 -0600

Seen: 3,122 times

Last updated: Aug 29 '16