main content

generate code for sobel edge detection that uses half-凯发k8网页登录

this example shows how to generate a standalone c library from a matlab® function that performs sobel edge detection of images by using half-precision floating point numbers. the sobel edge algorithm accepts an image that is represented as a matrix and returns an image emphasizing the high spatial frequency regions that correspond to its edges. this example also shows how to test the generated code by using a mex function.

sobel edge detection algorithm

in the sobel edge detection algorithm, a 2-d spatial gradient operation is performed on a grayscale image. this operation emphasizes the high spatial frequency regions that correspond to the edges in the image.

type sobeledgedetectionalg
function edgeimg = sobeledgedetectionalg(img,thresh)  %#codegen
% entry-point function for half-precision sobel edge detection example.
% 凯发官网入口首页 copyright 2018-2022 the mathworks, inc.
kern = half([1 2 1; 0 0 0; -1 -2 -1]);
% finding horizontal and vertical gradients.
h = conv2(img(:,:,2),kern,'same');
v = conv2(img(:,:,2),kern','same');
% finding magnitude of the gradients.
e = sqrt(h.*h   v.*v);
% threshold the edges
edgeimg = uint8((e > thresh) * 240);
end

the sobel edge algorithm computes the horizontal gradient h and the vertical gradient v of the input image by using two orthogonal filter kernels maskx and masky. after the filtering operation, the algorithm computes the gradient magnitude and applies a threshold to find the regions of the image that correspond to the edges.

read images and pack data into rgba packed column major order

use the function to read the images. imread represents the rgb channels of an images with integers, one for each pixel. the integers range from 0 to 255. simply casting inputs to half type might result in overflow during convolutions. to avoid this issue, scale the images to values between 0 and 1.

im = imread('peppers.png');
figure();
image(im);
impacked = half(im)/255;
thresh = half(100)/255;

generate mex

generate a c mex function for the sobeledgedetectionalg function by using the codegen command.

cfg = coder.config('mex');
cfg.targetlang = 'c  ';
cfg.generatereport = true;
codegen -config cfg -args {impacked,thresh} sobeledgedetectionalg
code generation successful: to view the report, open('codegen/mex/sobeledgedetectionalg/html/report.mldatx')

run generated mex and display detected edge

before generating c code, you must first test the mex function inside matlab environment to make sure that it is functionally equivalent to the original matlab code and that no run-time errors occur. by default, codegen generates a mex function named sobeledgedetectionalg_mex in the current folder. this allows you to test the matlab code and mex function and compare the results.

out_disp = sobeledgedetectionalg_mex(impacked,thresh);
figure();
imagesc(out_disp);

generate static c library

use the codegen command to produces a c static library. by default, the generated library is located in the folder codegen/lib/sobeledgedetectionalg/.

cfg = coder.config('lib');
cfg.targetlang = 'c  ';
cfg.generatereport = true;
codegen -config cfg -args {impacked,thresh} sobeledgedetectionalg;
code generation successful: to view the report, open('codegen/lib/sobeledgedetectionalg/html/report.mldatx')

inspect the generated function

type codegen/lib/sobeledgedetectionalg/sobeledgedetectionalg.cpp
//
// file: sobeledgedetectionalg.cpp
//
// matlab coder version            : 5.6
// c/c   source code generated on  : 03-mar-2023 04:51:05
//
// include files
#include "sobeledgedetectionalg.h"
#include "conv2movingwindowsamecm.h"
#include "rtwhalf.h"
#include "sobeledgedetectionalg_data.h"
#include "sobeledgedetectionalg_initialize.h"
#include 
// function definitions
//
// entry-point function for half-precision sobel edge detection example.
//  凯发官网入口首页 copyright 2018-2022 the mathworks, inc.
//
// arguments    : const real16_t img[589824]
//                real16_t thresh
//                unsigned char edgeimg[196608]
// return type  : void
//
void sobeledgedetectionalg(const real16_t img[589824], real16_t thresh,
                           unsigned char edgeimg[196608])
{
  static const real16_t hv[9]{real16_t(1.0f), real16_t(0.0f), real16_t(-1.0f),
                              real16_t(2.0f), real16_t(0.0f), real16_t(-2.0f),
                              real16_t(1.0f), real16_t(0.0f), real16_t(-1.0f)};
  static const real16_t hv1[9]{
      real16_t(1.0f),  real16_t(2.0f),  real16_t(1.0f),
      real16_t(0.0f),  real16_t(0.0f),  real16_t(0.0f),
      real16_t(-1.0f), real16_t(-2.0f), real16_t(-1.0f)};
  static real16_t h[196608];
  static real16_t v[196608];
  if (!isinitialized_sobeledgedetectionalg) {
    sobeledgedetectionalg_initialize();
  }
  //  finding horizontal and vertical gradients.
  coder::conv2movingwindowsamecm(&img[196608], hv, h);
  coder::conv2movingwindowsamecm(&img[196608], hv1, v);
  //  finding magnitude of the gradients.
  //  threshold the edges
  for (int k{0}; k < 196608; k  ) {
    real16_t b_h;
    real16_t h1;
    b_h = h[k];
    h1 = v[k];
    b_h = static_cast(
        std::sqrt(static_cast(b_h * b_h   h1 * h1)));
    h[k] = b_h;
    edgeimg[k] = static_cast((b_h > thresh) * 240u);
  }
}
//
// file trailer for sobeledgedetectionalg.cpp
//
// [eof]
//

see also

| |

related topics

  • (fixed-point designer)
网站地图