How can I convert an image from IPL_DEPTH_8U to IPL_DEPTH_32S?
I need to use the watershed function and I have a marker image in IPL_DEPTH_8U. The problem is that the watershed function only accepts IPL_DEPTH_32S for markers.
Thanks in advance,
How can I convert an image from IPL_DEPTH_8U to IPL_DEPTH_32S?
I need to use the watershed function and I have a marker image in IPL_DEPTH_8U. The problem is that the watershed function only accepts IPL_DEPTH_32S for markers.
Thanks in advance,
use Mat
instead of IplImage
then: marker_img.convertTo(marker_img,CV_32S);
If you want to use IplImage, you have to scale the image first and then the conversion
You can try the following if you don t wannt to use the new cv::Mat version :
IplImage* converted = cvCreateImage(cvSize(img->width,img->heigth),IPL_DEPTH_32S);
for (int i = 0;i<img->heigth;i++)
for(int j = 0;j<img-width;j++)
((signed long*)(converted->imageData+i*converted->widthStep))[j] = ((unsigned char*)(img->imageData+i*img->widthStep))[j]
and if you have a 3 channel image !!!!!!!!! you have to use the following :
IplImage* converted = cvCreateImage(cvSize(img->width,img->heigth),IPL_DEPTH_32S,3);
for (int i = 0;i<img->heigth;i++)
for(int j = 0;j<3*(img-width);j++)//since you have 3 channels
((signed long*)(converted->imageData+i*converted->widthStep))[j] = ((unsigned char*)(img->imageData+i*img->widthStep))[j]
and a better didactic way to write the code:
for (int i = 0;i<img->heigth;i++)
for(int j = 0;j<(img-width);j++)
{
((signed long*)(converted->imageData+i*converted->widthStep))[j*3] = ((unsigned char*)(img->imageData+i*img->widthStep))[j*3]//R or B ??
((signed long*)(converted->imageData+i*converted->widthStep))[j*3+1] = ((unsigned char*)(img->imageData+i*img->widthStep))[j*3+1]//G
((signed long*)(converted->imageData+i*converted->widthStep))[j*3+2] = ((unsigned char*)(img->imageData+i*img->widthStep))[j*3+2]//R or B ??
}
Do it manually per pixel per channel with explicit casting:
Img32s->imageData[y*((int)Img32s->widthStep)+x*3+C]=(int)((uchar*)Img8u->imageData)[y*((int)Img8u->widthStep)+x*3+C]/255.;