2066 1

[Weka及其他] MATLAB课程:代码示例之C Code Generation(二) [推广有奖]

企业贵宾

已卖:160份资源

巨擘

0%

还不是VIP/贵宾

-

威望
4
论坛币
624047 个
通用积分
180.4857
学术水平
918 点
热心指数
987 点
信用等级
841 点
经验
399143 点
帖子
9786
精华
48
在线时间
17322 小时
注册时间
2014-8-19
最后登录
2022-11-2

楼主
widen我的世界 学生认证  发表于 2016-3-16 12:04:58 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

MATLAB课程:代码示例之C Code Generation(二)


Edge Detection on Images


This example shows how to generate a standalone C library from MATLAB code that implements a simple Sobel filter that performs edge detection on images. The example also shows how to generate and test a MEX function in MATLAB prior to generating C code to verify that the MATLAB code is suitable for code generation.


Prerequisites

There are no prerequisites for this example.

Create a New Folder and Copy Relevant Files

The following code will create a folder in your current working folder (pwd). The new folder will only contain the files that are relevant for this example. If you do not want to affect the current folder (or if you cannot generate files in this folder), you should change your working folder.

Run Command: Create a New Folder and Copy Relevant Filescoderdemo_setup('coderdemo_edge_detection');


About the 'sobel' Function

The sobel.m function takes an image (represented as a double matrix) and a threshold value and returns an image with the edges detected (based on the threshold value).

type sobel


% edgeImage = sobel(originalImage, threshold)% Sobel edge detection. Given a normalized image (with double values)% return an image where the edges are detected w.r.t. threshold value.function edgeImage = sobel(originalImage, threshold) %#codegenassert(all(size(originalImage) <= [1024 1024]));assert(isa(originalImage, 'double'));assert(isa(threshold, 'double'));k = [1 2 1; 0 0 0; -1 -2 -1];H = conv2(double(originalImage),k, 'same');V = conv2(double(originalImage),k','same');E = sqrt(H.*H + V.*V);edgeImage = uint8((E > threshold) * 255);


Generate the MEX Function

Generate a MEX function using the 'codegen' command.

codegen sobel


Before generating C code, you should first test the MEX function in MATLAB to ensure 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 'sobel_mex' in the current folder. This allows you to test the MATLAB code and MEX function and compare the results.

Read in the Original Image

Use the standard 'imread' command.

im = imread('hello.jpg');image(im);


Convert Image to a Grayscale Version

Convert the color image (shown above) to an equivalent grayscale image with normalized values (0.0 for black, 1.0 for white).

gray = (0.2989 * double(im(:,:,1)) + 0.5870 * double(im(:,:,2)) + 0.1140 * double(im(:,:,3)))/255;


Run the MEX Function (The Sobel Filter)

Pass the normalized image and a threshold value.

edgeIm = sobel_mex(gray, 0.7);


Display the Resultim3 = repmat(edgeIm, [1 1 3]);image(im3);


Generate Standalone C Codecodegen -config coder.config('lib') sobel


Using 'codegen' with the '-config coder.config('lib')' option produces a standalone C library. By default, the code generated for the library is in the folder codegen/lib/sobel/

Inspect the Generated Functiontype codegen/lib/sobel/sobel.c


/* * File: sobel.c * * MATLAB Coder version            : 3.1 * C/C++ source code generated on  : 07-Jan-2016 17:21:15 *//* Include Files */#include "rt_nonfinite.h"#include "sobel.h"#include "sobel_emxutil.h"#include "sqrt.h"#include "conv2.h"/* Function Declarations */static double rt_roundd_snf(double u);/* Function Definitions *//* * Arguments    : double u * Return Type  : double */static double rt_roundd_snf(double u){  double y;  if (fabs(u) < 4.503599627370496E+15) {    if (u >= 0.5) {      y = floor(u + 0.5);    } else if (u > -0.5) {      y = u * 0.0;    } else {      y = ceil(u - 0.5);    }  } else {    y = u;  }  return y;}/* * Arguments    : const emxArray_real_T *originalImage *                double threshold *                emxArray_uint8_T *edgeImage * Return Type  : void */void sobel(const emxArray_real_T *originalImage, double threshold,           emxArray_uint8_T *edgeImage){  emxArray_real_T *H;  emxArray_real_T *V;  int b_H;  int c_H;  emxInit_real_T(&H, 2);  emxInit_real_T(&V, 2);  /*  edgeImage = sobel(originalImage, threshold) */  /*  Sobel edge detection. Given a normalized image (with double values) */  /*  return an image where the edges are detected w.r.t. threshold value. */  conv2(originalImage, H);  b_conv2(originalImage, V);  b_H = H->size[0] * H->size[1];  emxEnsureCapacity((emxArray__common *)H, b_H, (int)sizeof(double));  b_H = H->size[0];  c_H = H->size[1];  c_H *= b_H;  for (b_H = 0; b_H < c_H; b_H++) {    H->data[b_H] = H->data[b_H] * H->data[b_H] + V->data[b_H] * V->data[b_H];  }  emxFree_real_T(&V);  b_sqrt(H);  b_H = edgeImage->size[0] * edgeImage->size[1];  edgeImage->size[0] = H->size[0];  edgeImage->size[1] = H->size[1];  emxEnsureCapacity((emxArray__common *)edgeImage, b_H, (int)sizeof(unsigned    char));  c_H = H->size[0] * H->size[1];  for (b_H = 0; b_H < c_H; b_H++) {    edgeImage->data[b_H] = (unsigned char)rt_roundd_snf((double)(H->data[b_H] >      threshold) * 255.0);  }  emxFree_real_T(&H);}/* * File trailer for sobel.c * * [EOF] */


Cleanup

Remove files and return to original folder

Run Command: Cleanupcleanup




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Generation MATLAB课程 ration MATLAB matla MATLAB课程 代码示例 CCodeGeneration EdgeDetectiononImages


https://www.cda.cn/?seo-luntan
高薪就业·数据科学人才·16年教育品牌

沙发
美国队长2 在职认证  发表于 2016-3-16 12:05:41
很厉害,不过MATLAB课程我还没学到这个程度……

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-22 11:38