Integrating C++ code with Matlab Code

1 view (last 30 days)
Hi, I am computing the output of an image using two components of a code. One component is in C++ while the other one is in matlab. I run the c++ component on my image, save it in a folder and then run my matlab code on it. Is it possible to integrate them together. Like my C++ code runs in matlab and then automatically passes the output to my matlab code??
Thanks for your help.
Amarjot

Accepted Answer

Walter Roberson
Walter Roberson on 8 Mar 2013
Using mex you can have MATLAB call the C++ part and retrieve the output.
  2 Comments
Amarjot
Amarjot on 8 Mar 2013
Thank you very much.
Amarjot
Amarjot on 9 Mar 2013
Edited: Walter Roberson on 9 Mar 2013
Hi i just looked into it. I think, i just need to modify the main file according to matlab. Can you please give me an idea how i can modify it. Here is my main file (Sorry i am very new to it)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "retinex_pde_lib.h"
#include "io_png.h"
#include "norm.h"
#include "mex.h"
/**
* @brief main function call
*/
int main(int argc, char *const *argv)
{
float t; /* retinex threshold */
size_t nx, ny, nc; /* image size */
size_t channel, nc_non_alpha;
float *data, *data_rtnx;
/* "-v" option : version info */
if (2 <= argc && 0 == strcmp("-v", argv[1])) {
fprintf(stdout, "%s version " __DATE__ "\n", argv[0]);
return EXIT_SUCCESS;
}
/* wrong number of parameters : simple help info */
if (4 != argc) {
fprintf(stderr, "usage : %s T in.png rtnx.png\n", argv[0]);
fprintf(stderr, " T retinex threshold [0...255]\n");
return EXIT_FAILURE;
}
/* retinex threshold */
t = atof(argv[1]);
if (0. > t || 255. < t) {
fprintf(stderr, "the retinex threshold must be in [0..255]\n");
return EXIT_FAILURE;
}
/* read the PNG image into data */
if (NULL == (data = read_png_f32(argv[2], &nx, &ny, &nc))) {
fprintf(stderr, "the image could not be properly read\n");
return EXIT_FAILURE;
}
/* allocate data_rtnx and fill it with a copy of data */
if (NULL == (data_rtnx = (float *) malloc(nc * nx * ny * sizeof(float)))) {
fprintf(stderr, "allocation error\n");
free(data);
return EXIT_FAILURE;
}
memcpy(data_rtnx, data, nc * nx * ny * sizeof(float));
/* the image has either 1 or 3 non-alpha channels */
if (3 <= nc)
nc_non_alpha = 3;
else
nc_non_alpha = 1;
/*
* run retinex on each non-alpha channel data_rtnx,
* normalize mean and standard deviation and save
*/
for (channel = 0; channel < nc_non_alpha; channel++) {
if (NULL == retinex_pde(data_rtnx + channel * nx * ny, nx, ny, t)) {
fprintf(stderr, "the retinex PDE failed\n");
free(data_rtnx);
free(data);
return EXIT_FAILURE;
}
normalize_mean_dt(data_rtnx + channel * nx * ny,
data + channel * nx * ny, nx * ny);
}
write_png_f32(argv[3], data_rtnx, nx, ny, nc);
free(data_rtnx);
free(data);
return EXIT_SUCCESS;
}

Sign in to comment.

More Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!