1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
////////////////////////////////////////////////////////////////////////////////////
// - the "src" Image must have a IPL_DEPTH_8U ofand 1 or 3 channels //
// - the convertion is done in regards to the ROI if it is set //
// - if you want to stretch your TBitmap ,specify width and height before
// calling the function
////////////////////////////////////////////////////////////////////////////////////
#include <Graphics.hpp>
#include <Dialogs.hpp>
#include <cv.h>
bool __fastcall IplImageToTBitmap (const IplImage *src,Graphics::TBitmap *dest){
if (!src || !dest)
return false;
IplImage *temp;
CvRect roi;
if (src->roi)
roi=cvGetImageROI(src);
else
roi=cvRect(0,0,src->width,src->height);
temp=cvCreateImage(cvSize(roi.width,roi.height),IPL_DEPTH_8U,3);
if (src->nChannels!=3)
cvCvtColor(src,temp,CV_GRAY2RGB);
else
cvCopy(src,temp);
if (dest->Width==0 || dest->Height==0){
dest->Width = roi.width;
dest->Height = roi.height;
}
else{
IplImage *temp1=cvCloneImage(temp);
cvReleaseImageData(temp);
cvInitImageHeader(temp,cvSize(dest->Width,dest->Height),IPL_DEPTH_8U,3,src->origin,4);
cvCreateImageData(temp);
roi.width=dest->Width;
roi.height=dest->Height;
cvResize(temp1,temp);
cvReleaseImage(&temp1);
}
dest->PixelFormat=pf24bit;
try{
unsigned char *pLine;
int x,y;
for(y=0 ;y<roi.height;y++){
pLine = (unsigned char *)dest->ScanLine[y];
for(x=0 ;x <roi.width*3 ;x++){
pLine[x]= ((unsigned char*)(temp->imageData + temp->widthStep*y))[x];
}
}
}
catch(...){
ShowMessage("Error while converting ...");
}
cvReleaseImage(&temp);
return true;
} |
Partager