01-10-2024 02:01 AM
Hello, I'm using call library function and the code below is for building a dll file.
#include "extcode.h"
#include <stdio.h>
#include <math.h>
// Bilinear interpolation function
double bilinear_interpolation(double* array, int width, int height, double x, double y) {
int x1 = (int)x;
int y1 = (int)y;
int x2 = x1 + 1;
int y2 = y1 + 1;
double q11 = array[y1 * width + x1];
double q21 = array[y1 * width + x2];
double q12 = array[y2 * width + x1];
double q22 = array[y2 * width + x2];
double dx1 = x2 - x;
double dx2 = x - x1;
double dy1 = y2 - y;
double dy2 = y - y1;
double value = (q11 * dx1 * dy1 + q21 * dx2 * dy1 + q12 * dx1 * dy2 + q22 * dx2 * dy2);
return value;
}
_declspec(dllexport) void ARRAY2D(double* array1,
double* array2,
double* array3,
int array_length_row,
int array_length_col,
int x_shift,
int y_shift,
double angle,
double magnification);
_declspec(dllexport) void ARRAY2D(double* array1,
double* array2,
double* array3,
int array_length_row,
int array_length_col,
int x_shift,
int y_shift,
double angle,
double magnification) {
int i, j;
double radians = angle * 3.141592 / 180.0;
double cos_angle = cos(radians);
double sin_angle = sin(radians);
// Calculate the new dimensions after rescaling
int new_width = (int)(round(array_length_col * magnification));
int new_height = (int)(round(array_length_row * magnification));
for (i = 0; i < array_length_row; i++) {
for (j = 0; j < array_length_col; j++) {
// Rescale coordinates
double new_i2 = (i) / magnification;
double new_j2 = (j) / magnification;
// Adjust for centering
new_i2 += (array_length_row - new_height) / 2.0;
new_j2 += (array_length_col - new_width) / 2.0;
// Bilinear interpolation
//array2[(i * array_length_col) + j] = bilinear_interpolation(array1, array_length_col, array_length_row, new_j2, new_i2);
int new_i1 = (int)(cos_angle * i - sin_angle * j);
int new_j1 = (int)(sin_angle * i + cos_angle * j);
array3[(i * array_length_col) + j] = (bilinear_interpolation(array2, array_length_col, array_length_row, new_j2, new_i2) -
array1[((new_i1 - y_shift) * array_length_col) + (new_j1 - x_shift)] + 65535) / 2;
}
}
}
I'm controlling 2 sCMOS pco cameras with the example VI they gave. I started from the example and modified that code to implement image processing in cpp to use in call library function. My task is to rescale array2(controlling magnification), translate or rotate array1, and then subtract array1(the red boundary display on left) from array2(the right display) to form array3(bottom display). The problem is that
this kind of memory error occurs every time but if I click continue, the error sometimes fades away and the VI operates normally but sometimes not.
If it operates normally after I click continue in some cases, the display is shown like this but I can observe the very awkward pattern on the top side of array3 so I guess there is a problem but it is hard to troubleshoot.
Do you know reasons of my problem and how to fix it?
01-10-2024 03:11 AM - edited 01-10-2024 03:12 AM
Generally I would do your specific operations in native LabVIEW. I don't see why you would do this externally.
However, check if your set types and connected wire types at the call library node are correct. If those are correct use the IDE you use
for your DLL's to hang into LabVIEW process and debug your indexing.
Consider uploading a working minimal example containing your data and setup cln nodes.
01-10-2024 06:27 AM
Lots of potential errors but my first rough guess is that your original image ROI you enter multiplied by the magnification is getting bigger than 2048 * 2060 pixels.
You can't just operate like in LabVIEW when interfacing to C code. Your C code does NOT adjust memory sizes of those arrays as needed, so whatever you pass in is what it gets. But if the C code then starts indexing memory outside of that passed in buffer, you get these errors (or a full crash).