0

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,

user1931907
  • 41
  • 10

3 Answers3

1

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

Abhishek Thakur
  • 16,337
  • 15
  • 66
  • 97
  • Thanks for your answer...What do you mean by scale the image, is it to convert first image into grayscale? If yes then how to convert, I've tried cvConvertScale(marker, marker32s , 1/255.); but when coming to showing it, it shows me a black box instead of the image... – user1931907 Feb 26 '13 at 14:49
0

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 ??

}
jamk
  • 836
  • 1
  • 10
  • 24
  • I've tried the first code, cause I have a grayscale Image (1 channel)...bu tit shows me the image twice in the window...I don't know why...Please help – user1931907 Feb 26 '13 at 18:56
  • Describe better what you see :) what do you mean with you see the image twice ? side by side left and right ? Or post all the code you are trying. – jamk Feb 27 '13 at 08:29
  • hi, yes it shows me the image left and right...I can't post the image, cause I should have at least 10 reputation...Here is the code...IplImage* img32S = cvCreateImage(cvGetSize(img8U),IPL_DEPTH_32S,1); for (int i = 0 ; i < img8U->height ; i++) for(int j = 0 ; j < img8U->width ; j++) ((signed long*)(img32S->imageData+ ( j * img32S->widthStep)))[i] = ((unsigned short*)(img8U->imageData+img8U->widthStep*j))[i]; – user1931907 Feb 27 '13 at 09:04
  • I have edited my code new now it should work ! My example was for 16 bit pixels but you have 8 bit pixels (unsigned char) try again and good luck – jamk Feb 27 '13 at 10:30
  • thanks for your reply...It shows a black window...sorry for question but i'm beginner... – user1931907 Feb 27 '13 at 10:45
0

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.;
LovaBill
  • 5,107
  • 1
  • 24
  • 32
  • thnks for you answer, what do you mean by '+C'...? – user1931907 Feb 26 '13 at 16:08
  • this Code will not work since you use x to iterate over pixels !! You have to cast the (unsigned char*) pointer of imageData to unsigned long first or you have to step over 4 bytes (32 bits are 4 bytes) ! – jamk Feb 26 '13 at 16:48
  • @user1931907 C is the color channel. 2,1,0 for R,G,B. – LovaBill Feb 27 '13 at 08:06