问题
用 LabWindows CVI2012 做图像的形态学处理的时候,处理图像出现报错:
NON-FATAL RUN-TIME ERROR: "morph_pro.c", line 88, col 9, thread id 0x00000C28: Function imaqMorphology: (errorCode == -1074396154 [0xbff60406]). The image is not large enough for the operation.
代码部分:
#include "nivision.h" #include <cvirte.h> #include <userint.h> #include "morph_pro.h" #include "nivision.h" static int morphMoth; static Image *SourceImage; static Image *DestImage; static Image *TempImage; int main (int argc, char *argv[]) { if (InitCVIRTE (0, argv, 0) == 0) return -1; /* out of memory */ if ((morphMoth = LoadPanel (0, "morph_pro.uir", MORPH_MOTH)) < 0) return -1; DisplayPanel (morphMoth); SourceImage = imaqCreateImage(IMAQ_IMAGE_U8, 2); DestImage = imaqCreateImage(IMAQ_IMAGE_U8, 2); TempImage = imaqCreateImage(IMAQ_IMAGE_U8, 2); RunUserInterface (); DiscardPanel (morphMoth); return 0; } int CVICALLBACK Load_image(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(morphMoth, MORPH_MOTH_HISTOGRAM, -1, VAL_IMMEDIATE_DRAW); PlotY(morphMoth, MORPH_MOTH_HISTOGRAM, (*report).histogram, 256, VAL_UNSIGNED_INTEGER, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); } break; } return 0; } int CVICALLBACK Threshold(int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { int thre_value; HistogramReport *report; switch (event) { case EVENT_COMMIT: GetCtrlVal(morphMoth, MORPH_MOTH_NUMERICSLIDE, &thre_value); imaqThreshold(DestImage, SourceImage, thre_value, 255, TRUE, 255); imaqSetWindowTitle(1, "二值化分割"); imaqMoveWindow(1, imaqMakePoint(150, 260)); imaqDisplayImage(DestImage, 1, TRUE); report = imaqHistogram(DestImage, 256, 0, 255, IMAQ_IMAGE_U8); DeleteGraphPlot(morphMoth, MORPH_MOTH_HISTOGRAM, -1, VAL_IMMEDIATE_DRAW); PlotY(morphMoth, MORPH_MOTH_HISTOGRAM, (*report).histogram, 256, VAL_UNSIGNED_INTEGER, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); break; } return 0; } int CVICALLBACK Morph_processing(int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { int methodValue; switch (event) { case EVENT_COMMIT: GetCtrlVal(morphMoth, MORPH_MOTH_MORPH_MOTHED, &methodValue); imaqMorphology(DestImage, TempImage, methodValue, NULL); imaqSetWindowPalette(2, IMAQ_PALETTE_BINARY, NULL, 2); imaqSetWindowTitle(2, "形态学处理"); imaqMoveWindow(2, imaqMakePoint(50, 60)); imaqDisplayImage(DestImage, 2, TRUE); imaqDisplayImage(TempImage, 2, TRUE); break; } return 0; } int CVICALLBACK Save_an_image(int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { char SavePath[260]; ImageInfo Image_Info; int status; int width, height; switch (event) { case EVENT_COMMIT: status = FileSelectPopup("", "*.bmp,*.tif,*.apd", "*.bmp", "Select a SavePath", VAL_SAVE_BUTTON, 0, 0, 1, 0, SavePath); if (status) { imaqWriteFile(DestImage, SavePath, NULL); } 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; }
原因
TempImage 和 DestImage 尺寸大小不一
解决
在形态学处理之前,利用 imaqSetImageSize () 函数将两张图像缩放到同一尺寸
imaqSetImageSize(TempImage,640,320); imaqSetImageSize(DestImage,640,320);
完整代码:
#include "nivision.h" #include <cvirte.h> #include <userint.h> #include "morph_pro.h" #include "nivision.h" static int morphMoth; static Image *SourceImage; static Image *DestImage; static Image *TempImage; int main (int argc, char *argv[]) { if (InitCVIRTE (0, argv, 0) == 0) return -1; /* out of memory */ if ((morphMoth = LoadPanel (0, "morph_pro.uir", MORPH_MOTH)) < 0) return -1; DisplayPanel (morphMoth); SourceImage = imaqCreateImage(IMAQ_IMAGE_U8, 2); DestImage = imaqCreateImage(IMAQ_IMAGE_U8, 2); TempImage = imaqCreateImage(IMAQ_IMAGE_U8, 2); RunUserInterface (); DiscardPanel (morphMoth); return 0; } int CVICALLBACK Load_image(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(morphMoth, MORPH_MOTH_HISTOGRAM, -1, VAL_IMMEDIATE_DRAW); PlotY(morphMoth, MORPH_MOTH_HISTOGRAM, (*report).histogram, 256, VAL_UNSIGNED_INTEGER, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); } break; } return 0; } int CVICALLBACK Threshold(int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { int thre_value; HistogramReport *report; switch (event) { case EVENT_COMMIT: imaqSetImageSize(TempImage,640,320); imaqSetImageSize(DestImage,640,320); GetCtrlVal(morphMoth, MORPH_MOTH_NUMERICSLIDE, &thre_value); imaqThreshold(DestImage, SourceImage, thre_value, 255, TRUE, 255); imaqSetWindowTitle(1, "二值化分割"); imaqMoveWindow(1, imaqMakePoint(150, 260)); imaqDisplayImage(DestImage, 1, TRUE); report = imaqHistogram(DestImage, 256, 0, 255, IMAQ_IMAGE_U8); DeleteGraphPlot(morphMoth, MORPH_MOTH_HISTOGRAM, -1, VAL_IMMEDIATE_DRAW); PlotY(morphMoth, MORPH_MOTH_HISTOGRAM, (*report).histogram, 256, VAL_UNSIGNED_INTEGER, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); break; } return 0; } int CVICALLBACK Morph_processing(int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { int methodValue; switch (event) { case EVENT_COMMIT: GetCtrlVal(morphMoth, MORPH_MOTH_MORPH_MOTHED, &methodValue); imaqMorphology(DestImage, TempImage, methodValue, NULL); imaqSetWindowPalette(2, IMAQ_PALETTE_BINARY, NULL, 2); imaqSetWindowTitle(2, "形态学处理"); imaqMoveWindow(2, imaqMakePoint(50, 60)); imaqDisplayImage(DestImage, 2, TRUE); imaqDisplayImage(TempImage, 2, TRUE); break; } return 0; } int CVICALLBACK Save_an_image(int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { char SavePath[260]; ImageInfo Image_Info; int status; int width, height; switch (event) { case EVENT_COMMIT: status = FileSelectPopup("", "*.bmp,*.tif,*.apd", "*.bmp", "Select a SavePath", VAL_SAVE_BUTTON, 0, 0, 1, 0, SavePath); if (status) { imaqWriteFile(DestImage, SavePath, NULL); } 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; }