EDN Admin
Well-known member
Hello,
Im trying to copy data from a TIFF image into an OpenCV IplImage. I have also posted this question to the OpenCV forums, but I feel like I must be missing some pointer basics, and so have posted here:
First, code for loading the TIFF, straightforward:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; void CProjectorTabCtrl::OnBnClickedButtoncalibrate()
{
TIFF* currentTif;
IplImage* <span style="color:Blue; in = NULL;
<span style="color:Blue; int tiffW;
CFileDialog dlg(TRUE, _T(<span style="color:#A31515; "*.tif"), <span style="color:#A31515; "", OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY, <span style="color:#A31515; "TIF images (*.tif) |*.tif||",NULL);
dlg.m_ofn.lpstrTitle= <span style="color:#A31515; "Load an TIFF Image";
<span style="color:Blue; if (dlg.DoModal() == IDOK) {
LPCSTR m_filename = dlg.GetPathName();
currentTif = TIFFOpen(m_filename, <span style="color:#A31515; "r");
TIFFGetField(currentTif, TIFFTAG_IMAGEWIDTH, &tiffW); <span style="color:Green; // get image width
CString errorString = TEXT(<span style="color:#A31515; "");
errorString.Format(TEXT(<span style="color:#A31515; "%d"), tiffW);
MessageBox(errorString,<span style="color:#A31515; "TIFF width is non-zero...",0); <span style="color:Green; // TIFF is good
<span style="color:Blue; in = TIFFtoIpl(currentTif);
} <span style="color:Blue; else {
MessageBox(<span style="color:#A31515; "Did not get a valid image!", <span style="color:#A31515; "Did not get a valid image!",0);
}
}
[/code]
Then code for the conversion:
<div style="color:Black;background-color:White; <pre>
IplImage* CProjectorTabCtrl::TIFFtoIpl(TIFF* tif){
<span style="color:Blue; int w,h,Width,Height;
uint32* TIFFImageData = NULL;
<span style="color:Blue; unsigned <span style="color:Blue; long result, imageOffset;
<span style="color:Blue; int stripMax, stripCount;
<span style="color:Blue; unsigned <span style="color:Blue; long *buffer = NULL;
<span style="color:Blue; int numbytes = 4;
int32_t rowsperstrip, stripbytecnt, numstrips;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Width); <span style="color:Green; // get size
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Height);
numstrips = TIFFNumberOfStrips(tif);
rowsperstrip = Width/numstrips;
stripbytecnt = TIFFStripSize(tif);
IplImage *m_Ipl = cvCreateImage(cvSize(Width,Height), IPL_DEPTH_16U , 1); <span style="color:Green; // create empty destination image
imageOffset = 0;
stripMax = Height/rowsperstrip;
<span style="color:Blue; for (stripCount = 0; stripCount < stripMax; stripCount++){
result = TIFFReadRawStrip(tif, stripCount, buffer + imageOffset, stripbytecnt);
imageOffset += (result/numbytes);
}
<span style="color:Blue; unsigned <span style="color:Blue; short* srcPtr = (<span style="color:Blue; unsigned <span style="color:Blue; short*)buffer;
<span style="color:Blue; unsigned <span style="color:Blue; short* iplPtr = (<span style="color:Blue; unsigned <span style="color:Blue; short*)m_Ipl->imageData;
MessageBox(<span style="color:#A31515; "before pointers", <span style="color:#A31515; "before pointers",0); <span style="color:Green; // code got here ...
<span style="color:Blue; for ( w = 0; w < Width; w++){
<span style="color:Blue; for (h = 0; h < Height; h++){
iplPtr[Height*w + h] = srcPtr[Height*w + h];
}
}
MessageBox(<span style="color:#A31515; "after pointers", <span style="color:#A31515; "after pointers",0); <span style="color:Green; //... but not here
<span style="color:Blue; return(m_Ipl);
[/code]
Both compile just fine, OpenCV is properly installed and the images I test on are valid TIFF images. When run, the code gets to just before the copying of the pointers, then crashes. Debugging results in
0xC0000005: Access violation reading location 0x00000000.
which leads me to think its a problem with the pointers.
Any input you can give is greatly appreciated.
Best,
--Phu
View the full article
Im trying to copy data from a TIFF image into an OpenCV IplImage. I have also posted this question to the OpenCV forums, but I feel like I must be missing some pointer basics, and so have posted here:
First, code for loading the TIFF, straightforward:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; void CProjectorTabCtrl::OnBnClickedButtoncalibrate()
{
TIFF* currentTif;
IplImage* <span style="color:Blue; in = NULL;
<span style="color:Blue; int tiffW;
CFileDialog dlg(TRUE, _T(<span style="color:#A31515; "*.tif"), <span style="color:#A31515; "", OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY, <span style="color:#A31515; "TIF images (*.tif) |*.tif||",NULL);
dlg.m_ofn.lpstrTitle= <span style="color:#A31515; "Load an TIFF Image";
<span style="color:Blue; if (dlg.DoModal() == IDOK) {
LPCSTR m_filename = dlg.GetPathName();
currentTif = TIFFOpen(m_filename, <span style="color:#A31515; "r");
TIFFGetField(currentTif, TIFFTAG_IMAGEWIDTH, &tiffW); <span style="color:Green; // get image width
CString errorString = TEXT(<span style="color:#A31515; "");
errorString.Format(TEXT(<span style="color:#A31515; "%d"), tiffW);
MessageBox(errorString,<span style="color:#A31515; "TIFF width is non-zero...",0); <span style="color:Green; // TIFF is good
<span style="color:Blue; in = TIFFtoIpl(currentTif);
} <span style="color:Blue; else {
MessageBox(<span style="color:#A31515; "Did not get a valid image!", <span style="color:#A31515; "Did not get a valid image!",0);
}
}
[/code]
Then code for the conversion:
<div style="color:Black;background-color:White; <pre>
IplImage* CProjectorTabCtrl::TIFFtoIpl(TIFF* tif){
<span style="color:Blue; int w,h,Width,Height;
uint32* TIFFImageData = NULL;
<span style="color:Blue; unsigned <span style="color:Blue; long result, imageOffset;
<span style="color:Blue; int stripMax, stripCount;
<span style="color:Blue; unsigned <span style="color:Blue; long *buffer = NULL;
<span style="color:Blue; int numbytes = 4;
int32_t rowsperstrip, stripbytecnt, numstrips;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Width); <span style="color:Green; // get size
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Height);
numstrips = TIFFNumberOfStrips(tif);
rowsperstrip = Width/numstrips;
stripbytecnt = TIFFStripSize(tif);
IplImage *m_Ipl = cvCreateImage(cvSize(Width,Height), IPL_DEPTH_16U , 1); <span style="color:Green; // create empty destination image
imageOffset = 0;
stripMax = Height/rowsperstrip;
<span style="color:Blue; for (stripCount = 0; stripCount < stripMax; stripCount++){
result = TIFFReadRawStrip(tif, stripCount, buffer + imageOffset, stripbytecnt);
imageOffset += (result/numbytes);
}
<span style="color:Blue; unsigned <span style="color:Blue; short* srcPtr = (<span style="color:Blue; unsigned <span style="color:Blue; short*)buffer;
<span style="color:Blue; unsigned <span style="color:Blue; short* iplPtr = (<span style="color:Blue; unsigned <span style="color:Blue; short*)m_Ipl->imageData;
MessageBox(<span style="color:#A31515; "before pointers", <span style="color:#A31515; "before pointers",0); <span style="color:Green; // code got here ...
<span style="color:Blue; for ( w = 0; w < Width; w++){
<span style="color:Blue; for (h = 0; h < Height; h++){
iplPtr[Height*w + h] = srcPtr[Height*w + h];
}
}
MessageBox(<span style="color:#A31515; "after pointers", <span style="color:#A31515; "after pointers",0); <span style="color:Green; //... but not here
<span style="color:Blue; return(m_Ipl);
[/code]
Both compile just fine, OpenCV is properly installed and the images I test on are valid TIFF images. When run, the code gets to just before the copying of the pointers, then crashes. Debugging results in
0xC0000005: Access violation reading location 0x00000000.
which leads me to think its a problem with the pointers.
Any input you can give is greatly appreciated.
Best,
--Phu
View the full article