Ask Your Question
1

Why in open cv morphology operations is inverted?

asked 2016-08-21 20:15:50 -0500

dpietrek gravatar image

For example dilate in open cv is erode and vice versa.

edit retag flag offensive close merge delete

Comments

1

it just depends, what you define as "foreground". (opencv prefers white here). if you swap black and white there, dilate and erode swap their meaning.

berak gravatar imageberak ( 2016-08-22 06:44:52 -0500 )edit
1

@berak I know that, but this is not intuitive. Why does opencv prefer white as foreground?

dpietrek gravatar imagedpietrek ( 2016-08-22 09:02:42 -0500 )edit
2

probably because white==255==on, and black==0==off.

but yea, if you come more from "paper world", you'd think: black==print==on, white==bg==off.

berak gravatar imageberak ( 2016-08-22 09:05:58 -0500 )edit
1

Because OpenCV is a computer vision library. It is common sense to mark background pixels as black, while white pixels are the moving blobs in the video. This is done throughout the whole community and thus thats the main reason why OpenCV decided to follow that!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-08-23 08:10:19 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2016-08-21 21:24:37 -0500

Tetragramm gravatar image

It isn't? A single white pixel creates a single kernel shape of white, when dilating, and goes away when eroding.

edit flag offensive delete link more

Comments

It is dilating and eroding white pixels. It should operate on set of black pixels. Bitwise and and or are also inverted.

dpietrek gravatar imagedpietrek ( 2016-08-22 09:00:41 -0500 )edit
1

bitwise_and and _or are exactly what you would expect. 255 = 0b11111111, and 0 = 0b00000000. So bitwise_or with 255 always gives you white, and bitwise_and with 0 always gives you black.

I can see the confusion with morphology, but the bitwise is exactly correct.

Tetragramm gravatar imageTetragramm ( 2016-08-22 21:27:15 -0500 )edit
1

answered 2016-10-09 16:58:18 -0500

Eduardo gravatar image

I would like to add some additional information even if mathematical morphology in general is out of my scope.

In image processing, the erosion operation can be defined for grayscale image and with a flat structuring element as:

erosion

The destination image is set for each location to the minimum value of the pixels that lie in the structuring element chosen.

In a same way, the dilatation operation can be defined for grayscale image and with a flat structuring element as:

dilatation

This time, the destination image is set for a particular position with the maximum value of the pixels of the source image that lie in the structuring element.

We have the same definition in the OpenCV documentation (e.g. cv::erode) or in the Matlab documentation (imerode).

This "general" definition can be applied for grayscale images and for binary images as well.

More specifically, the binary erosion of A by B is:

Erosion2

And the binary dilatation of A by B is:

Dilatation2

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-08-21 20:15:50 -0500

Seen: 881 times

Last updated: Oct 09 '16