opencv4nodejs icon indicating copy to clipboard operation
opencv4nodejs copied to clipboard

CV::Merge binding

Open pereibex opened this issue 3 years ago • 2 comments

Hi,

I'm trying to add the binding for CV::Merge

cv::merge(InputArrayOfArrays mv, OutputArray dst)

But I don't know how to treat InputArrayOfArrays in coreBindings.h, I haven't found another function that uses them.

    class Merge : public CvClassMethodBinding<Mat> {
    public:
            void setup() {
                    auto mat = ???
                    auto dst = ret<Mat::Converter>("dst");
                    executeBinding = [=]() {
                            cv::merge(mat->ref(), dst->ref());
                    };
            };
    };

function as defined as: export function merge(mat: Mat[]): Mat;

Regards and thanks for your work

pereibex avatar Nov 30 '22 20:11 pereibex

I do not remember how it woorks.

but As I told you find existing code with params and return type, and that will be easy.

the function is : void cv::merge (const Mat *mv, size_t count, OutputArray dst)

will be add to core_array.d.ts as

  export function merge (src: Mat, count?: number): Mat;
  export function mergeAsync(src: Mat, count?: number): Promise<Mat>;

or added in Mat.d.ts

export class Mat {
  merge (count?: number): Mat;
  mergeAsync(count?: number): Promise<Mat>;
}

The first argument is a Mat, so pick the first argument mapping from on another one. like:

export function mulSpectrums(src1: Mat, src2: Mat, dftRows?: boolean, conjB?: boolean): Mat;
export function mulSpectrumsAsync(src1: Mat, src2: Mat, dftRows?: boolean, conjB?: boolean): Promise<Mat>;

this function also returns a Mat

or pick the code from:

  idct(flags?: number): Mat;
  idctAsync(flags?: number): Promise<Mat>;

the signature is the same.

From Mat.cc duplicate:

  Nan::SetPrototypeMethod(ctor, "idct", Idct);
  Nan::SetPrototypeMethod(ctor, "idctAsync", IdctAsync);

as

  Nan::SetPrototypeMethod(ctor, "merge ", Merge);
  Nan::SetPrototypeMethod(ctor, "mergeAsync", MergeAsync);

From Mat.cc duplicate:

NAN_METHOD(Mat::Idct) {
  FF::executeSyncBinding(
    std::make_shared<MatBindings::DCTWorker>(Mat::unwrapSelf(info)),
    "Mat::Idct",
    info
  );
}

NAN_METHOD(Mat::IdctAsync) {
  FF::executeAsyncBinding(
    std::make_shared<MatBindings::DCTWorker>(Mat::unwrapSelf(info)),
    "Mat::IdctAsync",
    info
  );
}

as

NAN_METHOD(Mat::Merge) {
  FF::executeSyncBinding(
    std::make_shared<MatBindings::MergeWorker>(Mat::unwrapSelf(info)),
    "Mat::Merge",
    info
  );
}

NAN_METHOD(Mat::MergeAsync) {
  FF::executeAsyncBinding(
    std::make_shared<MatBindings::MergeWorker>(Mat::unwrapSelf(info)),
    "Mat::MergeAsync",
    info
  );
}

From Mat.h duplicate:

  static NAN_METHOD(Idct);
  static NAN_METHOD(IdctAsync);

as

  static NAN_METHOD(Merge);
  static NAN_METHOD(MergeAsync);

MatBindings.h

Duplique the DCTWorker as MergeWorker, I see a // TODO in the code, which may cause some issues. build It, best It, ... write a test...

I have some HTML stuff to write right now... So keep going.

add needed

#if CV_VERSION_GREATER_EQUAL(3, 2, 0) if the function is not supper old. #endif

UrielCh avatar Dec 01 '22 06:12 UrielCh

I managed to bind another function without problems, but with Merge the first parameter is an array of Mats, not a Mat, and I can't get it to compile correctly:

../cc/core/coreBindings.h:266:80: error: no matching function for call to ‘merge(cv::Mat&, int&, cv::Mat&)’ 266 | cv::merge(mat->ref(), size_t->ref(), dst->ref()); ... /node_modules/@u4/opencv4nodejs/node_modules/@u4/opencv-build/opencv-4.5.5- ecae4/build/include/opencv4/opencv2/core.hpp:945:17: note: candidate: ‘void cv::merge(const cv::Mat*, size_t, cv::OutputArray)’ 945 | CV_EXPORTS void merge(const Mat* mv, size_t count, OutputArray dst); ... /node_modules/@u4/opencv4nodejs/node_modules/@u4/opencv-build/opencv-4.5.5- ecae4/build/include/opencv4/opencv2/core.hpp:945:34: note: no known conversion for argument 1 from ‘cv::Mat’ to ‘const cv::Mat*’ 945 | CV_EXPORTS void merge(const Mat* mv, size_t count, OutputArray dst);

I can define the function with: export function merge (src: Mat[], count?: number): Mat;

But then I don't know how to bind the Array of Mats in coreBindings.h, doing it as if it were a Mat (auto mat = reqMat::Converter()) doesn't work.

pereibex avatar Dec 05 '22 10:12 pereibex