问题
用 LabWindows CVI2012 做直方图均衡化的时候,读入图像出现报错:
NON-FATAL RUN-TIME ERROR: "his.c", line 33, col 17, thread id 0x00004240: Function imaqReadFile: (errorCode == -1074396120 [0xbff60428]). Not an image.
代码部分:
#include <cvirte.h> #include <userint.h> #include "his.h" #include "nivision.h" static int panelHandle; static Image *SourceImage; int main (int argc, char *argv[]) { if (InitCVIRTE (0, argv, 0) == 0) return -1; /* out of memory */ if ((panelHandle = LoadPanel (0, "his.uir", PANEL)) < 0) return -1; DisplayPanel (panelHandle); RunUserInterface (); DiscardPanel (panelHandle); return 0; } int CVICALLBACK Load_and_display(int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { char fileName[512]; int status; HistogramReport *report; switch (event) { case EVENT_COMMIT: status = FileSelectPopup("", "*.bmp", "", "Select an Image File", VAL_LOAD_BUTTON, 0, 0, 1, 0, fileName); if (status == 1) { imaqReadFile(SourceImage, fileName, NULL, NULL); imaqMoveWindow(0, imaqMakePoint(50, 260)); imaqDisplayImage(SourceImage, 0, TRUE); report = imaqHistogram(SourceImage, 256, 0,255, IMAQ_IMAGE_U8); DeleteGraphPlot(panelHandle, PANEL_HISTOGRAM, -1, VAL_IMMEDIATE_DRAW); PlotY(panelHandle, PANEL_HISTOGRAM, (*report).histogram, 256, VAL_UNSIGNED_INTEGER, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); } break; } return 0; } int CVICALLBACK Quit (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: QuitUserInterface (0); break; } return 0; }
运行的时候面板能够打开,从目录中选取读入的图像文件也没有问题,但在点击 Load 加载图像的时候就遇到了报错。
刚开始看到 Not an image 我以为是图像有问题,把原本读入的 jpg 文件换成 bmp 文件,后来发现还是行不通
原因
代码中虽然定义了 SourceImage ,但是没有创建存储区域
解决
在 main() 函数中添加代码:
SourceImage = imaqCreateImage (IMAQ_IMAGE_U8, 2);
完整代码:
#include <cvirte.h> #include <userint.h> #include "his.h" #include "nivision.h" static int panelHandle; static Image *SourceImage; int main (int argc, char *argv[]) { if (InitCVIRTE (0, argv, 0) == 0) return -1; /* out of memory */ if ((panelHandle = LoadPanel (0, "his.uir", PANEL)) < 0) return -1; DisplayPanel (panelHandle); SourceImage = imaqCreateImage (IMAQ_IMAGE_U8, 2); RunUserInterface (); DiscardPanel (panelHandle); return 0; } int CVICALLBACK Load_and_display(int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { char fileName[512]; int status; HistogramReport *report; switch (event) { case EVENT_COMMIT: status = FileSelectPopup("", "*.bmp", "", "Select an Image File", VAL_LOAD_BUTTON, 0, 0, 1, 0, fileName); if (status == 1) { imaqReadFile(SourceImage, fileName, NULL, NULL); imaqMoveWindow(0, imaqMakePoint(50, 260)); imaqDisplayImage(SourceImage, 0, TRUE); report = imaqHistogram(SourceImage, 256, 0,255, IMAQ_IMAGE_U8); DeleteGraphPlot(panelHandle, PANEL_HISTOGRAM, -1, VAL_IMMEDIATE_DRAW); PlotY(panelHandle, PANEL_HISTOGRAM, (*report).histogram, 256, VAL_UNSIGNED_INTEGER, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); } break; } return 0; } int CVICALLBACK Quit (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: QuitUserInterface (0); break; } return 0; }