EN ·
🌏 中文

Tips for Converting NumPy to OpenCV Mat

Sigmoid Function

A sigmoid function is a mathematical function having a characteristic "S"-shaped curve or sigmoid curve.

From Wikipedia: Sigmoid_function

The formula is: S(t)=11+etS(t)=\frac {1}{1+e^{-t}}

We can implement the sigmoid function in Python using NumPy as follows:

import numpy as np
z = 1/(1 + np.exp(-x))

Now, let’s implement this in C++ using OpenCV. Suppose we have an output from a neural network stored as a [1, h, w] cv::Mat named pred. We want to perform an operation similar to pred = sigmoid(pred):

cv::Mat sigmoid(const cv::Mat& pred)
{
  cv::Mat z;
  cv::exp(-pred, z);
  z = 1.f/(1.f+z);
  return z;
}  

Done. Simple. 😊

Channel Split and Merge

In NumPy or similar APIs, we can perform slicing operations like this:

temp = preds[:2, :, :]

This operation creates a snapshot of the first two channels. In OpenCV, we can simulate this by splitting and merging channels:

  std::vector<cv::Mat> preds_split;
  cv::split(preds, preds_split);
  
  std::vector<cv::Mat> preds_temp = {preds_split[0], preds_split[1]};
  cv::Mat temp;
  cv::merge(preds_temp, temp);

Conditional Filtering of NumPy Matrices

Suppose we have a 2D NumPy array where the first channel is the score map of an input image. We need to filter pixels that exceed a specific threshold.

  pred_score = preds[0] # Extract the first channel of the preds output
  pred_mask = pred_score > min_confidence 

The implementation in C++:

  std::vector<cv::Mat> preds;
  cv::split(output, preds);
  cv::Mat pred_score = preds[0];

  cv::threshold(pred_score, pred_mask, min_confidence, 1.f, cv::THRESH_BINARY);
  cv::multiply(pred_mask, 255, pred_mask);
  pred_mask.convertTo(pred_mask, CV_8U);

To be continued.