How to Implement an Function Equivalent to Bwmorph Matlab Function in Opencv

from MATLAB to C++: equivalent of bwmorph with option 'remove'

The 'remove' option to `bwmorph:

Removes interior pixels. This option sets a pixel to 0 if all its 4-connected neighbors are 1, thus leaving only the boundary pixels on.

You can implement this using a simple 4-connected erosion, then taking the difference between input and eroded image.

auto se = getStructuringElement(MORPH_CROSS, Size{3,3});
erode(in, out, se);
subtract(in, out, out);

equivalent function of sub2ind in opencv

A quick example to illustrate. Consider:

>> v = (1:4*3)
v =
1 2 3 4 5 6 7 8 9 10 11 12
>> M = reshape(v,[4 3])
M =
1 5 9
2 6 10
3 7 11
4 8 12

Now all the following are equivalent:

sz = size(M);

i = 3; j = 2;
M(i,j)
v( sub2ind(sz,i,j) )
v( sz(1)*(j-1)+i )

Just keep in mind that MATLAB uses a column-major order, while C is row-major order

Python Equivalent for bwmorph

You will have to implement those on your own since they aren't present in OpenCV or skimage as far as I know.
However, it should be straightforward to check MATLAB's code on how it works and write your own version in Python/NumPy.

Here is a guide describing in detail NumPy functions exclusively for MATLAB users, with hints on equivalent functions in MATLAB and NumPy:
Link

roipoly matlab function equivalent in OpenCV

There is no corresponding inbuilt function like roipoly in OpenCV.

Instead, OpenCV provides functions like cv2.polyline() and cv2.drawContours(). If you have the coordinates of the vertices, (as shown in matlab) you can create a numpy array with them. Then draw this polygon on a black image, which gives you the mask image as returned by roipoly. An example is demonstrated below :

import cv2
import numpy as np

img = cv2.imread('eight.png')
mask = np.zeros(img.shape[:2],dtype = 'uint8')

c = [194, 253, 293, 245]
r = [72, 14, 76, 125]

rc = np.array((c,r)).T

cv2.drawContours(mask,[rc],0,255,-1)
cv2.drawContours(img,[rc],0,255,2)
mask = cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)

res = np.hstack((img,mask))

Below is the result I got :

Sample Image

Matlab Bwareaopen equivalent function in OpenCV

Take a look at the cvBlobsLib, it has functions to do what you want. In fact, the code example on the front page of that link does exactly what you want, I think.
Essentially, you can use CBlobResult to perform connected-component labeling on your binary image, and then call Filter to exclude blobs according to your criteria.



Related Topics



Leave a reply



Submit